lbdbzj110 发表于 2018-7-16 12:47:40

Linux 下使用expect自动telnet/ssh cisco设备执行ping测试

  R1#tclsh
  R1(tcl)#foreach address { ip_address1 ip_address2 ip-address3 } { ping $address re 5 si 1500 }
  R1(tcl)#tclquit
  For login one router, then from there to ping other devices. We can view result from web

  yum -y install expect
  please refer http://hj192837.blog.51cto.com/655995/1352783 to send mail via outside ISP
  vi /tmp/telnet.sh
  #!/usr/bin/expect -f
  #telnet auto login
  set timeout -1
  set host 192.168.1.20
  set user cisco
  set password cisco
  spawn telnet $host
  expect "*name:"
  send "$user\r"
  expect "*word:"
  send "$password\r"
  foreach address { 192.168.1.21 192.168.1.22 } {
  expect "*#"
  send "ping $address\r"
  }
  expect "*#"
  send "exit\n"
  #sleep is apply on above send command
  sleep 2
  expect eof
  vi /tmp/pingtest.sh
  ping -c 5 192.168.1.20 &> /dev/null
  result=$?
  if [ $result -eq 0 ]
  then
  /tmp/telnet.sh > /tmp/telnet-pingtest.txt
  echo "ping test result" | mail -s "ping test result" -a /tmp/telnet-pingtest.txt 133xxxxxxxx@189.cn
  else
  echo "can not reach R1" | mail -s "can not reach R1" 133xxxxxxxx@189.cn
  fi
  chmod +x /tmp/telnet.sh /tmp/pingtest.sh
  首先完成ssh cisco@192.168.1.20
  vi /tmp/ssh.sh
  #!/usr/bin/expect -f
  #ssh auto login
  set timeout -1
  set host 192.168.1.20
  set user cisco
  set password cisco
  spawn ssh $host -l $user
  expect "*password:"
  send "$password\r"
  foreach address { 192.168.1.21 192.168.1.22 } {
  expect "*#"
  send "ping $address\r"
  }
  expect "*#"
  send "exit\n"
  #sleep is apply on above send command
  sleep 2
  expect eof
  vi /tmp/pingtest.sh
  ping -c 5 192.168.1.20 &> /dev/null
  result=$?
  if [ $result -eq 0 ]
  then
  /tmp/ssh.sh > /tmp/ssh-pingtest.txt
  echo "ping test result" | mail -s "ping test result" -a /tmp/ssh-pingtest.txt 133xxxxxxxx@189.cn
  else
  echo "can not reach R1" | mail -s "can not reach R1" 133xxxxxxxx@189.cn
  fi
  chmod +x /tmp/ssh.sh /tmp/pingtest.sh
  You can add to crontab to send your mail periodically.
页: [1]
查看完整版本: Linux 下使用expect自动telnet/ssh cisco设备执行ping测试