Linux ssh下执行ssh命令远程登录其他机器,总是需要输入密码,如果人工去登录,输入密码那还可以,但是让程序自动化登录远程ssh服务器,并执行命令着就比较麻烦了。

Linux下有个程序是expect,它可以模拟键盘,输入文本。

1 . 安装expect

机器上一般是没有这个命令的,需要安装expect

sudo apt-get install expect
yum install expect

经常看到一些脚本有会expect、spawn、send关键字,这几个关键字都是在expect程序里面使用的。

2 . 使用expect脚本运行

#!/usr/bin/expect
 
set timeout 5
 
password=ppp
 
spawn ssh user@localhost -p 22
expect {
    "(yes/no)" { send "yes\r"; exp_continue }
    "password:" { send "$password\r" }
}
expect user@*   {send \" ls -l \r\" }  ;
expect user@*  { send exit\r } ;
expect 100% ;
expect eof ;

3 . 使用命令行参数运行expect

有时候写个expect脚本比较麻烦,直接只用命令行参数去执行命令,或者嵌套在shell脚本、python脚本中,这样可以减少expect脚本的数量

一下是一个登录并执行一个ls命令的demo:

expect -c "
           set timeout 1;
           spawn ssh user@localhost -p 22  ;
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"password\r\" }
           } ;
           expect user@*   {send \" ls -l \r\" }  ;
           expect user@*  { send exit\r } ;
           expect eof ;
       "

expect的参数-c后面是字符串,里面就相当于脚本里面的内容了。

expect与scp使用

expect一般用于登录ssh服务器,除了ssh命令输入密码外,还有scp也是需要密码输入的,因此expect与scp结合起来使用的场景也是很多的。

expect -c "
           set timeout 1;
           spawn scp -P 22 user@remoteHost:/tmp/file.txt ~/  ;
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"password\r\" }
           } ;
           expect user@*   {send \" ls -l \r\" }  ;
           expect user@*  { send exit\r } ;
           expect 100% ;
           expect eof ;
       "

注意:scp的制定端口使用大写的-P。

用expect速度会比较慢,因为需要等待返回的数据,然后输入命令执行。没有ssh密钥登录的快速。