我尝试在 bash 中设置一个期望脚本。
#!/bin/bash
/usr/bin/expect <<- EOD
set router 192.168.0.251
set user admin
set pass test
set timeout 1000
set filesave [exec date +%m-%d-%Y]
spawn telnet $router
send "\n"
expect "Username:"
send "$user\n"
expect "Password:"
send "$pass\n"
expect ">"
send "en\n"
expect "Password:"
send "$pass\n"
send "term len 0\n"
log_file $router--$filesave.cfg
send "show running-config\n"
expect "end\r"
send "\n"
log_file
send "exit\n"
EOD
cat /Users/test/Desktop/python/$router--$filesave.cfg | grep end
exit 0
我刚得到这个输出
./script2
spawn telnet
telnet> telnet>