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