linux本机(192.168.99.11)登陆远程机器(192.168.99.77)shell脚本

实验:

linux本机(192.168.99.11)登陆远程机器(192.168.99.77);

执行如下操作:

1useradd yanes

2mkdir /opt/test

3) exit自动退出

#!/bin/bash

passwd='123456'

/usr/local/bin/expect <<-EOF

set time 30

spawn ssh -p22 [email protected]

expect {

"*yes/no" { send "yes\r"; exp_continue }

"*password:" { send "$passwd\r" }

}

expect "*#"

send "useradd yanes\r"

expect "*#"

send "mkdir /opt/test\r"

expect "*#"

send "exit\r"

interact

expect eof

EOF

执行结果:

[root@localhost ~]# vi test-ssh.sh

#!/bin/bash

passwd='123456'

/usr/local/bin/expect <<-EOF

set time 30

spawn ssh -p22 [email protected]

expect {

"*yes/no" { send "yes\r"; exp_continue }

"*password:" { send "$passwd\r" }

}

expect "*#"

send "useradd yanes\r"

expect "*#"

send "mkdir /opt/test\r"

expect "*#"

send "exit\r"

interact

expect eof

EOF

[root@localhost ~]# sh test-ssh.sh

spawn ssh -p22 [email protected]

The authenticity of host '192.168.1.77 (192.168.1.77)' can't be established.

RSA key fingerprint is 5a:b0:56:4d:8f:d7:81:ae:c7:fc:d1:3e:5f:e4:21:b8.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.1.77' (RSA) to the list of known hosts.

[email protected]'s password:

Last login: Wed Dec 21 23:24:42 2016

[root@oracle11g ~]# useradd yanes

useradd: user yanes exists

[root@oracle11g ~]# mkdir /opt/test

[root@oracle11g ~]# [root@localhost ~]#

 

 


实验结果成功;

 实验文档见网盘:

http://pan.baidu.com/s/1c7V0CI

如果需要安装expect工具和gcc工具,本人的其它两篇博客(写本文的时候,另外两篇博客还未审核通过)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章