shell项目-分发系统-expect

安装expect

[[email protected] ~]# yum install -y expect

用脚本登陆远程机器
编写脚本1.expect
内容如下:

[[email protected] expect]# vim 1.expect
#!/usr/bin/expect
set host "192.168.130.128"     #定义变量
set passwd "6811327"             #定义变量
spawn ssh [email protected]$host
expect {
"yes/no" {send "yes\r"; exp_continue }        #当提示语句出现yes/no 输入yes然后继续
"password:" {send "$passwd\r" }                #当提示语句出现password 输入密码
}
interact

执行结果

[[email protected] expect]# chmod a+x 1.expect ; ./1.expect
spawn ssh [email protected]
[email protected]‘s password:
Last login: Wed Apr 25 07:48:41 2018 from 192.168.130.1
[[email protected] ~]# 

用脚本登陆远程机器执行命令后退出
编写脚本2.expect
内容如下:

[[email protected] expect]# vim 2.expect

#!/usr/bin/expect
set user "root"
set passwd "6811327"
spawn ssh [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"
expect "]*"
send "ls -l /tmp/12.txt\r"
expect "]*"
send "cat /tmp/12.txt\r"
expect "]*"
send "exit\r"

执行结果

[[email protected] expect]# chmod a+x 2.expect ; ./2.expect
spawn ssh [email protected]
[email protected]‘s password:
Last login: Wed Apr 25 08:07:55 2018 from 192.168.130.116
[[email protected] ~]# touch /tmp/12.txt
[[email protected] ~]# echo 1212 > /tmp/12.txt
[[email protected] ~]# ls -l /tmp/12.txt
-rw-r--r-- 1 root root 5 4月  25 08:32 /tmp/12.txt
[[email protected] ~]# cat /tmp/12.txt
1212
[[email protected] ~]# [[email protected] expect]# 

脚本传递参数
编写脚本3.expect
内容如下:

[[email protected] expect]# vim 3.expect

#!/usr/bin/expect

set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "6811327"
set cm [lindex $argv 2]
spawn ssh [email protected]$host

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"

执行结果

[[email protected] expect]# chmod a+x 3.expect ; ./3.expect root 192.168.130.128 ls
spawn ssh [email protected]
[email protected]‘s password:
Last login: Wed Apr 25 08:32:02 2018 from 192.168.130.116
[[email protected] ~]# ls
anaconda-ks.cfg  com  zabbix-release-3.2-1.el7.noarch.rpm
[[email protected] expect]# ./3.expect root 192.168.130.128 "ls;w;ps aux|grep sshd"
spawn ssh [email protected]
[email protected]‘s password:
Last login: Wed Apr 25 08:50:42 2018 from 192.168.130.116
[[email protected] ~]# ls;w;ps aux|grep sshd
anaconda-ks.cfg  com  zabbix-release-3.2-1.el7.noarch.rpm
 08:52:15 up  1:04,  2 users,  load average: 0.00, 0.01, 0.01
USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.130.1    07:48   15.00s  0.01s  0.01s -bash
root     pts/1    192.168.130.116  08:52    0.00s  0.00s  0.00s w
root        826  0.0  0.4 105996  4128 ?        Ss   07:48   0:00 /usr/sbin/sshd -D
root        947  0.0  0.5 148316  5376 ?        Ss   07:48   0:00 sshd: [email protected]/0
root       1098  0.0  0.5 148312  5504 ?        Ss   08:52   0:00 sshd: [email protected]/1
root       1118  0.0  0.0 112680   984 pts/1    S+   08:52   0:00 grep --color=auto sshd
[[email protected] ~]# [[email protected] expect]# 

设置超时
在执行的命令下面加上一行
set timeout -1 #永不超时
set timeout 1 #1秒超时
set timeout 5 #5秒超时
例如:
expect "]*"
send "vmstat 1" #这个命令会一直执行
set timeout -1

expect "]*"
send "vmstat 1" #这个命令会5秒后自动停止
set timeout 5

自动同步文件
编写脚本
内容如下:

[[email protected] expect]# vim 4.expect

#!/usr/bin/expect
set passwd "6811327"
spawn rsync -av [email protected]:/tmp/12.txt /tmp/

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}

expect eof

执行结果

[[email protected] expect]# chmod a+x 4.expect ; ./4.expect
spawn rsync -av [email protected]:/tmp/12.txt /tmp/
[email protected]‘s password:
receiving incremental file list
12.txt

sent 30 bytes  received 84 bytes  228.00 bytes/sec
total size is 5  speedup is 0.04
[[email protected] expect]# cat /tmp/12.txt
1212

指定host和要同步的文件
编写脚本5.expect
内容如下:

[[email protected] expect]# vim 5.expect

