之前自己搭建了VPS,苦于每次登录主机时都要密码。所以打算做一个自动登录。expect提供了完全自动化功能。苦于刚接触expcet,只好用autoexpect替代,通过Autoexpect创建一个交互式SSH会话。(autoexpect也不熟悉)
autoexpect
autoexpect能够自动录制脚本。不需手动编辑,只要将需要执行的步骤执行一遍就可以录制.
yuchuanxingdeMacBook-Pro:~ yuchuanxing$ autoexpect -p -f loginVPS.exp
autoexpect started, file is loginVPS.exp
bash-3.2$ exit
autoexpect done, file is loginVPS.exp
yuchuanxingdeMacBook-Pro:~ yuchuanxing$
ctrl + D结束录制。 录制的内容会格式化成 expect的格式,保存在 loginVPS.exp中
Last login: Fri Aug 10 22:02:47 on ttys003
You have new mail.
yuchuanxingdeMacBook-Pro:~ yuchuanxing$ autoexpect -p -f ssh.exp
autoexpect started, file is ssh.exp
bash-3.2$ ssh -l root -p 27961 45.78.3.64
root@45.78.3.64's password:
Last login: Mon Aug 6 12:09:58 2018 from 101.69.254.214
[root@host ~]# exit
logout
Connection to 45.78.3.64 closed.
bash-3.2$ exit
exit
autoexpect done, file is ssh.exp
yuchuanxingdeMacBook-Pro:~ yuchuanxing$ autoexpect ssh.exp
autoexpect started, file is script.exp
couldn't execute "ssh.exp": no such file or directory
while executing
"spawn -noecho ssh.exp"
("eval" body line 1)
invoked from within
"eval spawn -noecho $argv"
invoked from within
"if {[llength $argv]>0} {
eval spawn -noecho $argv
cmd "spawn $argv"
} else {
spawn -noecho $env(SHELL)
cmd "spawn \$env(SHELL)"
}"
(file "/usr/local/bin/autoexpect" line 319)
yuchuanxingdeMacBook-Pro:~ yuchuanxing$ ls -all
total 313472
drwxr-xr-x@ 146 yuchuanxing staff 4672 8 10 23:30 .
drwxr-xr-x 7 root admin 224 10 30 2017 ..
-r-------- 1 yuchuanxing staff 9 8 30 2017 .CFUserTextEncoding
-rw-r--r--@ 1 yuchuanxing staff 22532 8 10 15:42 .DS_Store
drwxr-xr-x 3 yuchuanxing staff 96 4 16 20:04 .ShadowsocksX
drwxr-xr-x 5 yuchuanxing staff 160 4 11 07:24 .ShadowsocksX-NG
drwx------ 10 yuchuanxing staff 320 8 10 22:30 .Trash
drwxr-xr-x 3 yuchuanxing staff 96 9 22 2017 .anaconda
drwxr-x--- 4 yuchuanxing staff 128 7 3 18:04 .android
-rw------- 1 yuchuanxing staff 10171 8 10 23:28 .bash_history
-rw-r--r-- 1 yuchuanxing staff 752 5 19 19:11 .bash_profile
-rw-r--r-- 1 yuchuanxing staff 200 8 31 2017 .bash_profile-anaconda.bak
-rw-r--r-- 1 yuchuanxing staff 440 3 26 07:21 .bash_profile.pysave
drwx------ 689 yuchuanxing staff 22048 8 10 23:27 .bash_sessions
-rw-r--r-- 1 yuchuanxing staff 214 7 6 09:37 .bashrc
drwxr-xr-x 3 yuchuanxing staff 96 8 31 2017 .cocoapods
drwxr-xr-x 4 yuchuanxing staff 128 9 28 2017 .conda
-rw-r--r-- 1 yuchuanxing staff 40 9 28 2017 .condarc
drwx------