Linux批量創建主機信任關係

Linux批量創建主機信任關係

1.實現環境:
本機redhat6.3 32位的操做系統
2.實現目的:
對其餘主機作信任,使本機登陸其餘主機不須要輸入密碼
3.實現過程:
 1)先在本機用ssh-keygen -t rsa生成公鑰,生成~/.ssh/id_rsa.pub公鑰文件
 2)安裝軟件包sshpass-1.04.tar.gz
   tar -zxvf sshpass-1.04.tar.gz
   cd sshpass-1.04
   ./configure
   make && make install
  3)把 StrictHostKeyChecking no加到/etc/ssh/ssh_config能夠讓ssh客戶端自動接受新主機的hostkey,不用每次在ssh新主機時都本身輸入yes 
  4)編寫hostlist文件,裏面列舉了主機名(ip地址)和主機密碼(主機名和密碼之間用空格隔開),一行內只有一對主機名和密碼 
  5)執行批量創建主機信任關係的腳本trusthost.sh
  
#!/bin/bash
#author:cq
#date:06-04-2013
hosts=`sed '/^#/d' ~/trusthost/hostlist | awk '{print $1}'`
id=`cat ~/.ssh/id_rsa.pub`
for host in $hosts
do 
   ping -c 2 -W 1 $host
   if [ $? = 0 ]
   then
      passwd=`awk /$host/'{print $2}' ~/trusthost/hostlist`
      sshpass -p "$passwd" ssh $host "echo $id >>~/.ssh/authorized_keys"
    else
      echo "$host is lost" >~/trusthost/ssh.log
    fi
done
相關文章
相關標籤/搜索