使用ssh client与bash scripts轻松管理多台主机
使用ssh client与bash scripts轻松管理多台主机
博客园团队的站内短消息,原来是因为编辑人员处理不当,而导致上一篇文章被错误地移出首页,且已将上一篇文章移回。
向博客园团队认真、负责的工作态度致敬!
由于同一篇文章在博客园首页上出现了两次,我已将上一篇文章移出首页。
SlaveServer.conf
1 #global var: glServerList 2 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 i 11 for i in $glServerList 12 do 13 echo "communicating via ssh with $i ... ..." 14 ssh -o GSSAPIAuthentication=no -p "$1" root@${i} "mkdir ~/.ssh ; echo ${clientRSApk} >> ~/.ssh/authorized_keys" 15 done 16 } 17 SSH_RSAPublicKeyBroadCast "$1"
第二个工具:
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 i 10 for i in $glServerList 11 do 12 echo "communicating via ssh with $i ... ..." 13 scp -o GSSAPIAuthentication=no -P "$1" "$2" root@${i}:${3} 14 done 15 } 16 17 SSH_FileBroadCast "$1" "$2" "$3"
第三个工具:
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 $glServerList 10 do 11 echo "communicating via ssh with $i ... ..." 12 ssh -o GSSAPIAuthentication=no -p "$1" root@${i} "${2}" 13 done 14 } 15 16 SSH_CommandBroadCast "$1" "$2"
第四个工具:
AllSlaveExecLocalScripts.sh
#!/bin/bash . SlaveServer.conf function 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 exec declare tempFile=`mktemp` #local tmp declare remoteTmpDir if 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 0 else rm -f $tempFile echo "connect error:exit" return 1 fi } function SSH_BroadCastExecLocalScripts () { # $1 : sshd port like : 3198 # $2 local bash scripts you want the remoteServer to exec declare i for i in $glServerList do echo "communicating via ssh with $i ... ..." SSH_RemoteServerExecLocalScripts "$i" "$1" "$2" done } SSH_BroadCastExecLocalScripts "$1" "$2"
附录:
关于实验环境的配置情况,请查看文章《构建一个完整的DNS系统》,这里不再赘述。
sh
1 #!/bin/bash
2
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
至此,我们的工具实验展示结束。如有问题或建议,欢迎讨论 :)
评论暂时关闭