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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分发系统介绍expect脚本远程执行命令expect脚本远程传递参数expect脚本传递参数相关的知识,希望对你有一定的参考价值。

分发系统介绍

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

expect脚本远程执行命令

1.安装expect
[[email protected] mon]# yum install -y expect
2.自动远程登录
[[email protected] shell]# vi 1.expect

增加以下脚本内容:

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

[[email protected] shell]# chmod a+x 1.expect 
[[email protected] shell]# ./1.expect 
spawn ssh [email protected]
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.
[email protected]‘s password: 
Last login: Mon Feb 26 18:22:32 2018 from 172.16.111.100
[[email protected] ~]# 登出
Connection to 172.16.111.110 closed.
[[email protected] shell]# 

expect脚本运程传递参数

1.自动远程登录后,执行命令并退出
[[email protected] shell]# vi 2.expect

增加脚本如下内容:

#!/usr/bin/expect
set user "root"
set passwd 123456"
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"

## 执行脚本
[[email protected] shell]# ./2.expect 
spawn ssh [email protected]
[email protected]‘s password: 
Last login: Tue Feb 27 10:23:28 2018 from 172.16.111.100
[[email protected] ~]# touch /tmp/12.txt
[[email protected] ~]# echo 1212 > /tmp/12.txt

## 回车退出
[[email protected] ~]# [[email protected] shell]# 
[[email protected] shell]# 

##重新执行自动登录脚本
[[email protected] shell]# ./1.expect 
spawn ssh [email protected]
[email protected]‘s password: 
Last login: Tue Feb 27 10:34:42 2018 from 172.16.111.100

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

##查看远程脚本创建的文件内容
[[email protected] ~]# cat /tmp/12.txt 
1212
[[email protected] ~]# 

expect脚本传递参数

1.传递参数
[[email protected] 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 [email protected]$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"

[[email protected] shell]# chmod a+x 3.expect 
[[email protected] shell]# ./3.expect root 172.16.111.110 ls
spawn ssh [email protected]
[email protected]‘s password: 
Last login: Tue Feb 27 10:39:46 2018 from 172.16.111.100
[[email protected] ~]# 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

[[email protected] ~]# [[email protected] shell]# ./3.expect root 172.16.111.110 "ls;w;vmstat 1"
spawn ssh [email protected]
[email protected]‘s password: 
Last login: Tue Feb 27 10:53:39 2018 from 172.16.111.100
[[email protected] ~]# 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             [email protected]   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

以上是关于分发系统介绍expect脚本远程执行命令expect脚本远程传递参数expect脚本传递参数的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

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

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

20.27分发系统介绍;20.28expect脚本远程登录;20.29expect脚本远程执行命令;20.30expect脚本传递参数