我正在使用期望登录到远程机器。但我真的不知道这个期望到底是什么。通过谷歌搜索,我了解了这些命令。如何保存这些脚本以及如何执行它?
spawn telnet 10.123.9.111
expect login {send username\r}
expect password {send password\r}
这个对吗 ?欢迎提出改进此脚本的建议。谢谢你。
我正在使用期望登录到远程机器。但我真的不知道这个期望到底是什么。通过谷歌搜索,我了解了这些命令。如何保存这些脚本以及如何执行它?
spawn telnet 10.123.9.111
expect login {send username\r}
expect password {send password\r}
这个对吗 ?欢迎提出改进此脚本的建议。谢谢你。
Save this to a file telnet.exp
:
#!/usr/bin/expect -f
spawn telnet 10.123.9.111
expect login {send username\r}
expect password {send password\r}
interact ;# I assume you want to do something on the remote machine here
Make it executable: chmod 700 telnet.exp
-- you want to use restrictive permissions since you're storing your password in a plain-text file which is not a good idea (you should be using ssh with keys unless the device cannot run ssh)
Run it: ./telnet.exp
If you want to be able to pass variables to your script:
#!/usr/bin/expect -f
lassign $argv ip user passwd
spawn telnet $ip
expect login {send $user\r}
expect password {send $passwd\r}
interact ;# I assume you want to do something on the remote machine here
Run it: ./telnet.exp 10.123.9.111 username secret
This is bad too because now the password will show up in a ps
listing.