【发布时间】:2016-02-03 02:19:13
【问题描述】:
以下脚本 telnet 到从 txt 文件调用的各种设备,然后运行命令,记录输出,然后退出。但是,当使用不正确的 IP 地址时,脚本会显示输入的用户名和密码,这在日志文件中可见,这并不理想。知道如何插入超时来防止这种情况发生吗?
#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
【问题讨论】: