shell programming (c)

1. The distribution system introduced

        Today, some of the larger companies, mostly using load balancing, and sometimes because of some program you want to change, or some bug to be modified, quickly update code, etc., if only a few server, it is very simple, to have a good change program copy in the past, or rsync remote push, and then, or NFS share online what you can; but if there are hundreds of dozens, that approach would be too cumbersome, then you can expect to bulk distribution task.

  • expect: a automatic interactive features of the software suite, a scripting language based on Tcl has a simple syntax;

  • Features: automatic login to the remote machine, and automatically execute the command; and shell scripts in combination, can be fully automated;

  • Note: The same automatic login and automatic remote execution of commands can be achieved if the use of non-key authentication password. But you can not use key authentication, we have no other way. So, at this time only know each other machine account and password to log in and can be achieved through remote commands expect script.

2.expect remote login script

1. Install the software suite expect command: yum install -y expect

2. Complete remote login and execution of the script

Host: 192.168.2.168

Remote end: 192.168.2.188  

(1) On the host side, into the / usr / local / sbin /, expect to create a script; vim 1.expect

(2) to log in directly to the remote end with ssh 192.168.2.168

[root@ying01 sbin]# ssh 192.168.2.188

[email protected]'s password:

Last login: Thu Aug 2 08:28:02 2018 from 192.168.2.168

[Root @ ying02 ~] # logout

Connection to 192.168.2.188 closed.

(3) grant 1.expect executable permission to automatically connect remote end.

[root@ying01 sbin]# ./ 1.expect

-bash: ./: is a directory

[root@ying01 sbin]# ./1.expect

-bash: ./1.expect: 权限不够

[root@ying01 sbin]# chmod a+x 1.expect

[root@ying01 sbin]# ./1.expect

spawn ssh [email protected]

[email protected]'s password:

Last login: Thu Aug 2 15:22:34 2018 from 192.168.2.168

为了让远程登录时候出现提示,可以清空/root/.ssh/known_hosts目录;然后执行1.except,可以看到脚本,自动执行

清空密匙:

自动执行了:

3.

3. expect脚本远程执行命令

在主机端建立2.expect,查看其脚本

同样需要授予2.expect 访问权限:chmod a+x 2.expect

执行2.expect: #./2.expect 

在远程端查看执行的情况:

4. expect脚本传递参数

expect脚本可以接受从bash传递过来的参数.可以使用[lindex $argv n]获得,n从0开始,分别表示第一个,第二个,第三个….参数

给予可执行权限,并定义参数后执行脚本

#chmod a+x 3.expect

执行命令及结果:

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_40876986/article/details/92851436