使用ssh client與bash scripts輕松管理多台主機


 

  當我們需要控制一個局域網中的很多台服務器時,一個簡單的全局操作可能會被放大地異常繁瑣,這時我們就會需要新的工具來快速完成這種工作。

 我們將使用ssh客戶端提供的一些工具來快速完成這一開發工作,我們的開發平台是GNU/Linux。

 下面即是我們整個系統的鳥瞰圖:

第一個工具:

   要建立非交互式的ssh連接,采用客戶端公鑰認證登錄的方式是一種很棒的方法。

  第一個工具的功能是,將本地ssh客戶端的公鑰追加到各SlaveServers的/root/.ssh/authorized_keys文件中,以實現ssh公鑰登錄的認證。

  具體實現如下:

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"

  其使用、驗證請看附錄。

第二個工具:

  當我們需要將一個本地文件傳送到所有SlaveServer的一個相同路徑時,我們便需要一個工具來完成這樣的任務。

 例如:當我們想統一調整所有SlaveServer的sshd服務的參數時,我們需要改變所有SlaveServer上的/etc/ssh/sshd_config文件,這時第二個工具將發揮它的威力。

 具體實現如下:

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"

  其使用、驗證請看附錄。 

第三個工具:

  在完成第二個工具后,我們有了向所有SlaveServer傳送文件的能力,但是,如何向其所有發送"service sshd reload"命令呢?

  於是,我們的第三個工具,CommandBroadCast入場。

  具體實現如下:

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"

  其使用、驗證請看附錄。 

第四個工具:

  第三個工具可以讓我們控制所有的SlaveServer執行我們指定的一段命令,但是,如果我們想讓它們執行本地的一個bash腳本呢?

 如此,第四個工具的功能是命令所有SlaveServer執行我們在本地定義的一個bash腳本文件。

 具體實現如下:

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系統》,這里不再贅述。

  1.控制所有SlaveServer對192.168.31.2主機進行ssh公鑰登錄認證:

  2.由於SlaveServer們的sshd參數設置問題,導致ssh登錄連接的建立“異常”緩慢,所以我們需要重新調整所有SlaveServer的sshd配置文件:

  3.向所有SlaveServer發送'service sshd reload'命令:

  4.命令所有SlaveServer執行本地主機192.168.31.2上的一個腳本:

ip_host.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


  至此,我們的工具實驗展示結束。如有問題或建議,歡迎討論 :)       

  

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM