分发系统-expect讲解,expect脚本远程登录,expect脚本远程执行命令,expect脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分发系统-expect讲解,expect脚本远程登录,expect脚本远程执行命令,expect脚本相关的知识,希望对你有一定的参考价值。
分发系统-expect讲解
- 业务越来越大,网站app,后端,编程语言是php,所以就需要配置lamp或者lnmp,最好还需要吧代码上传到服务器上;但是因为业务增加,代码增加,多台机器,就会非常麻烦;这是只需要一个分发系统,就可以把每次更新的代码发布到需要更新的服务器上
- expect,是一种脚本语言;通过他可以实现传代码上线,也可以登录机器输入命令
- 首先要准备一台模板机器,机器的IP,对应用户的密码,通过rsync同步代码,还可以通过expect去执行某些命令
expect脚本远程登录
- yum install -y expect
- 自动远程登录
[[email protected] sbin]# vim 1.expect [[email protected] sbin]# vim 1.expect #! /usr/bin/expect set host "192.168.21.129" set passwd "s5381561" spawn ssh [email protected]$host expect { "yes/no" { send "yes "; exp_continue} "password:" { send "$passwd " } } interact [[email protected] sbin]# chmod a+x 1.expect [email protected] sbin]# ./1.expect spawn ssh [email protected] The authenticity of host ‘192.168.21.129 (192.168.21.129)‘ can‘t be established. ECDSA key fingerprint is SHA256:r2y+hZVvvomXE4d3uRSM0cO+kMdqyWAOMqpTtA2qp6I. ECDSA key fingerprint is MD5:ee:da:3b:ea:27:56:38:82:bc:e8:73:10:18:a0:c2:bd. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘192.168.21.129‘ (ECDSA) to the list of known hosts. [email protected]‘s password: Last login: Wed Jul 11 21:59:23 2018 from 192.168.21.1 [[email protected] ~]#
- 在expect 定义变量,用set
- 这个文件是就保证登录信息的,清空的话,重新远程登录ssh 会有提示 /root/.ssh/known_hosts
exp_continue 表示继续 表示换行 interact 继续停留在这台机器,不退出expect脚本远程执行命令
- 远程登录后,执行命令并退出
[[email protected] sbin]# vim 2.expect #!/usr/bin/expect set user "root" set passwd "s5381561" spawn ssh [email protected] expect { "yes/no" { send "yes "; exp_continue} "password:" { send "$passwd " } } expect "]*" send "touch /tmp/12.txt " expect "]*" send "echo 1212 > /tmp/12.txt " expect "]*" send "exit " [[email protected] sbin]# chmod a+x 2.expect [[email protected] sbin]# ./2.expect spawn ssh [email protected] [email protected]‘s password: Last login: Sun Jul 22 22:07:20 2018 from 192.168.21.128 [[email protected] ~]# touch /tmp/12.txt [[email protected] ~]# echo 1212 > /tmp/12.txt [[email protected] ~]# [[email protected] sbin]# [[email protected] ~]# ls /tmp/ 12.txt mysql.sock systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-chronyd.service-5DHBEF systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-vgauthd.service-sG39NN systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-vmtoolsd.service-CVypi4 [[email protected] ~]# cat /tmp/12.txt 1212
- expect "]*" : “” 通配]右边所有字符
- send 执行命令
expect脚本传递参数
- 传递参数
[[email protected] sbin]# vim 3.expect #!/usr/bin/expect set user [lindex $argv 0] #第一个参数赋值给user set host [lindex $argv 1] #第二个参数 set passwd "s5381561" set cm [lindex $argv 2] #第三个参数 spawn ssh [email protected]$host expect { "yes/no" { send "yes "} "password:" { send "$passwd " } } expect "]*" send "$cm " expect "]*" send "exit " [[email protected] sbin]# chmod a+x 3.expect [[email protected] sbin]# ./3.expect root 192.168.21.129 ls spawn ssh [email protected] [email protected]‘s password: Last login: Sun Jul 22 22:14:12 2018 from 192.168.21.1 [[email protected] ~]# ls 111 anaconda-ks.cfg [[email protected] ~]# [[email protected] sbin]# [[email protected] ~]# [[email protected] sbin]# ./3.expect root 192.168.21.129 "ls;w;vmstat 1" spawn ssh [email protected] [email protected]‘s password: Last login: Sun Jul 22 22:19:35 2018 from 192.168.21.128 [[email protected] ~]# ls;w;vmstat 1 111 anaconda-ks.cfg 22:19:51 up 41 min, 2 users, load average: 0.00, 0.02, 0.05 USER TTY FROM [email protected] IDLE JCPU PCPU WHAT root pts/0 192.168.21.1 22:14 55.00s 0.01s 0.01s -bash root pts/1 192.168.21.128 22:19 0.00s 0.01s 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 1142532 2076 173112 0 0 15 14 51 44 0 0 99 0 0 0 0 0 1142780 2076 173208 0 0 0 6 257 149 0 0 100 0 0 0 0 0 1142780 2076 173208 0 0 0 294 188 181 0 0 100 0 0 0 0 0 1142780 2076 173208 0 0 0 0 122 112 0 0 100 0 0 0 0 0 1142780 2076 173208 0 0 0 0 116 105 0 0 100 0 0 0 0 0 1142780 2076 173208 0 0 0 0 107 99 0 0 100 0 0 0 0 0 1142780 2076 173208 0 0 0 0 160 136 0 0 100 0 0 0 0 0 1142780 2076 173208 0 0 0 5 124 119 0 0 100 0 0 0 0 0 1142780 2076 173208 0 0 0 168 157 157 0 0 100 0 0 0 0 0 1142780 2076 173208 0 0 0 0 102 99 0 0 100 0 0 0 0 0 1142780 2076 173208 0 0 0 0 119 113 0 0 100 0 0 [[email protected] sbin]#
- 支持多条参数运行,vmstat 1命令10秒后自动退出
以上是关于分发系统-expect讲解,expect脚本远程登录,expect脚本远程执行命令,expect脚本的主要内容,如果未能解决你的问题,请参考以下文章
分发系统介绍,expect脚本远程登录,expect脚本远程执行命令,expect脚本传递参数
分发系统介绍,expect脚本远程登录, expect脚本远程执行命令, expect脚本传递参数
分发系统介绍expect脚本远程登录expect脚本远程执行命令expect脚本传递参数
分发系统介绍,expect脚本远程登录,expect脚本远程执行命令,expect脚本传递参数