管道文件和I/O文件用途——自…

这个要学习一下

    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
total 64
drwxrwxr-x  2 macg macg 4096 Dec  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 '^]'.

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

   192.4.7.0/24 is directly connected, Serial1
   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


    上边script的缺陷,tail -f file & 语句忘记关闭了,所以多次运行script后,会出现多个进程
[macg@machome ~]$ ps -ef | grep "tail -f out"
macg          3678        0 21:27 pts/1      00:00:00 tail -f out
macg          3712        0 21:29 pts/1      00:00:00 tail -f out
macg          3729        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
 
    解决办法二。用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
 




 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值