linux使用expect远程脚本登录

1.更新本机的tcl

yum -y list tcl
yum -y install tcl

2.安装expec

1 rpm -qa|grep expec
2 yum -y list expec
3 yum -y install expec


3.远程登录代码如下:

#!/usr/bin/expect -f
  
set port 22
set user root
set host 192.168.1.2
set password root
set timeout -1
  
spawn ssh -D $port $user@$host
expect "*password:*"

send "$password\r"
expect eof


4.远程执行命令代码并单行命令如下:

#!/usr/bin/expect -f

set port 22
set user root
set host 192.168.1.2
set password root
set timeout -1
   
spawn ssh -D $port $user@$host "echo '15 8-10 * * *  xx' >> home"

expect "*password:*"
send "$password\r"
expect eof

5.修改文件的权限

chmod 755 wenjian.sh

6.执行脚本出现bin/bash: bad interpreter: No such file or directory

ssh  -o StrictHostKeyChecking=no  root@192.168.0.xxx

7.再次执行脚本就OK了

 

posted @ 2016-03-16 17:45  园中菜鸟  阅读(352)  评论(0)    收藏  举报