1. 程式人生 > >管道文件和I/O文件用途

管道文件和I/O文件用途

memcache telnet

mknod pipe1 p與exec 8 pipe1指令合用,實現自動telnet功能

mknod pipe1 p與exec 8 pipe1指令合用,實現自動telnet功能

#vi autologin.sh

mknod pipe1 p
exec 8<>pipe1 I/O文件與前面建立的管道用<>符號連接到一起

telnet 192.168.1.12 <&8 &
要求其輸入重定向到 I/O文件&8
(引用I/O文件都要用&)
特殊要求:此程序必須在後臺執行,否則後面的指令會
處於“等待”狀態,因為 shell是順序執行指令的
後臺執行&實際是實現並行處理

sleep 1
echo macg > pipe1 用戶名
sleep 1 要求指令輸入之間要有間隔,用sleep sec 調整,
因為主機或ROUTER都有響應時間.
echo 008421 > pipe1 口令
sleep 1
echo "ls -l" > pipe1 指令
sleep 1
echo "exit" > pipe1
執行結果
$ sh autologin.sh
Trying 192.168.1.12...
Connected to www.macg.com.1.168.192.in-addr.arpa (192.168.1.12).
Escape character is ‘^]‘.
Fedora Core release 4 (Stentz)
Kernel 2.6.11-1.1369_FC4 on an i686
login: macg
Password:
Last login: Thu Jan 21 00:37:36 from 192.168.1.11
ls -l
[[email protected] ~]$ ls -l
total 64
drwxrwxr-x 2 macg macg 4096 Dec 3 2006 bigtest
drwxr-xr-x 2 macg macg 4096 Jan 3 10:02 Desktop
drwxrwxr-x 2 macg macg 4096 Jan 17 05:28 filetmp
drwxrwxr-x 2 macg macg 4096 Dec 15 2006 mysqltmp
[[email protected] ~]$ Connection closed by foreign host.
[[email protected] tiptest]$ exit
logout

Connection closed by foreign host.

自動telnet cisco路由器 並執行幾個show命令

$ vi cisco.sh
#!/bin/bash

if (( $# != 2 )); then
echo "usage: $0 host enablepasswd"
exit 1
fi
host=$1
enablepasswd=$2

mknod pipe1 p

exec 8 <> pipe1

telnet 192.168.1.12 <&8 >output & 輸入和輸出都重定向
tail -f $outputfile & 將輸出反顯回屏幕
command=enable
sleep 1
echo $command >> pipe1
sleep 1
echo $enablepasswd >> pipe1

command="show ip int brief"
sleep 1
echo $command >> pipe1

command="show ip route"
sleep 1
echo $command >> pipe1

sleep 1
command="exit"
echo $command >> pipe1
$ sh cisco.sh 192.168.1.150 cisco
Trying 192.168.1.150...
Connected to 192.168.1.150 (192.168.1.150).
Escape character is ‘^]‘.
r5-2509> echo enable
Password:
r5-2509#show ip int brief
Interface IP-Address OK? Method Status Protocol
Ethernet0 192.168.1.150 YES NVRAM up up
Serial0 135.7.35.5 YES NVRAM up down
Serial1 192.4.7.1 YES NVRAM up up
r5-2509#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
ia - IS-IS inter area, * - candidate default, U - per-user static route
o - ODR, P - periodic downloaded static route

Gateway of last resort is 0.0.0.0 to network 0.0.0.0

C 192.4.7.0/24 is directly connected, Serial1
C 192.168.1.0/24 is directly connected, Ethernet0
S* 0.0.0.0/0 is directly connected, Ethernet0
r5-2509exit
[[email protected] ~]$ Connection closed by foreign host.
Exit


上邊script的缺陷,tail -f file & 語句忘記關閉了,所以多次運行script後,會出現多個進程
[[email protected] ~]$ ps -ef | grep "tail -f out"
macg 3678 1 0 21:27 pts/1 00:00:00 tail -f out
macg 3712 1 0 21:29 pts/1 00:00:00 tail -f out
macg 3729 1 0 21:29 pts/1 00:00:00 tail -f out
macg 3770 3645 0 21:33 pts/1 00:00:00 grep tail -f out
解決辦法一。用pgrep,pkill

[[email protected] tiptest]$ ps -ef
UID PID PPID C STIME TTY TIME CMD
。。。
macg 3911 3876 0 01:16 pts/1 00:00:00 tail -f output

[[email protected] tiptest]$ pkill tail

解決辦法二。用ps –ef獲取,然後通過gawk摘出來

com=$(ps -ef | grep "tail -f out" | gawk -F" " ‘{ print $2 }‘)
if [ -f tmp ]
then
rm tmp
fi
echo $com > tmp

com1=$(gawk -F" " ‘{ print $1 }‘ tmp)
com2=$(gawk -F" " ‘{ print $2 }‘ tmp)
com3=$(gawk -F" " ‘{ print $3 }‘ tmp)
kill $com1 >/tmp/null 2>&1
kill $com2 >/tmp/null 2>&1
kill $com3 >/tmp/null 2>&1


計劃任務定期清空memcache數據。

#!/bin/bash

. /root/.bash_profile

PIPE_FILE=/root/shell/tmp_in

PORT=(11211 11212 11213 11214 11215 11216 11217)

if [ -e ${PIPE_FILE} ] ; then

rm ${PIPE_FILE} -rf

fi

#創建管道文件

/bin/mknod ${PIPE_FILE} p

#填寫一個標識符

exec 8<> ${PIPE_FILE}

for i in ${PORT[*]}

do

#判斷端口是否存在

/bin/netstat -tnlp | /bin/grep ${i} >> /dev/null

#clean memcached data

if [ $? == "0" ] ; then

telnet 127.0.0.1 ${i} <&8 &

echo "stats reset" >> ${PIPE_FILE}

sleep 1

echo -e "\035quit" >> ${PIPE_FILE}

fi

done




本文出自 “夢想照進現實” 博客,請務必保留此出處http://lookingdream.blog.51cto.com/5177800/1946397

管道文件和I/O文件用途