#!/usr/bin/expect

set passwd "6811327"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av $file [email protected]$host:$file

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}

expect eof

执行结果

[[email protected] expect]# chmod a+x 5.expect ; ./5.expect 192.168.130.128 "/tmp/12.txt"
spawn rsync -av /tmp/12.txt [email protected]:/tmp/12.txt
[email protected]‘s password:
sending incremental file list

sent 31 bytes  received 12 bytes  86.00 bytes/sec
total size is 5  speedup is 0.12
[[email protected] expect]# 

原文地址:http://blog.51cto.com/10963213/2107524

时间: 2024-08-29 14:42:56

shell项目-分发系统-expect的相关文章

shell项目-分发系统-构建文件分发系统

shell项目-分发系统-构建文件分发系统 需求背景对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台.所以,自动同步文件是至关重要的. 实现思路首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可. 核心命令rsync -av --files-from=list.txt / [email protected]:/ 文件分发系统的实现 1.rsync.expect 内容 #!/u

分发系统-expect

分发系统-expect expect是一种自动交互语言,能实现在shell脚本中为scp和ssh等自动输入密码自动登录.登录多台系统执行指定命令:创建文件最好以expect结尾: 安装包 yum install -y expect 自动远程登录 1.expect代码: #! /usr/bin/expect set host "192.168.188.3" set passwd "123456" spawn ssh [email protected]$host exp

分发系统-expect讲解,expect脚本远程登录,expect脚本远程执行命令,expect脚本

分发系统-expect讲解 业务越来越大,网站app,后端,编程语言是php,所以就需要配置lamp或者lnmp,最好还需要吧代码上传到服务器上:但是因为业务增加,代码增加,多台机器,就会非常麻烦:这是只需要一个分发系统,就可以把每次更新的代码发布到需要更新的服务器上 expect,是一种脚本语言:通过他可以实现传代码上线,也可以登录机器输入命令 首先要准备一台模板机器,机器的IP,对应用户的密码,通过rsync同步代码,还可以通过expect去执行某些命令 expect脚本远程登录 yum i

shell【分发系统】

第一部分:expect讲解 expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令.当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令.但当不能使用密钥验证的时候,我们就没有办法了.所以,这时间只知道对方机器的账号和密码可以通过expect脚本实现登录和远程命令. 使用expect之前,需要安装expect: yum install -y expect 1.自动远程登录,并执行命令 首先来看一个登录后不退出的脚本 vim 1.expect      #需使用e

2018-4-25 18周1次课 分发系统-expect讲解(上)

20.27 分发系统介绍 由于业务迭代更新,需要更改代码,如果机器很多,那么久需要一个分发系统,可以把每段时间更新的代码分别发布到机器上去 分发系统就是上线的shell脚本,核心为expect expect是一种脚本语言,和shell很像,可以用它去实现传输文件和远程执行命令,不需要去输入密码 20.28 expect脚本远程登录 [[email protected] ~]# yum install -y expect (过程省略) ·自动远程登录 expect中定义变量和shell中的区别 s

2018-4-27 18周2次课 分发系统-expect讲解(下)

20.31 expect脚本同步文件 ·自动同步文件 [[email protected] sbin]# chmod a+x 4.expect [[email protected] sbin]# ./4.expect spawn rsync -av [email protected]:/tmp/12.txt /tmp/ [email protected]'s password: receiving incremental file list 12.txt sent 30 bytes  recei

分发系统expect远程登录,执行命令,传递参数

分发系统:shell 上线脚本expect实现远程传输文件,执行命令,系统上线等功能expect 脚本远程登录vim 1.expect#! /usr/bin/expectset host "192.168.91.129"set passwd "1q2w3e"spawn ssh [email protected]$hostexpect {"yes/no" { send "yes\r"; exp_continue}"as

分发系统——expect

20.27 分发系统介绍 expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令.当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令.但当不能使用密钥验证的时候,我们就没有办法了.所以,这时候只要知道对方机器的账号和密码就可以通过expect脚本实现登录和远程命令. 分发准备: 模板脚本.服务器IP.用户名.密码.expect脚本 20.18 expect脚本远程登录 使用expect前需要先安装: [[email protected] ~]# yum ins

20.31 expect脚本同步文件;20.32 expect脚本指定host和要同步的文件;20.33 构建文件分发系统;20.34

20.31 expect脚本同步文件 自动同步文件 1. 同步远程机器hao2上/tmp/12.txt文件 到本机/tmp/下: [[email protected] ~]# vim 4.expect 添加内容: #!/usr/bin/expect set passwd "admin" spawn rsync -av [email protected]192.168.211.129:/tmp/12.txt /tmp/ expect { "yes/no" { send