Expcet

Expcet

expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令。主要是完成较小网络规模的自动化管理

 

Expect常见用法

 

1. #!/usr/bin/expect

这一行告诉操作系统脚本里的代码使用那一个shell来执行。这里的expect其实和linux下的bashwindows下的cmd是一类东西。

 

注意:这一行需要在脚本的第一行。

 

2. spawn ssh -lusername 192.168.1.1

spawn是进入expect环境后才可以执行的expect内部命令,如果没有装expect或者直接在默认的SHELL下执行是找不到spawn命令的。所以不要用which spawn“之类的命令去找spawn命令。好比windows里的dir就是一个内部命令,这个命令由shell自带,你无法找到一个dir.com dir.exe 的可执行文件。

它主要的功能是给ssh运行进程加个壳,用来传递交互指令。

 

4. expect"password:"

这里的expect也是expect的一个内部命令,有点晕吧,expectshell命令和内部命令是一样的,但不是一个功能,习惯就好了。这个命令的意思是判断上次输出结果里是否包含“password:”的字符串,如果有则立即返回,否则就等待一段时间后返回,这里等待时长就是前面设置的30

 

5. interact

执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。如果你只是登录过去执行

 

6.$argv 参数数组

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

 

 

Expect的安装

Yum installexpect

 

Expect案例1:自动远程登录

 

#!/usr/bin/expect

set host"192.168.0.116"

set passwd"123456"

spawnssh  root@$host

expect {

"yes/no"{ send "yes\r"; exp_continue}

"assword:"{ send "$passwd\r" }

}

Interact

 

执行结果

[root@nagiossbin]# ./1.expect

spawn [email protected]

Theauthenticity of host '192.168.0.116 (192.168.0.116)' can't be established.

RSA keyfingerprint is cb:23:84:1d:d3:44:81:4a:ac:37:fe:fe:a6:c4:79:a7.

Are you sureyou want to continue connecting (yes/no)? yes

Warning:Permanently added '192.168.0.116' (RSA) to the list of known hosts.

Address192.168.0.116 maps to bogon, but this does not map back to the address -POSSIBLE BREAK-IN ATTEMPT!

[email protected]'spassword:

Last login:Thu Jun 25 22:28:23 2015

[root@bogon ~]#ifconfig

eth1      Link encap:Ethernet  HWaddr 00:0C:29:98:08:49

          inet addr:192.168.0.116  Bcast:192.168.0.255  Mask:255.255.255.0

 

案例2:自动登录,执行指定命令

#!/usr/bin/expect

set user"root"

set passwd"123456"

spawn [email protected]

expect {

"yes/no"{ send "yes\r"; exp_continue}

"password:"{ send "$passwd\r" }

}

expect"]*"                                 

send"touch /tmp/12.txt\r"                    #执行的命令

expect"]*"

send"echo 1212 > /tmp/12.txt\r"             #\r代表换行

expect"]*"

send"exit\r"      

 

案例3:传递参数,命令该脚本为3.expect   

#!/usr/bin/expect

set user[lindex $argv 0]                 #[lindex $argv 0] 表示第一个参数

set host[lindex $argv 1]

set passwd"123456"

set cm[lindex $argv 2]

spawn ssh$user@$host

expect {

"yes/no"{ send "yes\r"}

"password:"{ send "$passwd\r" }

}

expect"]*"

send"$cm\r"

expect"]*"

send"exit\r"  

 

执行方法:3.expect root 192.168.0.116 w;top;last   

“w;top;last” 表示可以执行3条命令

 

案例4自动同步文件rsync的使用),命名为4.expect

#!/usr/bin/expect

set passwd"123456"

spawn rsync-av [email protected]:/tmp/12.txt /tmp/

expect {

"yes/no"{ send "yes\r"}

"password:"{ send "$passwd\r" }

}

expecteof                   #expcet eof该句的作用主要是让spawn进程运行完毕,在结束该脚本,如果不加该句,那么如上所示,执行完password:" { send "$passwd\r" }就关闭该脚本。

 

 

执行结果:

[root@nagiossbin]# ./4.expect

spawn rsync-av [email protected]:/tmp/12.txt /tmp/

Address192.168.0.116 maps to bogon, but this does not map back to the address -POSSIBLE BREAK-IN ATTEMPT!

[email protected]'spassword:

receivingincremental file list

12.txt

 

sent 30bytes  received 84 bytes  228.00 bytes/sec

total size is5  speedup is 0.04

 

案例5构建文件分发系统

1. 需求背景

对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。

 

2. 实现思路

首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。

3. 核心命令

rsync -av--files-from=list.txt /  root@host:/

#--file-from read list ofsource-file names from FILE

             读取指定文件的名称,相对于路径

4. 文件分发系统的实现

 

4.1 expect脚本,命令为5.expect

#!/usr/bin/expect

set passwd"123456"

set host[lindex $argv 0]

set file[lindex $argv 1]

spawn rsync-av --files-from=$file / root@$host:/

expect {

"yes/no"{ send "yes\r"}

"password:"{ send "$passwd\r" }

}

expect eof

 

4.2 IP用户,命令为ip.list,其内容如下

192.168.0.116

192.168.0.115

 

4.3 建立文件list.txt

/tmp/1

/tmp/2

/tmp/3

说明:rsync读取指定文件目录或者路径,用于同步。

 

4.4 shell脚本,命名为rsync.sh,用于循环执行5.expect

#!/bin/bash

for ip in`cat ip.list`                    #读取ip.list的内容赋值给ip变量

do

    echo $ip

    ./5.expect $ip list.txt               #调用5.expect,需要更新的内容为list.txt中指定的内容

done

 

注意:5.expect ip.listrsync.shlist.txt在该运行环境下,都在一个目录。如不在同一个目录,需要指定相应的目录。

 

总结:文件分发系统,其实expect部分主要是接收相关参数,然后通过shell的循环去不断执行expect,最终达到对所有指定机器执行相关动作

 

 

 


你可能感兴趣的:(except)