这个要学习一下
mknod pipe1 p与exec 8 pipe1指令合用,实现自动telnet功能
自动telnet cisco路由器 并执行几个show命令
上边script的缺陷,tail -f file & 语句忘记关闭了,所以多次运行script后,会出现多个进程
[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
解决办法二。用ps –ef获取,然后通过gawk摘出来
原文地址:管道文件和I/O文件用途——自动telnet linux和路由器
作者:macg
#vi autologin.sh mknod pipe1 p exec 8<>pipe1 telnet 192.168.1.12 <&8 & sleep 1 echo macg > pipe1 sleep 1 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 total 64 drwxrwxr-x drwxr-xr-x drwxrwxr-x drwxrwxr-x [macg@localhost ~]$ Connection closed by foreign host. [macg@localhost tiptest]$ exit logout Connection closed by foreign host. |
$ vi cisco.sh #!/bin/bash if (( $# != 2 )); then fi host=$1 enablepasswd=$2 mknod pipe1 p exec 8 <> pipe1 telnet 192.168.1.12 <&8 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 Ethernet0 Serial0 Serial1 r5-2509#show ip route Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP Gateway of last resort is 0.0.0.0 to network 0.0.0.0 C C S* r5-2509exit [macg@machome ~]$ Connection closed by foreign host. Exit |
[macg@machome ~]$ ps -ef | grep "tail -f out"
macg
macg
macg
macg
[macg@localhost tiptest]$ ps -ef UID 。。。 macg [macg@localhost tiptest]$ pkill tail |
com=$(ps -ef | grep "tail -f out" | gawk -F" " if [ -f tmp ] then rm tmp fi echo $com > tmp com1=$(gawk -F" " com2=$(gawk -F" " com3=$(gawk -F" " kill $com1 >/tmp/null 2>&1 kill $com2 >/tmp/null 2>&1 kill $com3 >/tmp/null 2>&1 |