您的位置:首页 > 其它

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

2018-02-27 17:06 726 查看

分发系统介绍

expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令。当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令。但当不能使用密钥验证的时候,我们就没有办法了。所以,这时候只要知道对方机器的账号和密码就可以通过expect脚本实现登录和远程命令。

expect脚本远程执行命令

1.安装expect
[root@garytao-01 mon]# yum install -y expect

2.自动远程登录
[root@garytao-01 shell]# vi 1.expect

增加以下脚本内容:

#! /usr/bin/expect
set host "172.16.111.110"     #远程登录IP
set passwd "123456"          #远程登录密码
spawn ssh root@$host       #登录语句
expect {
"yes/no" { send "yes\r"; exp_continue}   #初次登录需要输入yes才能进入
"assword:" { send "$passwd\r" }        #自动输入密码
}
interact             #作用,表示要停留在远程机器了,不会退出来,如果不加就会退出来
#如果是expect eof 就会在机器上停留一两秒退出来

[root@garytao-01 shell]# chmod a+x 1.expect
[root@garytao-01 shell]# ./1.expect
spawn ssh root@172.16.111.110
The authenticity of host '172.16.111.110 (172.16.111.110)' can't be established.
ECDSA key fingerprint is 09:6d:70:42:42:9a:12:69:51:9b:ad:e5:73:98:b9:c0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.111.110' (ECDSA) to the list of known hosts.
root@172.16.111.110's password:
Last login: Mon Feb 26 18:22:32 2018 from 172.16.111.100
[root@garytao-02 ~]# 登出
Connection to 172.16.111.110 closed.
[root@garytao-01 shell]#

expect脚本运程传递参数

1.自动远程登录后,执行命令并退出
[root@garytao-01 shell]# vi 2.expect

增加脚本如下内容:

#!/usr/bin/expect
set user "root"
set passwd 123456"
spawn ssh $user@172.16.111.110

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@garytao-01 shell]# ./2.expect
spawn ssh root@172.16.111.110
root@172.16.111.110's password:
Last login: Tue Feb 27 10:23:28 2018 from 172.16.111.100
[root@garytao-02 ~]# touch /tmp/12.txt
[root@garytao-02 ~]# echo 1212 > /tmp/12.txt

## 回车退出
[root@garytao-02 ~]# [root@garytao-01 shell]#
[root@garytao-01 shell]#

##重新执行自动登录脚本
[root@garytao-01 shell]# ./1.expect
spawn ssh root@172.16.111.110
root@172.16.111.110's password:
Last login: Tue Feb 27 10:34:42 2018 from 172.16.111.100

##查看远程创建的文件
[root@garytao-02 ~]# ls -l /tmp/12.txt
-rw-r--r-- 1 root root 5 2月  27 10:34 /tmp/12.txt

##查看远程脚本创建的文件内容
[root@garytao-02 ~]# cat /tmp/12.txt
1212
[root@garytao-02 ~]#

expect脚本传递参数

1.传递参数
[root@garytao-01 shell]# vi 3.expect

增加如下脚本内容:

#!/usr/bin/expect

set user [lindex $argv 0]   #把第一个参数的值赋给user
set host [lindex $argv 1]
set passwd "123456"
set cm [lindex $argv 2]
spawn ssh $user@$host

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
set timeout -1 #-1表示永远不超时,1表示1秒,2表示2秒....,表示执行命令几秒后停止
expect "]*"
send "exit\r"

[root@garytao-01 shell]# chmod a+x 3.expect
[root@garytao-01 shell]# ./3.expect root 172.16.111.110 ls
spawn ssh root@172.16.111.110
root@172.16.111.110's password:
Last login: Tue Feb 27 10:39:46 2018 from 172.16.111.100
[root@garytao-02 ~]# ls
123.txt   1_heard.txt  1.txt  aming       anaconda-ks.cfg.1  shell    zabbix-release-3.2-1.el7.noarch.rpm
123.txt~  1_sorft.txt  2.txt  aminglinux  rsync              yum.log

[root@garytao-02 ~]# [root@garytao-01 shell]# ./3.expect root 172.16.111.110 "ls;w;vmstat 1"
spawn ssh root@172.16.111.110
root@172.16.111.110's password:
Last login: Tue Feb 27 10:53:39 2018 from 172.16.111.100
[root@garytao-02 ~]# ls;w;vmstat 1
123.txt   1_heard.txt  1.txt  aming       anaconda-ks.cfg.1  shell    zabbix-release-3.2-1.el7.noarch.rpm
123.txt~  1_sorft.txt  2.txt  aminglinux  rsync              yum.log
10:56:21 up 6 days,  9:04,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    172.16.111.1     一18   16:31m  0.00s  0.00s -bash
root     pts/1    172.16.111.100   10:56    0.00s  0.01s  0.01s 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 204888    876 239148    0    0     0     1   46   14  0  0 100  0  0
0  0      0 204904    876 239164    0    0     0    21   55  116  0  0 100  0  0
0  0      0 204904    876 239164    0    0     0     0   55  113  0  0 100  0  0
0  0      0 204904    876 239164    0    0     0     0   56  112  0  0 100  0  0
0  0      0 204904    876 239164    0    0     0     0   56  111  0  0 100  0  0
0  0      0 204904    876 239164    0    0     0     0   58  110  0  0 100  0  0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  分发系统 expec t脚本
相关文章推荐