分发系统介绍expect脚本远程登录expect脚本远程执行命令 expect脚本传递参数
2018-07-19 22:03
866 查看
分发系统介绍
分发系统-expect讲解(也就是一个分发的脚本)场景:
业务越来越大,网站app,后端,编程语言是php,所以就需要配置lamp或者lnmp,最好还需要吧代码上传到服务器上;但是因为业务增加,代码增加,多台机器,就会非常麻烦;这是只需要一个分发系统,就可以把每次更新的代码发布到需要更新的服务器上
expect,是一种脚本语言;通过他可以实现传输,输入命令(上线代码)
首先要准备一台模板机器,机器的IP,对应用户的密码,通过rsync同步代码,还可以通过expect去执行某些命令
expect脚本远程登录
yum install -y expect自动远程登录 vim 1.expect
[root@yong-01 sbin]# vim 1.expect #! /usr/bin/expect set host "192.168.180.135" set passwd "20655739" spawn ssh root@$host expect { "yes/no" { send "yes\r"; exp_continue} // "password:" { send "$passwd\r" } } interact //脚本结束
在expect 定义变量,用set
这个文件是就保证登录信息的,清空的话,重新远程登录ssh 会有提示 /root/.ssh/known_hosts
exp_continue 表示继续 \r 表示换行 interact 继续停留在这台机器,不退出
加入执行权限
[root@yong-01 sbin]# ./1.expect -bash: ./1.expect: 权限不够 [root@yong-01 sbin]# chmod a+x 1.expect [root@yong-01 sbin]# ll 1.expect -rwxr-xr-x 1 root root 191 7月 19 22:45 1.expect
成功登录
[root@yong-01 sbin]# ./1.expect spawn ssh root@192.168.180.135 The authenticity of host '192.168.180.135 (192.168.180.135)' can't be established. ECDSA key fingerprint is SHA256:oYqovve1b2BwHDBYcFasCiiFzZTHJvKDbTGZAjmlMXc. ECDSA key fingerprint is MD5:3d:f8:af:0d:85:48:db:2a:46:0e:68:5f:eb:43:3e:43. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.180.135' (ECDSA) to the list of known hosts. Last login: Thu Jul 19 21:43:59 2018 [root@yong-02 ~]#
expect脚本远程执行命令
动远程登录后,执行命令并退出 vim 2.expect[root@yong-01 sbin]# vim 2.expect #!/usr/bin/expect set user "root" set passwd "20655739" spawn ssh $user@192.168.180.135 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 "exit\r"
expect "]" *“” 通配]右边所有字符,
send 执行命令
A 机器执行脚本
[root@yong-01 sbin]# ll 2.expect -rw-r--r-- 1 root root 283 7月 19 22:49 2.expect [root@yong-01 sbin]# chmod a+x 2.expect [root@yong-01 sbin]# ./2.expect spawn ssh root@192.168.180.135 Last login: Thu Jul 19 22:45:41 2018 from 192.168.180.134 [root@yong-02 ~]# touch /tmp/12.txt [root@yong-02 ~]# echo 1212 > /tmp/12.txt [root@yong-02 ~]# [root@yong-01 sbin]#
B机器查看脚本运行的命令是否成功
[root@yong-02 ~]# ll /tmp/12.txt -rw-r--r-- 1 root root 5 7月 19 22:50 /tmp/12.txt [root@yong-02 ~]# cat /tmp/12.txt 1212
expect脚本传递参数
传递参数 vim 3.expectroot@yong-01 sbin]# vim 3.expect #!/usr/bin/expect set user [lindex $argv 0] //第一个参数 set host [lindex $argv 1] //第二个参数 set passwd "20655739" 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"
加执行权限 chmod a+x 3.expect
./3.expect [第一个参数] [第二个参数] [第三个参数]
[root@yong-01 sbin]# chmod a+x 3.expect [root@yong-01 sbin]# ./3.expect root 192.168.180.135 "ls;w" spawn ssh root@192.168.180.135 Last login: Thu Jul 19 22:51:33 2018 from 192.168.180.134 [root@yong-02 ~]# ls;w anaconda-ks.cfg cn_windows_7_ultimate_with_sp1_x64_dvd_618537.iso multi-user.target rescue.target a.txt c.txt poweroff.target zabbix-release-3.2-1.el7.noarch.rpm b.txt graphical.target reboot.target 这里需要指定一个用户名的密码文件 22:56:20 up 29 min, 1 user, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.180.134 22:56 4.00s 0.03s 0.00s w
支持多条参数
[root@yong-01 sbin]# ./3.expect root 192.168.180.135 "ls;vmstat 1" spawn ssh root@192.168.180.135 Last login: Thu Jul 19 22:56:10 2018 from 192.168.180.134 [root@yong-02 ~]# ls;vmstat 1 anaconda-ks.cfg cn_windows_7_ultimate_with_sp1_x64_dvd_618537.iso multi-user.target rescue.target a.txt c.txt poweroff.target zabbix-release-3.2-1.el7.noarch.rpm b.txt graphical.target reboot.target 这里需要指定一个用户名的密码文件 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 1119512 2112 190328 0 0 95 50 122 163 0 1 99 0 0 0 0 0 1119516 2112 190360 0 0 0 0 87 95 0 1 99 0 0 0 0 0 1119516 2112 190364 0 0 4 10 108 120 0 0 100 0 0 0 0 0 1119516 2112 190364 0 0 0 0 87 92 0 0 100 0 0 0 0 0 1119516 2112 190364 0 0 0 99 81 95 0 0 100 0 0 0 0 0 1119516 2112 190364 0 0 0 10 113 125 0 1 99 0 0
因为vmstat 1 是持续运行的。所以脚本最后的exit 就没有办法执行,只能手动终止
相关文章推荐
- 分发系统介绍、expect脚本远程登录、expect脚本远程执行命令、expect脚本传递参数
- 分发系统介绍 expect脚本远程登录 expect脚本远程执行命令 expect脚本传递参数
- 分发系统介绍,expect脚本远程登录,远程执行命令,传递参数
- 20.27分发系统介绍;20.28expect脚本远程登录;20.29expect脚本远程执行命令;20.30expect脚本传递参数
- 分发系统介绍、expect脚本远程登录、expect脚本远程执行命令、expect脚本传递参数
- 十七周三次课 2018.02.27分发系统介绍、expect脚本远程登录、expect脚本远程执行命令、expect脚本传递参数
- 分发系统介绍,expect脚本远程登录,expect脚本远程执行命令,expect脚本传递参数
- 【shell编程】分发系统介绍、expect脚本远程登录、expect脚本远程执行命令和expect脚本传递参数
- 分发系统介绍、expect脚本远程执行命令、expect脚本远程传递参数、expect脚本传递参数
- 20.27 分发系统介绍 20.28 expect脚本远程登录 20.29 expect脚本远程执行
- 20.27 分发系统介绍 20.28 expect脚本远程登录 20.29 expect脚本远程执行
- 20.27 分发系统介绍 expect脚本远程登录 脚本远程执行
- 20.27 分发系统介绍 20.28 expect脚本远程登录 20.29 expect脚本远程执行
- expect脚本同步文件,构建文件分发系统,批量远程执行命令
- expect脚本同步文件 expect脚本指定host和要同步的文件 构建文件分发系统 批量远程执行命令
- expect脚本同步文件expect脚本指定host和要同步的文件 构建文件分发系统批量远程执行命令
- 十七周四次课 2018.03.05 expect脚本同步文件、expect脚本指定host和要同步的文件、构建文件分发系统、批量远程执行命令
- expect脚本同步文件、指定host和要同步的文件、构建文件分发系统、批量远程执行命令
- linux学习第七十篇:expect脚本同步文件,expect脚本指定host和要同步的文件,构建文件分发系统,批量远程执行命令
- expect脚本同步文件,expect脚本指定host和要同步的文件,构建文件分发系统,批量远程执行命令