说明:通过ssh key完成企业批量分发管理操作
要求:4台Centos 6.5
客户端:A:192.168.1.103 B:192.168.1.104 C:192.168.1.105
服务端:192.168.1.101
具体实施步骤:
echo 123456|passwd --stdin shnne
su - shnne
total 12
-rw------- 1 shnne shnne 668 Jun 23 18:33 id_dsa #私钥
-rw-r--r-- 1 shnne shnne 607 Jun 23 18:33 id_dsa.pub #公钥
drwx------ 2 shnne shnne 4096 Jun 23 18:39 /home/shnne/.ssh/ #注意: .ssh目录权限一定是700
输入yes 和 密码 【密码为之前创建shnne的密码】
输入yes 和 密码 【密码为之前创建shnne的密码】
输入 yes 和 密码 【密码为之前创建shnne的密码】
5)分别验证连接到客户端shnne是否输入密码
[shnne@nfsserver ~]$ ssh -p22 shnne@192.168.1.103
注意事项:
total 4
-rw------- 1 shnne shnne 607 Jun 24 13:40 authorized_keys #为什么从服务器端靠过来的文件id_dsa.pub名称在B、C客户端被更改为authorized_kes,那是因为ssh客户端配置文件决定的grep authorized /etc/ssh/sshd_config
在执行下面的脚本的时候我们还需要各个客户端进行普通用户sudo授权
在远端服务器执行命令分发脚本
shnne@nfsserver ~]$ scp -P22 fenfa.sh shnne@192.168.1.103:~ #!/bin/bash . /etc/init.d/functions if [ ! $# -eq 2 ];then echo "usage $0 COMMAND DIRNAME" fi for i in `seq 101 105` do ssh -p22 -t $1 shnne@192.168.1.$i $1 $2 if [ $? -eq 0 ];then action "it's ok" /bin/true action "it's false" /bin/true done 执行脚本: [shnne@nfsserver ~]$ ssh -p22 -t shnne@192.168.1.103 sudo rsync 1.log / Connection to 192.168.1.103 closed. 在A服务器查看是否有1.log文件 [root@lamp01 shnne]# ll /1.log -rw-r--r-- 1 root root 0 Jun 24 22:05 /1.log
#服务端批量分发文件到客户端
[shnne@nfsserver ~]$ vim fenfa.sh #!/bin/sh . /etc/init.d/functions if [ $# -ne 1 ] then echo "USAGE:$0 {FILENAME|DIRNAME}" exit 1 fi for m in `seq 103 105` do scp -P22 -r $1shnne@192.168.1.$m:~ &>/dev/null if [ $? -eq 0 ] then action "fenfa $1 ok" /bin/true else action "fenfa $1 ok" /bin/false fi done
服务端批量分发文件到客户端并执行命令
#!/bin/sh . /etc/init.d/functions if [ $# -ne 2 ] then echo "USAGE:$0 COMMAND DIRNAME" exit 1 fi for i in `seq 103 105` do scp -P22 -rp $1 shnne@192.168.1.$i:~ >/dev/null 2>&1 ssh -p22 -t shnne@192.168.1.$i "sudo rsync $1" $2 if [ $? -eq 0 ];then action "rsync $1 to $2" /bin/true else action "rsync $1 to $2" /bin/false fi done
注意如果在执行上述脚本中出现下面的提示:
[shnne@nfsserver ~]$ ssh -p22 -t shnne@192.168.1.105 sudo rsync 1.log /
Address 192.168.1.105 maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
Connection to 192.168.1.105 closed.
此ssh报警连接问题是由于服务端,在hosts文件未添加对应的ip与主机名对应解析,添加完毕即不会出现此问题
shell脚本实现无密码交互的SSH自动登陆
首先安装yum install expect -y fenfa_sshkey.sh #!/bin/sh . /etc/init.d/functions for ip in 133 134 do #expect fenfa_sshkey.exp ~/.ssh/id_dsa.pub 192.168.1.$ip >/dev/null 2>&1 expect fenfa_sshkey.exp ~/.ssh/id_dsa.pub 192.168.1.$ip if [ $? -eq 0 ];then action "$ip" /bin/true else action "$ip" /bin/false fi done vim fenfa_sshkey.exp #!/usr/bin/expect if { $argc != 2 } { send_user "usage: expect fenfa_sshkey.exp file host\n" exit } #define var set file [lindex $argv 0] set host [lindex $argv 1] set password "123456" #spawn scp /etc/hosts root@192.168.1.101:/etc/hosts #spawn scp -P52113 $file shnne@$host:$dir #spawn ssh-copy-id -i $file "-p 52113 shnne$host" spawn ssh-copy-id -i $file "-p 22 shnne@$host" expect { "yes/no" {send "yes\r";exp_continue} "*password" {send "$password\r"} } expect eof exit -onexit { send_user "Successfully!\n" }