本文共 4494 字,大约阅读时间需要 14 分钟。
expect脚本教程
Expect script is a great linux/unix utility. I have to deal with a lot of unix servers in my daily life, whether it’s at work or it’s my hosting server. So I need to remember a lot of SSH users, their passwords and then SU users and passwords. It’s kind of messy when the number of servers are huge. So I thought of writing a script that will automatically login me to the server.
Expect脚本是一个很棒的linux / unix实用程序。 无论是在工作还是在托管服务器,我每天都要处理很多Unix服务器。 因此,我需要记住很多SSH用户,他们的密码以及SU用户和密码。 当服务器数量巨大时,这有点混乱。 因此,我想到编写一个脚本,该脚本将自动将我登录到服务器。
When I first started working on this, I got stuck as how to enter the SSH password because normal unix shells don’t have any way to send the password when it gets prompted for login. Then I come to know about expect script
that allows to automate the interaction with programs that opens a terminal for input.
当我第一次开始处理此问题时,我对如何输入SSH密码感到困惑,因为普通的unix shell在提示登录时无法发送密码。 然后,我了解了expect script
,该expect script
允许与打开终端以供输入的程序自动进行交互。
Expect Script is very easy to learn and as the name suggests it works by parsing the output of the command and when it matches the specified regular expression, it processes the specified instruction.
Expect脚本非常易于学习,顾名思义,它可以通过解析命令的输出来工作,并且当它与指定的正则表达式匹配时,它将处理指定的指令。
Here is the script I created for automatically login to the SSH server and then login with super user and then run a simple command.
这是我创建的用于自动登录SSH服务器,然后以超级用户身份登录,然后运行简单命令的脚本。
sshsudologin.expect
sshsudologin.expect
#!/usr/bin/expect#Usage sshsudologin.expectset timeout 60spawn ssh [lindex $argv 1]@[lindex $argv 0]expect "yes/no" { send "yes\r" expect "*?assword" { send "[lindex $argv 2]\r" } } "*?assword" { send "[lindex $argv 2]\r" }expect "# " { send "su - [lindex $argv 3]\r" }expect ": " { send "[lindex $argv 4]\r" }expect "# " { send "ls -ltr\r" }interact
expect
command followed by the regular expression and then what should be send as a response. The first Yes/No choice is added to make sure that it doesn’t fail if remote server key is not already imported. 注意, expect
命令后跟正则表达式,然后发送应作为响应的内容。 添加第一个是/否选项,以确保如果尚未导入远程服务器密钥,则不会失败。 Here is the output when I run the above expect script with correct parameters.
这是当我使用正确的参数运行上述期望脚本时的输出。
pankaj@Pankajs-MacBook-Pro:~$/Users/pankaj/scripts/sshloginsudo.expect 'journaldev.com' 'pankaj' 'ssh_pwd' 'su_user' 'su_pwd'spawn ssh pankaj@journaldev.compankaj@journaldev.com's password: Last login: Sun Jun 9 19:54:17 2013 from c-67-161-57-160.hsd1.ca.comcast.netpankaj@journal [~]# su - su_userPassword: su_user@journal [~]# ls -ltrtotal 708...
alias journal="/Users/pankaj/scripts/sshloginsudo.expect 'journaldev.com' 'pankaj' 'ssh_pwd' 'su_user' 'su_pwd'"
由于所有信息均作为参数传递,因此期望脚本可以重复使用,因此最好为每个参数创建别名以快速登录,并节省键入所有参数的时间。 例如, alias journal="/Users/pankaj/scripts/sshloginsudo.expect 'journaldev.com' 'pankaj' 'ssh_pwd' 'su_user' 'su_pwd'"
NOTE: The above script is tested on Mac OS and Linux systems.
注意 :上面的脚本已在Mac OS和Linux系统上经过测试。
Reference:
参考:
翻译自:
expect脚本教程
转载地址:http://bgozd.baihongyu.com/