自动化运维,远程交互从服务器A上ssh到服务器B上,然后执行服务器B上的命令。

第一种:

ftp -v -n 192.168.0.1 21 <<!
user ftp ftp123
bay
!

第二种:

{
   echo -e "
"
   echo -e "ifconfig"| telnet 0 1234 

第三种:

ssh 192.168.0.2 '/etc/init.d/httpd start'

第一种方式最麻烦,比较喜欢第二种,第三种方式最简单,你觉得呢?

如果觉得好就收藏吧! 如果还有其他方式,请留言告诉我,谢谢。

第四种:

#!/usr/bin/expect -f
set port 22
set user root
set host 192.168.0.1
set password root
spawn ssh -D $port $user@$host
#root@192.168.0.1's password:  expect
"*assword:*" send "$password " spawn ifconfig interact expect eof
send:用于向进程发送字符串
expect:从进程接收字符串
spawn:启动新的进程
interact:允许用户交互

第四种交互方式怎么样?如何从服务器A上ssh到服务器B上,然后执行服务器B上的命令?听起来是不是很海派呢!
案例:(获取服务器Bifconfig保存文件,scp到服务器A)
#!/usr/bin/expect -f
set port 22
set user root
set host 192.168.1.2
set password root
set timeout -1
spawn ssh $user@$host
expect {
"*yes/no" { send "yes
"; exp_continue}
"*assword:" { send "$password
" }
}
expect "*#*"
send "ifconfig > /home/cfg 
"
send "exit
"
interact
spawn scp $host:/home/cfg ./
expect {
"*yes/no" { send "yes
"; exp_continue}
"*assword:" { send "$password
" }
}
expect eof

 第五种:

sshpass -p root ssh 192.168.48.73 "w"

但需要安装sshpass

下载:sshpass-1.05.tar.gz

百度去吧。

 
原文地址:https://www.cnblogs.com/Javame/p/4272440.html