通过shell脚本批量部署ssh私钥认证以及批量操作工具pssh的简单使用
2014-04-02 09:25
1101 查看
在日常工作中,批量管理服务器是个力气活,如果手工一台一台处理,效率低下。此时,老外写的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
for i in `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 --help
转至:http://www.linuxidc.com/Linux/2012-06/62662.htm
测试环境:
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
for i in `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 --help
转至:http://www.linuxidc.com/Linux/2012-06/62662.htm
相关文章推荐
- 批量部署ssh私钥认证以及pssh工具使用方法
- 批量部署ssh私钥认证以及pssh工具使用方法
- 批量部署ssh私钥认证以及pssh工具使用方法
- 批量部署ssh私钥认证以及pssh工具使用方法
- 通过ssh主机互信打通内、外网,使用shell脚本自动部署测试环境
- Hadoop-Linux集群批量部署管理工具parallel-ssh(PSSH)的安装与使用
- ssh、scp的使用,以及shell脚本解决scp需要输入密码的问题
- cygwin安装教程以及使用notepad++编写shell脚本,并通过cygwin运行
- 菜鸟学习历程【2】vim的简单操作、GCC工具链、make和Makefile、GDB调试工具、Shell使用基础
- 批量部署ssh私钥认证
- 简单的通过shell脚本接收参数进行数据库操作
- 使用python脚本利用SSH协议通过TFTP备份批量备份山石防火墙配置
- 定时编译提交工具(shell脚本)以及linux cron的使用
- shell脚本批量部署ssh
- Python之Fabric模块 Fabric是基于Python实现的SSH命令行工具,简化了SSH的应用程序部署及系统管理任务,它提供了系统基础的操作组件,可以实现本地或远程shell命令,包括:
- 使用shell和expect一键批量分发SSH密钥脚本
- 【C语言】没事可以试试这个小程序,使用文件操作,模拟实现一个简单的文件拷贝工具!
- linux服务器批量部署应用系统shell脚本(Tomcat/jetty)
- Shell中使用plink工具实现远程批量关机