You expect with a powerful witness - full automation Run

Pain points:

There are two Linux hosts A and B, how to ssh from Host A to Host B, then execute commands on the host B, how to make this process to achieve automation? You might use this approach:

ssh [email protected] "ls"

But this clumsy way, to enter a password every time, but can not perform some complex logic or command. So how to achieve full automation of it? This use to this article today expect a summary.

What expect that?

expect is a free programming tool to automate interactive tasks without human intervention. To put it plainly, expect is a set of software used to implement automatic interactive features.

In practice, we run the command, script or program, these commands, scripts or programs are required to enter certain commands continue to run from the terminal, which require human input manually. The use expect, you can follow the prompts provided to the program procedures, standard analog inputs, enabling automated interaction execution. This is expect! ! !

expect base

1. sendcommand receiving a string parameter, and this parameter is sent to the process.
2.expect command and send commands Instead, expect to wait a commonly used process of feedback, according to our feedback process, and then sends a corresponding command interaction.
3.spawn command used to start a new process, send the spawn and expect commands are open and use spawn processes interact.
4.interact command is actually not a lot of use under normal circumstances spawn, send commands and expect to be very good to complete our task; but in some special cases still need to use the command interact, interact command is used to exit Automation into human interaction. For example, we use spawn, send, and expect to complete the ftp command landed the host, perform file download task, but we hope that after the end of the download file, you can still stay in the ftp command line, in order to manually perform a subsequent command, this time using the interact command It can be very good to complete this task.

case study

For the above article to add bulk hosts, for example, we add a host through zabbix-api interfaces bulk, but still not finished, need to add agent on the monitored host, great if one added workload, so here spend just right.

#!/bin/bash
user=linfan #主机用户名
pass='linfan123' #主机密码
for ip in `cat iplist.txt` #文件中放着需要执行命令的主机IP
do
  /usr/bin/expect << EOF
  set timeout 3  #链接超时3秒
  spawn scp /home/linfan/zabbix_xiufu.sh $user@$ip:/home/linfan/#将要执行的脚本传到需要执行命令的主机
    expect {
        "(yes/no)" {send "yes\r"; exp_continue}#出现“yes/no”的字样,就自动输入“yes”其中exp_continue表示循环式匹配,通常匹配之后都会退出语句,但如果有exp_continue则可以不断循环匹配,输入多条命令,简化写法。
        "password:" {send "$pass\r"}#出现“password:”字样,就自动输入密码
  }
  spawn ssh $user@$ip #远程切换到执行命令的主机中
  expect {
        "password:" {send "$pass\r"}#出现“password:”字样,就自动输入密码
  }
  expect "$user@*"  {send "sudo chmod 755 /home/linfan/zabbix_xiufu.sh\r";}#出现“用户名@任何的”字样 就给与脚本执行权限
  expect "$user@*"  {send "sudo /home/linfan/zabbix_xiufu.sh\r";}#出现“用户名@任何的”字样 就执行脚本
  expect "$user@*"  {send "exit\r"}##出现“用户名@任何的”字样 就退出
EOF
done

Guess you like

Origin blog.51cto.com/13858192/2424314