linux服务器批量加证书,通过shell脚本批量部署ssh私钥认证以及批量操作工具pssh的简单使用...

在日常工作中,批量管理服务器是个力气活,如果手工一台一台处理,效率低下。此时,老外写的pssh工具实现了批量管理,它是基于Python开发的,它的官方地址是:http://www.theether.org/pssh/ 。它的原理是先建立ssh私钥认证,然后用pssh工具批量管理。

测试环境:

192.168.8.188 ---->本地服务器

192.168.8.50  ---->远程服务器

192.168.8.220 ---->远程服务器

1.先在登陆机上生成公钥和私钥

[root@lnamp ~]# ssh-keygen -t rsa     #一路回车

如果不用脚本也如下一步一步操作:

把公钥id_rsa.pub拷贝到远程登录机上

[root@lnamp ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.50

21

root@192.168.1.101's password:

Now try logging into the machine, with "ssh 'root@192.168.8.50'", and check in:

.ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting.

现在你直接用:ssh 192.168.8.50   就不用输入密码了!

下面介绍用脚本批量部署

2.批量部署ssh私钥认证的脚本

首先要查看系统有没有expect这个工具,后面脚本中要用这个工具,没有的话yum安装就可以了!

[root@lnamp ~]# yum -y install expect

[root@lnamp ~]# cat /root/batch_sshkey.sh

#!/bin/bash

cd /root

cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys

foriin`cat ip.txt`

do

ip=$(echo "$i"|cut -f1 -d":")

password=$(echo "$i"|cut -f2 -d":")

expect -c "

spawn scp /root/.ssh/authorized_keys /root/remote_operate.sh  root@$ip:/tmp/

expect {

\"*yes/no*\" {send \"yes\r\"; exp_continue}

\"*password*\" {send \"$password\r\"; exp_continue}

\"*Password*\" {send \"$password\r\";}

}

"

expect -c "

spawn ssh root@$ip "/tmp/remote_operate.sh"

expect {

\"*yes/no*\" {send \"yes\r\"; exp_continue}

\"*password*\" {send \"$password\r\"; exp_continue}

\"*Password*\" {send \"$password\r\";}

}

"

done

[root@lnamp ~]# chmod +x /root/batch_sshkey.sh

3.建立被登录端IP列表

列表中服务器IP和密码用冒号":" 分割

[root@lnamp ~]# cat >> ip.txt << EOF

192.168.8.50:admin1

192.168.8.220:admin2

EOF

4.建立remote_operate.sh脚本

这个脚本的功能不说明了。脚本比较容易简单

[root@lnamp ~]# cat remote_operate.sh

#!/bin/bash

if[ ! -d /root/.ssh ];then

mkdir /root/.ssh

fi

cp /tmp/authorized_keys /root/.ssh/

[root@lnamp ~]# chmod +x remote_operate.sh

5.运行 batch_sshkey.sh 这个脚本

[root@lnamp ~]# ./batch_sshkey.sh

这样就达到了批量部署SSH私钥认证的目的!

下面开始介绍pssh这个工具如何执行批量操作!

6.pssh安装

[root@lnamp ~]# wget http://parallel-ssh.googlecode.com/files/pssh-2.3.tar.gz

[root@lnamp ~]# tar zxf pssh-2.3.tar.gz

[root@lnamp ~]# cd pssh-2.3

[root@lnamp ~]# python setup.py install

这样就安装好了!!

7.pssh安装后生成的相关命令

[root@lnamp bin]# ll

total 24

-rwxr-xr-x 1 10005 10000 2778 Jan 25 03:14 pnuke

-rwxr-xr-x 1 10005 10000 3917 Jan 25 03:14 prsync

-rwxr-xr-x 1 10005 10000 3238 Jan 25 03:14 pscp

-rwxr-xr-x 1 10005 10000 4030 Jan 25 03:14 pslurp

-rwxr-xr-x 1 10005 10000 3854 Jan 25 03:14 pssh

-rwxr-xr-x 1 10005 10000  270 May 24  2011 pssh-askpass

pssh使用可以查看帮助:

[root@lnamp ~]# pssh --help0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值