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

20.27 分发系统介绍


由于业务迭代更新,需要更改代码,如果机器很多,那么久需要一个分发系统,可以把每段时间更新的代码分别发布到机器上去

分发系统就是上线的shell脚本,核心为expect

expect是一种脚本语言,和shell很像,可以用它去实现传输文件和远程执行命令,不需要去输入密码




20.28 expect脚本远程登录


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

(过程省略)

·自动远程登录



expect中定义变量和shell中的区别

shell中是:变量=xxx

expect中是:set 变量 "xxx"

spawn ssh [email protected]$host 登录的语句

yes/no 是指第一次远程登录时,会提示是否连接,如下图

当提示为 yes/no 时,怎么做。send "yes\r"; exp_continue

当提示为 password 时,怎么做。 send "$passwd\r"  (填写关键词接口,也可以是assword)

interact 表示结束,停留在远程的机器上(如果不加这一行,那么登录后马上回退出)

(如果不用interact,而是expect eof ,那么会停留在机器上几秒,然后退出)


[[email protected] sbin]# chmod a+x 1.expect                 ##给执行权限
[[email protected] sbin]# ./1.expect
spawn ssh [email protected]
The authenticity of host '192.168.65.129 (192.168.65.129)' can't be established.
ECDSA key fingerprint is SHA256:PXl0tJsOm3464x52jTxX7L+ToKm1Hb6AUMq6lh4ASX0.
ECDSA key fingerprint is MD5:2e:9a:f9:25:b3:80:43:05:a4:3d:3c:9b:48:6e:a0:0e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.65.129' (ECDSA) to the list of known hosts.
[email protected]'s password:
Last login: Tue Apr 24 22:19:31 2018 from 192.168.65.1
[[email protected] ~]# ifconfig

已经成功登录到了129这台机器




20.29 expect脚本远程执行命令


·自动远程登陆后,执行命令并退出



前一半和之前的登录脚本基本一致,只不过这次set的变量为user

expect "]*" ##遇到 "]*" ,则执行 send "touch /tmp/12.txt\r"

expect "]*" ##再遇到 "]*" ,则 send "echo 1212 > /tmp/12.txt\r"

expect "]*" ##然后 send "exit\r"

因为命令行在未执行命令时都是 ]# 或 ]$ 再最后,因此,这样可以一步一步地执行命令


[[email protected] sbin]# chmod a+x 2.expect
[[email protected] sbin]# ./2.expect
spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Apr 24 22:31:51 2018 from 192.168.65.128
[[email protected] ~]# touch /tmp/12.txt
[[email protected] ~]# echo 1212 > /tmp/12.txt
[[email protected]host ~]# [[email protected] sbin]#  ssh 192.168.65.129
[email protected]'s password:
Last login: Tue Apr 24 22:56:18 2018 from 192.168.65.128
[[email protected] ~]# ll /tmp/12.txt             ##在129上可以看到文本存在
-rw-r--r-- 1 root root 5 4月  24 22:56 /tmp/12.txt
[[email protected] ~]# cat /tmp/12.txt             ##内容为1212
1212



20.30 expect脚本传递参数

·传递参数



set user [lindex $argv 0],set host [lindex $argv 1],set cm [lindex $argv 2]分别表示第1,2,3个参数

设置用户名,主机,命令(cm)

执行过程和之前脚本一致,只不过在执行命令时,需要向在命令行写出3个参数传递到脚本中


[[email protected] sbin]# chmod a+x 3.expect
[[email protected] sbin]# ./3.expect root 192.168.65.129 ls
spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Apr 24 22:57:58 2018 from 192.168.65.128
[[email protected] ~]# ls
1.txt  aminglinux       a.txt  b.txt  sed       test   test1.txt  test2.txt  test3.txt  x.txt
2.txt  anaconda-ks.cfg  awk    grep   ssl.conf  test1  test2      test3      test.txt   zabbix-release-3.2-1.el7.noarch.rpm

(root为参数1,ip为参数2,命令ls为参数3)

如果想要传递多个命令,需要用双引号括起来,并且命令之间用分号隔开

[[email protected] sbin]# ./3.expect root 192.168.65.129 "ls;w;vmstat 1"
spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Apr 24 23:22:08 2018 from 192.168.65.128
[[email protected] ~]# ls;w;vmstat 1
1.txt  aminglinux       a.txt  b.txt  sed       test   test1.txt  test2.txt  test3.txt  x.txt
2.txt  anaconda-ks.cfg  awk    grep   ssl.conf  test1  test2      test3      test.txt   zabbix-release-3.2-1.el7.noarch.rpm
23:22:30 up  1:04,  2 users,  load average: 0.02, 0.03, 0.05
USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.65.1     22:19   50:30   0.04s  0.04s -bash
root     pts/1    192.168.65.128   23:22    0.00s  0.03s  0.00s w
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
2  0      0 187692   2076 203060    0    0    44    68  129  160  0  1 99  0  0
1  0      0 187584   2076 203060    0    0     0     8  137  156  0  0 100  0  0
0  0      0 187584   2076 203060    0    0     0   548  123  142  0  1 99  0  0
0  0      0 187584   2076 203060    0    0     0     5  142  161  0  0 100  0  0
0  0      0 187584   2076 203060    0    0     0   104  121  151  0  0 100  0  0
0  0      0 187584   2076 203060    0    0     0    11  136  167  0  0 100  0  0
0  0      0 187584   2076 203060    0    0     0     6  124  144  0  0 100  0  0
0  0      0 187336   2076 203060    0    0     0    12  159  178  0  1 98  1  0
0  0      0 187368   2076 203060    0    0     0   196  141  165  0  0 100  0  0
0  0      0 187368   2076 203060    0    0     0     5  159  173  1  1 98  0  0

