码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-02-28 11:17:47      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:expect脚本   expect脚本远程登录   expect脚本远程执行命令   expect脚本传递参数   

20.27 分发系统介绍

应用场景

当业务越做越大,服务器需求越来越多,几台服务器的话还好一点;当十几、几十台的时候,工作量就非常大!并且不规范,需要一个模板机分发到各个机器上去。

可以用开源的软件,expect脚本语言,进行实现分发系统的功能。

20.28 expect脚本远程登录

  • yum install -y expect

  • 自动远程登录,并执行命令
#!/usr/bin/expect
set host "192.168.21.130"
set passwd "rootroot"
spawn ssh root@$host
expect {
    "yes/no" {send "yes\r"; exp_continue}
    "assword:" {send "$passwd\r"}
}
interact #表示停留在机器上
#如果需要退出 可以expect eof

执行操作

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

#清空远程登录的记录
[root@qingyun-02 sbin]# vi /root/.ssh/known_hosts

#增加执行权限
[root@qingyun-02 sbin]# chmod a+x 1.expect 

#执行
[root@qingyun-02 sbin]# ./1.expect
spawn ssh root@192.168.21.130
The authenticity of host ‘192.168.21.130 (192.168.21.130)‘ can‘t be established.
ECDSA key fingerprint is SHA256:e6Fx3oJ8GcMbFnmTV7JIcvZ3sG6W6yrfvdKccXk+c7c.
ECDSA key fingerprint is MD5:15:57:6c:19:21:a2:e4:3e:df:19:27:13:c2:2e:8e:ba.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.21.130‘ (ECDSA) to the list of known hosts.
root@192.168.21.130‘s password: 
Last login: Wed Feb 28 08:57:58 2018 from 192.168.21.1

20.29 expect脚本远程执行命令

  • 自动远程登录后,执行命令并退出
#脚本代码

#!/usr/bin/expect
set user "root"
set passwd "rootroot"
spawn ssh $user@192.168.21.130

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"

执行

[root@qingyun-02 sbin]# chmod a+x 2.expect 
[root@qingyun-02 sbin]# ./2.expect
spawn ssh root@192.168.21.130
root@192.168.21.130‘s password: 
Last login: Wed Feb 28 09:30:05 2018 from 192.168.21.132
[root@qingyun-01 ~]# touch /tmp/12.txt
[root@qingyun-01 ~]# echo 1212 > /tmp/12.txt
[root@qingyun-01 ~]# [root@qingyun-02 sbin]# 

#查看远程端机上结果
[root@qingyun-02 sbin]# ./1.expect 
spawn ssh root@192.168.21.130
root@192.168.21.130‘s password: 
Last login: Wed Feb 28 09:36:13 2018 from 192.168.21.132
[root@qingyun-01 ~]# ls /tmp/12.txt
/tmp/12.txt
[root@qingyun-01 ~]# cat /tmp/12.txt
1212

20.30 expect脚本传递参数

  • 传递参数
[root@qingyun-02 sbin]# vim 3.expect

#!/usr/bin/expect

set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "rootroot"
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"

执行

[root@qingyun-02 sbin]# chmod a+x 3.expect
[root@qingyun-02 sbin]# ./3.expect root 192.168.21.130 ls
spawn ssh root@192.168.21.130
root@192.168.21.130‘s password: 
Last login: Wed Feb 28 09:39:16 2018 from 192.168.21.132
[root@qingyun-01 ~]# ls
anaconda-ks.cfg  shell
[root@qingyun-01 ~]# [root@qingyun-02 sbin]# 

[root@qingyun-02 sbin]# ./3.expect root 192.168.21.130 "ls;w;vmstat 1"
#当有多个命令 需要用双引号 作为一个参数传进去
spawn ssh root@192.168.21.130
root@192.168.21.130‘s password: 
Last login: Wed Feb 28 09:46:40 2018 from 192.168.21.132
[root@qingyun-01 ~]# ls;w;vmstat 1
anaconda-ks.cfg  shell
 09:49:34 up  1:00,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.21.1     08:57   12:46   0.03s  0.03s -bash
root     pts/1    192.168.21.132   09:49    0.00s  0.00s  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
 3  0      0 206004   2108 197024    0    0    46    11   93  134  0  0 99  1  0
 0  0      0 206020   2108 197040    0    0     0     0   74  105  0  0 100  0  0
 0  0      0 206020   2108 197040    0    0     0     0   76  100  0  0 100  0  0
 0  0      0 206020   2108 197040    0    0     0     9   87  126  0  0 100  0  0
 0  0      0 206020   2108 197040    0    0     0     0   75  101  0  0 100  0  0
 0  0      0 206020   2108 197040    0    0     0    98   82  115  0  1 99  0  0
 0  0      0 206020   2108 197040    0    0     0     5   89  121  0  0 100  0  0
 0  0      0 206020   2108 197040    0    0     0     0   75  108  0  0 100  0  0
 0  0      0 206012   2108 197048    0    0     0     0   92  119  1  1 98  0  0
 0  0      0 206012   2108 197048    0    0     0     0   88  118  0  0 100  0  0

#expect会有超时时间,大概10s左右

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

标签:expect脚本   expect脚本远程登录   expect脚本远程执行命令   expect脚本传递参数   

原文地址:http://blog.51cto.com/3622288/2073780

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!