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 root@192.168.1.77

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 root@192.168.1.77

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 root@192.168.1.77

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.

root@192.168.1.77'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工具,本人的其它两篇博客(写本文的时候,另外两篇博客还未审核通过)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值