Linux脚本实战之SSH执行远端脚本
Posted jks212454
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux脚本实战之SSH执行远端脚本相关的知识,希望对你有一定的参考价值。
一、系统环境
[root@tianyi ~]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core)
[root@tianyi ~]#
二、脚本要求
1.编写脚本名为ssh_connect.sh脚本
2.通过该脚本连接主机,在本端执行远程主机下的脚本
三、脚本内容
#!/bin/bash
##########################################################
#File Name:ssh_connect.sh
#Version:V1.0
#Created Time:2021-09-22 11:02:17
#Description:
##########################################################
ip_list=("192.168.200.135" "192.168.3.111")
user="root"
remote_commod="/root/ssh_connect.sh"
for ip in ${ip_list[*]}
do
if [ $ip = "192.168.200.135" ];then
echo "ssh connetc successful"
else
echo "failed!"
fi
ssh $user@$ip "/root/scripts/ipcheck.sh"
done
四、执行脚本
1.在本机执行效果
[root@control scripts]# ./ssh_connect.sh
ssh connetc successful
192.168.200.130 is Error!
192.168.200.131 is Error!
192.168.200.132 is Error!
192.168.200.133 is Error!
192.168.200.134 is Error!
192.168.200.135 is OK!
192.168.200.136 is Error!
192.168.200.137 is Error!
192.168.200.138 is Error!
192.168.200.139 is Error!
192.168.200.140 is Error!
failed!
ssh: connect to host 192.168.3.111 port 22: Connection refused
[root@control scripts]#
2.远程主机上执行脚本效果
[root@node1 scripts]# ./ipcheck.sh
192.168.200.130 is Error!
192.168.200.131 is Error!
192.168.200.132 is Error!
192.168.200.133 is Error!
192.168.200.134 is Error!
192.168.200.135 is OK!
192.168.200.136 is Error!
192.168.200.137 is Error!
192.168.200.138 is Error!
192.168.200.139 is Error!
192.168.200.140 is Error!
以上是关于Linux脚本实战之SSH执行远端脚本的主要内容,如果未能解决你的问题,请参考以下文章