expect脚本教程_Expect脚本SSH示例教程

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)

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脚本非常易于学习,顾名思义,它可以通过解析命令的输出来工作,并且当它与指定的正则表达式匹配时,它将处理指定的指令。

Expect脚本SSH示例 (Expect Script SSH Example)

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.expect <host> <ssh user> <ssh password> <su user> <su password>

set timeout 60

spawn 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脚本的要点 (Important Points about Expect Script)

  1. Notice the first line that specifies that expect script will be used as interpreter.

    注意,第一行指定将期望脚本用作解释器。
  2. Expect script default timeout is 10 seconds, so I have set the timeout to 60 seconds to avoid any timeout issues if the login prompt takes time to come.

    预期脚本的默认超时为10秒,所以我将超时设置为60秒,以避免登录提示需要花费时间时出现任何超时问题。
  3. Notice the 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命令后跟正则表达式,然后发送应作为响应的内容。 添加第一个是/否选项,以确保如果尚未导入远程服务器密钥,则不会失败。
  4. The other expect regular expressions varies across the servers, for my server it ends with “#” but for some other servers it might end with “$”, so you may need to edit it accordingly. The only thing to check is that the regular expression for the expect command should matches, so that it will send the corresponding command or password.

    另一个期望正则表达式在服务器之间会有所不同,对于我的服务器,它以“#”结尾,但对于其他一些服务器,它可能以“ $”结尾,因此您可能需要相应地对其进行编辑。 唯一要检查的是Expect命令的正则表达式应该匹配,以便它将发送相应的命令或密码。
  5. The last expect command just shows that we can send commands also once we are logged into the server.

    最后一个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.com
pankaj@journaldev.com's password: 
Last login: Sun Jun  9 19:54:17 2013 from c-67-161-57-160.hsd1.ca.comcast.net
pankaj@journal [~]# su - su_user
Password: 
su_user@journal [~]# ls -ltr
total 708
...

期望脚本的额外提示 (Extra Tips on expect script)

  1. The expect script can be reused since all the information is passed as arguments, so it’s best to create alias for each one of them for quick login and save your time in typing all the parameters. For example,
    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'"
  2. It’s always best to use single quotes to pass the arguments, since most of the time passwords contains special characters that can cause funny results if they are not quoted.

    始终最好使用单引号来传递参数,因为大多数时候密码包含特殊字符,如果不加引号,它们会导致有趣的结果。

NOTE: The above script is tested on Mac OS and Linux systems.

注意 :上面的脚本已在Mac OS和Linux系统上经过测试。

Reference: SourceForge Page

参考: SourceForge页面

翻译自: https://www.journaldev.com/1405/expect-script-ssh-example-tutorial

expect脚本教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值