0
以下脚本telnet到从txt文件调用的各种设备中,然后运行命令,记录输出并退出。但是,如果使用错误的IP地址,脚本将显示输入的用户名和密码,并且这在日志文件中是不可见的。任何想法如何插入超时以防止这种情况?Expect脚本telnet超时问题
#telnet.exp
######################################################
#!/usr/bin/expect -f
# Set variables
set hostname [lindex $argv 0]
set username [lindex $argv 2]
set password [lindex $argv 1]
# Log the output
log_file -a ~/configuration-telnet.log
# Which device we are working on and at what time
send_user "\n"
send_user ">>>>> Working on $hostname @ [exec date] <<<<<\n"
send_user "\n"
spawn telnet $hostname
expect "Username:"
send "$username\n"
expect "Password:"
send "$password\n"
expect "#"
send "term len 0\r"
send "show running-config\r"
expect "end\r"
send "\r"
send "exit\r"
########################################################
#telnet.sh
########################################################
#!/bin/bash
echo -n "Username:"
read -s -e username
echo -ne '\n'
echo -n "Password:"
read -s -e password
echo -ne '\n'
# Feed the expect script a device list & the collected passwords
for device in `cat telnet-device-list.txt`; do
./telnet.exp $device $password $username;
done