(因为expect有超时时间,所有10秒后会退出)


原文地址:http://blog.51cto.com/11530642/2107496

时间: 2024-07-30 11:31:36

2018-4-25 18周1次课 分发系统-expect讲解(上)的相关文章

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

2018.4.25 18周1次课

十八周一次课(4月25日) 20.27 分发系统介绍 20.28 expect脚本远程登录 20.29 expect脚本远程执行命令 20.30 expect脚本传递参数 20.27 分发系统介绍 expect是一种能够按照脚本内容里面设定的方式与交互式程序进行"会话"的程序.根据脚本内容,Expect可以知道程序会提示或反馈什么内容以及什么是正确的应答.它是一种可以提供"分支和嵌套结构"来引导程序流程的解释型脚本语言. 我们熟知的shell编程功能虽然很强大,但是

2018.1.25 7周4次课

七周四次课(1月25日) 10.15 iptables filter表案例 10.16/10.17/10.18 iptables nat表应用 10.15 iptables filter表案例 需求:只针对filter表,预设策略INPUT链DROP, 其他两个链ACCEPT,然后针对192.168.37.0/24开通22端口,对所有网段开放80端口,对所有网段开放21端口. 这个需求不算复杂,但是因为有多条规则,所以最好写成脚本的形式.脚本内容如下: vi /usr/local/sbin/ip

2018.1.9 5周2次课

五周第二次课(1月9日) 7.6 yum更换国内源 7.7 yum下载rpm包 7.8/7.9 源码包安装 7.6 yum更换国内源 1.恢复之前备份的文件 2. 进入"/etc/yum.repos.d"目录 3.删除"CentOS-Base.repo"文件 4.下载"163.repo"文件 wget http://mirrors.163.com/.help/CentOS7-Base-163.repo curl -O http://mirrors

2018.1.16 6周2次课

六周第二次课(1月16日) 9.4/9.5 sed 9.4/9.5 sed 其实grep工具的功能还不够强大,它实现的只是查找功能,而不能把查找的内容替换.以前用vim操作文档的时候,可以查找也可以替换, 但只限于在文本内部操作,而不能输出到屏幕上.sed工具以及后面要介绍的awk工具就能把替换的文本输出到屏幕上,而且还有其他更丰富的功能.sed和awk都是流式编辑器,是针对文档的行来操作的. sed  '/x/'p filename:匹配x字符 sed  -n  '/x/'p  filenam

2018.3.1 10周2次课

十周第二次课(3月1日) 11.14/11.15 Apache和PHP结合 11.16/11.17 Apache默认虚拟主机 11.14/11.15 Apache和PHP结合 配置httpd支持php httpd主配置文件/usr/local/apache2.4/conf/httpd.conf vim /usr/local/apache2.4/conf/httpd.conf   //修改以下4个地方 ServerName 搜索ServerName,把#ServerName www.example

2018.4.23 17周4次课

十七周4次课(4月23日) 20.20 告警系统主脚本 20.21 告警系统配置文件 20.22 告警系统监控项目 20.20 告警系统主脚本 创建告警系统的目录: [[email protected] /usr/local/sbin]# mkdir mon [[email protected] /usr/local/sbin]# ls mon  nginx_log_rotate.sh [[email protected] /usr/local/sbin]# cd mon [[email pro

2018.1.29 8周1次课

八周一次课(1月29日) 10.23 linux任务计划cron 10.24 chkconfig工具 10.25 systemd管理服务 10.26 unit介绍 10.27 target介绍 10.23 linux任务计划cron 其实大部分系统管理工作都是通过定期自动执行某个脚本来完成的, 那么如何定期执行某个脚本呢? 这就要借助Linux的cron功能了 Linux任务计划功能的操作都是通过crontab命令来完成的, 其常用的选项有以下几个. -u:表示指定某个用户,不加- u选项则为当

2018.1.8 5周1次课

五周第一次课(1月8日) 7.1 安装软件包的三种方法 7.2 rpm包介绍 7.3 rpm工具用法 7.4 yum工具用法 7.5 yum搭建本地仓库 7.1 安装软件包的三种方法 rpm工具 yum工具 源码包 在Windows系统下安装软件很简单,只要双击后缀为.exe的文件,然后根据提示连续单击"下一步" 按钮即可. 然而在Linux系统下安装软件就没那么容易了,因为我们不是在图形界面下.所以,你必须学会如何在Linux下安装软件 前面我们多次提到了yum命令,它是Red Ha