Use ssh client and bash scripts to easily manage multiple hosts

Source: Internet
Author: User

 

The insite Short Message of the blog site team was originally removed from the homepage by mistake due to improper handling by the editors, and the previous article has been moved back.

Pay tribute to the earnest and responsible work attitude of the blog community team!

Because the same article appears twice on the blog homepage, I have removed the previous article from the homepage. 

 

SlaveServer.conf
1 #global var: glServerList2 glServerList="192.168.31.98 \3               192.168.31.3  \4               192.168.31.4  \5               192.168.31.5  \6               192.168.31.6"
RSAPublicKeyBroadCast.sh
 1 #!/bin/bash 2  3 . SlaveServer.conf 4  5 function SSH_RSAPublicKeyBroadCast () { 6   # $1 : sshd port like : 3198 7   ssh-keygen 8   declare RSApk_path=/root/.ssh/id_rsa.pub 9   declare clientRSApk=`cat ${RSApk_path}`10   declare i11   for i in $glServerList12   do13     echo "communicating via ssh with $i ... ..."14     ssh -o GSSAPIAuthentication=no -p "$1" root@${i} "mkdir ~/.ssh ; echo ${clientRSApk} >> ~/.ssh/authorized_keys"15   done16 }17 SSH_RSAPublicKeyBroadCast "$1"

Second tool:

FileBroadCast.sh
 1  #!/bin/bash 2  3 . SlaveServer.conf 4  5 function SSH_FileBroadCast () { 6 # $1 : sshd port like : 3198 7 # $2 : src :local file path like /etc/ssh/sshd_config 8 # $3 : dest: remote server file path like /etc/ssh/sshd_config  9 declare i10 for i in $glServerList11 do12   echo "communicating via ssh with $i ... ..."13   scp -o GSSAPIAuthentication=no -P "$1" "$2" root@${i}:${3}  14 done15 }16 17 SSH_FileBroadCast "$1" "$2" "$3"

Third tool:

  

CommandBroadCast.sh
 1 #!/bin/bash 2   3 . SlaveServer.conf 4  5 function SSH_CommandBroadCast () { 6 # $1 : sshd port like : 3198 7 # $2 : command like ' service sshd reload ' 8 declare i 9 for i in $glServerList10 do11   echo "communicating via ssh with $i ... ..."12   ssh -o GSSAPIAuthentication=no -p "$1" root@${i} "${2}"13 done14 }15 16 SSH_CommandBroadCast "$1" "$2"

Fourth tool:

AllSlaveExecLocalScripts.sh
#!/bin/bash. SlaveServer.conffunction SSH_RemoteServerExecLocalScripts () {# $1 remoteServerArgs like: root@192.168.31.2# $2 remoteServer SSH Daemon's port like: 3198# $3 local bash scripts you want the remoteServer to execdeclare tempFile=`mktemp` #local tmp declare remoteTmpDirif ssh -o GSSAPIAuthentication=no -p "$2" "$1" 'declare tempDir=`mktemp -d` ; chmod 700 $tempDir ; chown root:root $tempDir ; cd $tempDir ; unset tempDir ; pwd ' 1> $tempFile then  remoteTmpDir=`tail -1 $tempFile`  scp -o GSSAPIAuthentication=no -P "$2" "$3" ${1}:$remoteTmpDir 1>/dev/null  ssh -o GSSAPIAuthentication=no -p "$2" "$1" " bash ${remoteTmpDir}/* ; rm -fr ${remoteTmpDir} "  rm -f $tempFile  return 0else  rm -f $tempFile  echo "connect error:exit"  return 1fi}function SSH_BroadCastExecLocalScripts () {# $1 : sshd port like : 3198# $2 local bash scripts you want the remoteServer to execdeclare ifor i in $glServerListdo  echo "communicating via ssh with $i ... ..."  SSH_RemoteServerExecLocalScripts "$i" "$1" "$2"done  }SSH_BroadCastExecLocalScripts "$1" "$2"

 

Appendix:

For details about the configuration of the experiment environment, refer to the article "Building a complete DNS system.

Sh

1 #!/bin/bash2 3 declare host_ip=`ifconfig | grep -Eo 'inet addr:\<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\>' | grep -Eo '\<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\>' | head -1`4 5 echo "host ip is : $host_ip"6 7 mpstat


So far, our tool experiment has ended. If you have any questions or suggestions, please discuss them :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.