首页 > 代码库 > 管道文件和I/O文件用途
管道文件和I/O文件用途
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 [macg@localhost ~]$ ls -l |
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
[macg@localhost ~]$ Connection closed by foreign host.
[macg@localhost 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 ‘^]‘. |
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
[macg@machome ~]$ Connection closed by foreign host.
Exit
[macg@machome ~]$ 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
[macg@localhost 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
[macg@localhost tiptest]$ pkill tail
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文件用途