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

分发系统介绍

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

 

 

expect脚本远程登录

  • yum install -y expect

  • 自动远程登录

[root@aminglinux-02 sbin]# vim 1.expect

#! /usr/bin/expect
set host "192.168.133.132"
set passwd "123456"
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@aminglinux-02 sbin]# ./1.expect
-bash: ./1.expect: 权限不够
[root@aminglinux-02 sbin]# chmod a+x 1.expect
  • 成功登录
[root@aminglinux-02 sbin]# ./1.expect
spawn ssh [email protected]
[email protected]'s password:
Last failed login: Wed Sep 20 23:31:03 CST 2017 from 192.168.133.131 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Sep 20 22:29:52 2017 from 192.168.133.1
[root@aminglinux-01 ~]# ^C

 

 

expect脚本远程执行命令

  • 动远程登录后,执行命令并退出
#!/usr/bin/expect
set user "root"
set passwd "123123a"
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 "exit\r"
  • expect "]" : “” 通配]右边所有字符,

  • send 执行命令

  • A 机器执行脚本

[root@aminglinux-02 sbin]# ./2.expect
-bash: ./2.expect: 权限不够
[root@aminglinux-02 sbin]# chmod a+x 2.expect
[root@aminglinux-02 sbin]# ./!$
./2.expect
spawn ssh [email protected]
[email protected]'s password:
Last login: Wed Sep 20 23:31:49 2017 from 192.168.133.131
[root@aminglinux-01 ~]# touch /tmp/12.txt
[root@aminglinux-01 ~]# echo 1212 > /tmp/12.txt
  • B机器查看脚本运行的命令是否成功
[root@aminglinux-01 ~]# ls /tmp/
12.txt      systemd-private-7723d189c8d3469ab732edadbbc340cb-nginx.service-eKHAIK
mysql.sock  systemd-private-7723d189c8d3469ab732edadbbc340cb-vmtoolsd.service-1yxB4M
[root@aminglinux-01 ~]# cat /tmp/12.txt
1212

 

 

expect脚本传递参数

  • 传递参数
#!/usr/bin/expect
set user [lindex $argv 0]   //第一个参数
set host [lindex $argv 1]   //第二个参数
set passwd "123123a"
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@aminglinux-02 sbin]# ./3.expect root 192.168.133.130 ls
spawn ssh [email protected]
[email protected]'s password:
Last failed login: Thu Sep 21 01:57:01 CST 2017 from 192.168.133.131 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Thu Sep 21 01:51:46 2017 from 192.168.133.1
[root@aminglinux-01 ~]# ls
anaconda-ks.cfg
  • 支持多条参数
[root@aminglinux-02 sbin]# ./3.expect root 192.168.133.130 "ls;whoami;vmstat 1"
spawn ssh [email protected]
[email protected]'s password:
Last login: Thu Sep 21 01:57:21 2017 from 192.168.133.131
[root@aminglinux-01 ~]# ls;whoami;vmstat 1
anaconda-ks.cfg
root
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 1470532    876 189616    0    0    12     2   42   93  0  0 100  0  0
0  0      0 1470532    876 189616    0    0     0     0   41   91  0  1 99  0  0
0  0      0 1470532    876 189616    0    0     0     0   33   74  0  0 100  0  0
0  0      0 1470532    876 189616    0    0     0     0   37   83  0  0 100  0  0
0  0      0 1470532    876 189616    0    0     0     0   35   77  0  0 100  0  0
^C
  • 因为vmstat 1 是持续运行的。所以脚本最后的exit 就没有办法执行,只能手动终止

 

 

 

 

 

 

 

你可能感兴趣的:(linux)