ssh key企业批量分发自动化管理

说明:通过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 

具体实施步骤:

基于密码的安全认证
1)四台服务器同时添加普通用户,并给密码
usradd shnne
id shnne  #检查一下

echo 123456|passwd --stdin shnne


2)在服务端切换用户名到shnne

su - shnne


3)在服务器端创建密钥
ssh-keygen -t dsa  一路回车
ll /home/shnne/.ssh/
[root@nfsserver shnne]# ll /home/shnne/.ssh/      #查看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   #公钥
[root@nfsserver shnne]# ll /home/shnne/.ssh/ -d

drwx------ 2 shnne shnne 4096 Jun 23 18:39 /home/shnne/.ssh/  #注意: .ssh目录权限一定是700


4)服务端分发公钥到客户端A、B、C
[root@nfsserver shnne]# ssh-copy-id -i .ssh/id_dsa.pub  shnne@192.168.1.103      #出现端口,则需要引号引号引用
[shnne@nfsserver ~]$ ssh-copy-id -i .ssh/id_dsa.pub shnne@192.168.1.103

输入yes  和 密码 【密码为之前创建shnne的密码】

[root@nfsserver shnne]# ssh-copy-id -i .ssh/id_dsa.pub  shnne@192.168.1.104     #出现端口,则需要引号引号引用
[shnne@nfsserver ~]$ ssh-copy-id -i .ssh/id_dsa.pub shnne@192.168.1.104

输入yes  和 密码 【密码为之前创建shnne的密码】

[root@nfsserver shnne]# ssh-copy-id -i .ssh/id_dsa.pub  shnne@192.168.1.105      #出现端口,则需要引号引号引用
[shnne@nfsserver ~]$ ssh-copy-id -i .ssh/id_dsa.pub shnne@192.168.1.105

输入 yes  和 密码 【密码为之前创建shnne的密码】


 5)分别验证连接到客户端shnne是否输入密码

[shnne@nfsserver ~]$ ssh -p22 shnne@192.168.1.103  


6)分别查看192.168.1.103服务器ip地址
ssh -p22 shnne@192.168.1.103  /sbin/ifconfig eth0

注意事项:

如果SSH修改成为特殊端口,如52113,那么上面的ssh-copy-id命令就无法进行分发公钥了。如果仍然要使用ssh-copy-id的话,那么可能的解决方法:
1.命令为:ssh-copy-id -i id_dsa.pub "-P 52113 shnne@192.168.1.103"    #特殊端口分发需要添加引号
2.vim /usr/bin/ssh-copy-id 在第41行做如下修改,加加粗部分
{ eval "$GET_ID" ; } | ssh -p52113 $1 "umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys && (test -x /sbin
    /restorecon && /sbin/restorecon ~/.ssh ~/.ssh/authorized_keys >/dev/null 2>&1 || true)" || exit 1
在分发服务器上执行ssh-copy-id脚本成功后,从B 192.168.1.103和 C 192.168.1.104上都可以看见从端靠过来的公钥(锁文件)如下:
[root@nfsserver ~]# ll /home/shnne/.ssh/
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"
}





分享到:
关键词:Linux运维

网友留言(0 条)

发表评论