linux无交互登录服务器工具expect的使用

expect脚本远程登录命

linux系统默认没有安装expect的命令,使用yum安装
yum install -y expect
一般我们登录服务器会有一个机器指纹的保存确认,以及需要手动输入密码登录。但是当有
编写一个自动交互确认的登录脚本,可以实现不需要人为干预登录到其他服务器
使用expect语法登录到一台服务器当中,对可能出现的交互进行事前确认操作,如这里对机器验证指纹的保存选择“yes”
在expect每个执行动作后都必须有一个\r选项。这个是代表键盘回车键
interact参数表示执行完成这些操作后退出这个已经登录的远程终端

[root@localhost src]# vim 1.expect 
#!/usr/bin/expect
set host "192.168.1.200"
set passwd "123456"
spawn ssh xiangchen@$host
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r" }
}
interact

expect脚本执行远程命令

expect如果想要在被登录端的服务器上执行一些操作,那么事先必须使用expect自动登录到该台机器上,然后使用expect匹配输入端信息和输入的操作指令即可
exit\r   表示执行完成一些操作后立即退出该登录终端
这里演示使用的是本地主机登录本地终端,在实现效果上是一致的
expect脚本内容

#!/usr/bin/expect
set host "192.168.1.234"
set passwd "pwd@123"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r" }
}
expect "]*"
send "touch /tmp/12345.txt\r"
expect "]*"
send "echo 2233 > /tmp/12345.txt\r"
expect "]*"
send "exit\r"

expect脚本执行过程

[root@localhost src]# chmod a+x 2.expect 
[root@localhost src]# ./2.expect 
spawn ssh [email protected]
The authenticity of host '192.168.1.234 (192.168.1.234)' can't be established.
ECDSA key fingerprint is 90:7e:dc:6c:5f:b5:f2:2f:ce:ff:90:25:50:90:f5:e4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.234' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Last login: Mon Sep 24 10:55:01 2018 from 192.168.1.113
[root@localhost ~]# touch /tmp/12345.txt
[root@localhost ~]# echo 2233 > /tmp/12345.txt
[root@localhost ~]# [root@localhost src]# cd /tmp
[root@localhost tmp]# ls
123                                    12345.txt                                     
pear                                   zabbix_server_alerter.sock
php-fcgi.sock                          zabbix_server.log
zabbix_server.log.old                  zabbix_server_preprocessing.sock
[root@localhost tmp]# cat 12345.txt 
2233

expect脚本传递参数

为了实现脚本登录到多个服务器执行较多的指令,使用expect一般将要执行的参数进行传参引用,这样就实现了调用expect+ip、用户名及命令来批量执行某些操作
expect脚本中的expect eof和interact的属性是在脚本执行完成后会在登录终端中停留几秒等待时间。和exit不同,不会在执行完成后立即退出当前的登录终端

#!/usr/bin/expect

set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "pwd@123"
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"
#expect eof

expect脚本中需要注意的是在expect标签内的语句,条件语句和确认语句中间需要有空格的格式,如果这里没有存在空格,那么在执行该expect脚本时该条件确认不会执行,不会自动输入执行条件

expect脚本传参执行过程
将脚本给予可读权限,然后使用脚本+用户+主机IP+执行命令的方式来操作,如果在参数中需要执行多个命令,需要使用双引号将命令括起来,使用分号对多个命令进行分割

[root@localhost src]# ./3.expect root 192.168.1.234 "ls;df -h"
spawn ssh [email protected]
[email protected]'s password: 
Last login: Mon Sep 24 11:00:58 2018 from 192.168.1.234
[root@localhost ~]# ls;df -h
anaconda-ks.cfg dead.letter
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/cl-root 18G 8.8G 9.1G 50% /
devtmpfs 902M 0 902M 0% /dev
tmpfs 912M 0 912M 0% /dev/shm
tmpfs 912M 8.7M 904M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 197M 117M 81M 60% /boot
tmpfs 183M 0 183M 0% /run/user/0

猜你喜欢

转载自blog.51cto.com/8844414/2284381