ssh批量管理批量分發
1 SSH
SSH是安全的加密協議,用於遠端連線linux伺服器。
SSH預設埠是22,有SSH1(有漏洞)、SSH2 兩個版本。
SSH服務端主要包含兩個服務功能SSH遠端連線,SFTP服務。
Linux SSH客戶端包含SSH遠端連線命令,以及遠端拷貝SCP命令等。
1.1 ssh,scp,sftp
ssh小結:
1、切換到別的機器上 ssh -p52113 [email protected] ([[email protected] [command])
2、到其他機器執行命令(不會切到機器上),ssh -p52113 [email protected]
3、當第一次ssh連線的時候,本地會產生一個金鑰檔案 ~/.ssh/known_host
scp小結:
1、scp是加密的遠端拷貝,而cp僅為本地拷貝
2、可以把資料從一臺機器推送到另一臺,也可以從其他機器把資料拉回到本地執行命令的機器
3、每次都是全量完整拷貝,因此,效率不高,適合第一次拷貝用,如果需要增量拷貝,用rsync
sftp小結:
1、linux下連線命令 sftp -oPort=52113 [email protected]
2、上傳put加客戶端本地路徑 put /etc/hosts
也可以指定路徑上傳
3、下載get服務端的內容 get hosts,linux下載到本地連線前的目錄
也可以指定路徑下載, get hosts /etc/
4、連結到的遠端家目錄為預設目錄,也可以切換到其他有許可權的目錄下。
1.2 SSH重點小結
1、SSH為加密的遠端連線協議。相關軟體有openssh,openssl
2、預設埠22
3、協議版本1.x和2.x,2.x更安全,瞭解SSH協議原理
4、服務端SSH遠端連線服務,sftp服務。sshd守護程序,開機要自啟動
5、SSH客戶端包含ssh,scp,sftp命令
6、SSH安全驗證方式:口令和金鑰,這兩種都是基於口令的,理解SSH金鑰登入的原理
7、SSH服務安全優化,修改預設埠22,禁止root遠端連線,禁止DNS,SSH只監聽內網IP
8、SSH金鑰對,公鑰在伺服器端,鎖;私鑰在客戶端,鑰匙
1.3 命令例項
ssh -p52113 [email protected]
ssh -p52113 [email protected] /sbin/ifconfig eth0 連線到遠端執行命令,但是不連過去
.ssh/known_hosts裡面存放的是RSA金鑰資訊
scp -P52113 -r -p /data/ [email protected]:/tmp/ 拷貝到遠端
-r 遞迴 -p 保持屬性
sftp -oPort=52113 [email protected] 登入到遠端
put /data/test.txt /tmp/ 上傳到/tmp/
get test.txt /tmp/ 下載到/tmp/
1.4 批量管理批量分發叢集方案
1、批量分發
2、批量部署,執行命令
3、批量配置管理
建立使用者
useradd wangxin echo 123456|passwd --stdin wangxin
建立金鑰
ssh-keygen -t dsa [[email protected] ~]$ ls -l .ssh/ -rw------- 1 wangxin incahome 668 6月 11 16:11 id_dsa <========私鑰 -rw-r--r-- 1 wangxin incahome 608 6月 11 16:11 id_dsa.pub <======公鑰
將公鑰分發給其他主機
[[email protected] ~]$ ssh-copy-id -i .ssh/id_dsa.pub "-p 52113 [email protected]" The authenticity of host '[192.168.4.121]:52113 ([192.168.4.121]:52113)' can't be established. RSA key fingerprint is 4c:f0:cf:b1:6f:b4:bf:d6:62:7f:64:07:7a:10:fa:55. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '[192.168.4.121]:52113' (RSA) to the list of known hosts. [email protected]'s password: Now try logging into the machine, with "ssh '-p 52113 [email protected]'", and check in: .ssh/authorized_keys 《========公鑰分發後改名字了 to make sure we haven't added extra keys that you weren't expecting.
驗證公鑰有沒有分發成功
[[email protected] ~]$ ssh -p52113 [email protected] /sbin/ifconfig eth0
分發hosts
scp -P52113 hosts [email protected]:~ 分發到wangxin的家目錄
金鑰的許可權一定不能改變,若改變了就不能免密認證了
1.5 利用指令碼實現多臺機器分發
批量分發sshkey
[[email protected] ~]# cat /server/scripts/fenfa.sh #!/bin/sh . /etc/init.d/functions if [ $# -ne 1 ] then echo "USAGE:$0 {FILENAME|DIRNAME}" exit 1 fi for n in 121 122 123 do scp -P52113 –r $1 [email protected]$n:~ &>/dev/null if [ $? -eq 0 ] then action "fenfa $1 ok" /bin/true else action "fenfa $1 ok" /bin/false fi done
驗證分發是否成功
[[email protected] ~]# cat /server/scripts/view.sh #!/bin/sh if [ $# -ne 1 ] then echo "USAGE:$0 COMMAND" exit 1 fi for n in 121 122 123 do ssh -p52113 [email protected]$n $1 done
expect語言 解決非互動式的問題
[[email protected] ~]# yum install expect -y [[email protected] ~]# which expect /usr/bin/expect [[email protected] scripts]# expect fenfa_sshkey.exp
批量分發指令碼 exp
[[email protected] ~]# cat /server/scripts/fenfa_sshkey.exp #!/usr/bin/expect if { $argc != 2 } { send_user "usage: expect fenfa_sshkey.exp file host\n" exit } set file [lindex $argv 0] set host [lindex $argv 1] set password "123456" spawn ssh-copy-id -i $file "-p 9923 $host" expect { "yes/no" {send "yes\r";exp_continue} "*password" {send "$password\r"} } expect eof
批量分發指令碼 shell
[[email protected] ~]# cat /server/scripts/fenfa_sshkey.sh #!/bin/sh . /etc/init.d/functions
#如果沒有金鑰檔案自動建立
[ ! -s ~/.ssh/id_dsa.pub ] && ssh-keygen -t dsa –P ' ' -f ~/.ssh/id_dsa >/dev/null 2>&1 for ip in `cat /server/scripts/iplist.txt` do expect /server/scripts/fenfa_sshkey.exp ~/.ssh/id_dsa.pub $ip >/dev/null 2>&1 if [ $? -eq 0 ] then action "fenfa $ip" /bin/true else action "fenfa $ip" /bin/false fi done iplist [[email protected] ~]# cat /server/scripts/iplist.txt 192.168.4.185 192.168.4.186
1.6 郵件服務
[[email protected] ~]# /etc/init.d/postfix restart 關閉 postfix: [確定] 啟動 postfix: [確定] [[email protected] ~]# lsof -i :25 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME master 3157 root 12u IPv4 19418 0t0 TCP localhost:smtp (LISTEN) master 3157 root 13u IPv6 19420 0t0 TCP localhost:smtp (LISTEN) mail -s "wangxin title" [email protected] </etc/hosts mailq 檢視郵件佇列是否有郵件,是否傳送成功
傳送郵件提醒分發成功或者失敗
ssh -t [email protected]$n sudo rsync $1 $2 &>/dev/null if [ $? -eq 0 ] then echo "$1 to $2 0.$n is ok" >>/tmp/ok.log else echo "$1 to $2 0.$n is fail" >>/tmp/fail.log fi if [ -s /tmp/fail.log ] then mail -s "$(date +%F\ %T) fenfa hosts" [email protected] </tmp/fail.log >/tmp/ok.log >/tmp/fail.log fi