某些时候需要定时获取下设备的指定信息,一个个telnet这个时候就显的不够用了,可以用脚本方式去批量执行
只要知道如何在shell脚本中自动执行你的命令,就可以实现了批量化
举例:登录路由器查看某个网口的流量情况#脚本内容[间隔时间自已看情况调整]:
#!/bin/bash
(sleep 1;echo "test";sleep 1;echo "passwd";sleep 1;echo "dis int g0/0/0";sleep 1;echo " ";sleep 1;echo "quit")|telnet 192.168.1.1
#下面是[自动执行]的过程:
[root@localhost ~]# sh telnet.sh
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.
Login authentication
Username:test
Password:
-----------------------------------------------------------------------------
User last login information:
-----------------------------------------------------------------------------
Access Type: Telnet
IP-Address : 192.168.1.10
Time : 2018-08-08 16:03:55+08:00
-----------------------------------------------------------------------------
dis int g0/0/0
GigabitEthernet0/0/0 current state : UP
Line protocol current state : DOWN
Description:HUAWEI, AR Series, GigabitEthernet0/0/0 Interface
Route Port,The Maximum Transmit Unit is 1500
Internet protocol processing : disabled
IP Sending Frames' Format is PKTFMT_ETHNT_2, Hardware address is 68cc-6e8a-9939
Last physical up time : 2018-07-10 11:18:53 UTC+08:00
Last physical down time : 2018-07-10 11:04:15 UTC+08:00
Current system time: 2018-08-08 16:11:15+08:00
Port Mode: FORCE COPPER
Speed : 1000, Loopback: NONE
Duplex: FULL, Negotiation: ENABLE
Mdi : AUTO, Clock : -
Last 300 seconds input rate 7159936 bits/sec, 943 packets/sec
Last 300 seconds output rate 2765752 bits/sec, 689 packets/sec
Input peak rate 208110760 bits/sec,Record time: 2018-07-24 12:18:16
Output peak rate 175132424 bits/sec,Record time: 2018-08-06 11:45:05
Input: 1694029507 packets, 1392763516032 bytes
Unicast: 1688764340, Multicast: 20219
Broadcast: 5244948, Jumbo: 0
Discard: 0, Total Error: 0
CRC: 0, Giants: 0
Jabbers: 0, Throttles: 0
Runts: 0, Symbols: 0
Ignoreds: 0, Frames: 0
Output: 1566820622 packets, 1170387114814 bytes
Unicast: 1566820620, Multicast: 0
Broadcast: 2, Jumbo: 27249
Discard: 0, Total Error: 0
Collisions: 0, ExcessiveCollisions: 0
Late Collisions: 0, Deferreds: 0
Input bandwidth utilization threshold : 100.00%
Output bandwidth utilization threshold: 100.00%
Input bandwidth utilization : 0.48%
Output bandwidth utilization : 0.28%
Connection closed by foreign host.
需要指定的信息,只用把结果导入到文本中用命令截取打印出来就行#脚本内容:
#!/bin/bash
(sleep 1;echo "test";sleep 1;echo "passwd";sleep 1;echo "dis int g0/0/0";sleep 1;echo " ";sleep 1;echo "quit")|telnet 192.168.1.1 >route.txt
egrep "^Last|^Input|^Output" route.txt
[root@localhost ~]# sh telnet.sh
Connection closed by foreign host.
Last physical up time : 2018-07-10 11:18:53 UTC+08:00
Last physical down time : 2018-07-10 11:04:15 UTC+08:00
Last 300 seconds input rate 6722328 bits/sec, 965 packets/sec
Last 300 seconds output rate 3974880 bits/sec, 825 packets/sec
Input peak rate 208110760 bits/sec,Record time: 2018-07-24 12:18:16
Output peak rate 175132424 bits/sec,Record time: 2018-08-06 11:45:05
Input: 1694537633 packets, 1393198165577 bytes
Output: 1567259062 packets, 1170657942563 bytes
把查询的结果发送到你的邮箱#发送结果到邮件脚本内容:
#!/bin/bash
GK="g0/0/0"
(sleep 1;echo "test";sleep 1;echo "passwd";sleep 1;echo "dis int $GK";sleep 1;echo " ";sleep 1;echo "quit")|telnet 192.168.1.1 >route.txt
egrep "^Last|^Input|^Output" route.txt> info.txt
python /shell/e.py [email protected] 路由器-$GK-网口信息 "`cat info.txt`"
[root@localhost ~]#sh telnet.sh
Connection closed by foreign host.
邮件发送成功
商业转载请联系作者获得授权,非商业转载请注明出处 本文地址:https://me.jinchuang.org/archives/246.html