这里发一个自己图省事搞的一个批量打通SSH的脚本,可能对于好多朋友也是实用的,是expect+python的一个组合实现,原理非常easy,
使用起来也不复杂,在此还是简单贴出来说说。

noscp.exp

  #!/usr/bin/expect

#noscp.exp

if {$argc<4} {
puts stderr "Usage: $argv0 localfile remotefile user passwd "
exit 1
} set localfile [ lindex $argv 0 ]
set remotefile [ lindex $argv 1 ]
set user [ lindex $argv 2 ]
set pwd [ lindex $argv 3 ] set timeout 30 spawn scp ${localfile} ${user}@${remotefile} expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$pwd\r" }
} expect eof

ssh_distribute.py

#!/usr/bin/python

import subprocess
import os file_dir='/home/hosts' with open(file_dir) as data:
for each_line in data.readlines():
if each_line != '':
(ip,passwd)=each_line.split(':',2)
print('./noscp.exp ~/.ssh/authorized_keys '+ip+':~/.ssh '+'root '+passwd.strip('\n'))
subprocess.Popen('./noscp.exp ~/.ssh/authorized_keys '+ip+':~/.ssh '+'root '+passwd.strip('\n'),shell=True)
# subprocess.Popen('./sshkey.exp '+ip+' root '+passwd+' \\| grep ssh-rsa >> ~/.ssh/authorized_keys',shell=True)
else:
pass
#subprocess.Popen('chmod 755 ~/.ssh/authorized_keys',shell=True)

ssh_setup.py

#!/usr/bin/python

import subprocess
import os file_dir='/home/hosts' with open(file_dir) as data:
for each_line in data.readlines():
if each_line != '':
(ip,passwd)=each_line.split(':',2)
print('./sshkey.exp '+ip+' root '+passwd.strip('\n')+' | grep ssh-rsa >> ~/.ssh/authorized_keys')
subprocess.Popen('./sshkey.exp '+ip+' root '+passwd.strip('\n')+' | grep ssh-rsa >> ~/.ssh/authorized_keys',shell=True)
# subprocess.Popen('./sshkey.exp '+ip+' root '+passwd+' \\| grep ssh-rsa >> ~/.ssh/authorized_keys',shell=True)
else:
pass
subprocess.Popen('chmod 755 ~/.ssh/authorized_keys',shell=True)
#subprocess.Popen('/home/ssh_distribute.py',shell=True)

sshkey.exp

#!/usr/bin/expect

#sshkey.exp

if {$argc<3} {
puts stderr "Usage: $argv0 host user passwd "
exit 1
} set host [ lindex $argv 0 ]
set user [ lindex $argv 1 ]
set pwd [ lindex $argv 2 ] set timeout 30 #spawn ssh ${user}@${host} "rm -rf ~/.ssh/id_rsa*"
#
#expect {
# "*yes/no" { send "yes\r"; exp_continue }
# "*password:" { send "$pwd\r"; exp_continue }
#} spawn ssh ${user}@${host} "ssh-keygen -t rsa" expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$pwd\r"; exp_continue }
"Enter file in which to save the key*" { send "\n\r"; exp_continue }
"Overwrite*" { send "y\n"; exp_continue }
"Enter passphrase (empty for no passphrase):" { send "\n\r"; exp_continue }
"Enter same passphrase again:" { send "\n\r" }
} spawn ssh ${user}@${host} "cat ~/.ssh/id_rsa.pub" expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$pwd\r" }
} expect eof
   多看两眼代码应该能够看出,expect的功能是能够等待一些Linux反馈 通过这个的反馈做出推断并能够分类进行兴许的动作,非常黄非常暴力。

也就是利用了这个原理。过程例如以下:
1.首先运行 ./ssh_setup.py 首先收集全部机器的公钥,然后定向到运行这个脚本的authorized_keys文件中边,自己主动赋予755权限。
2.运行./ssh_distribute.py 分发authorized_keys文件到全部的机器上。

下载连接在下方,详细用法里边有readme.txt

 http://download.csdn.net/detail/u012886375/9453810

简洁的一键SSH脚本的更多相关文章

  1. CentOS系统优化一键配置脚本

    #!/usr/bin/env bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export P ...

  2. etcd v3版本生产级集群搭建以及实现一键启动脚本

    本专栏的上一篇文章写了<长篇图解etcd核心应用场景及编码实战>,本文继续.后续计划章节内容如下: <长篇图解etcd核心应用场景及编码实战> <搭建高可用etcd集群& ...

  3. CentOS 6、7下pptp vpn一键安装脚本

    之前有折腾过<CentOS 6.7下IPSEC/L2TP VPN一键安装脚本>,不稳定.不支持IOS,因此换成pptp,并已经添加到<lnmp一键安装包>.这个脚本可以单独使用 ...

  4. centos一键优化脚本

    centos一键优化脚本:细节:http://oldboy.blog.51cto.com/2561410/1336488网络状态优化:http://oldboy.blog.51cto.com/2561 ...

  5. LAMP最新源码一键安装脚本

    Linux+Apache+MySQL+PHP (脚本可以选择是否安装+Pureftpd+User manager for PureFTPd+phpMyAdmin+memcache),添加虚拟主机请执行 ...

  6. LNMP、LAMP、LANMP一键安装脚本(定期更新)[转]

    这个脚本是使用shell编写,为了快速在生产环境上部署LNMP/LAMP/LANMP(Linux.Nginx/Tengine.MySQL/MariaDB/Percona.PHP),适用于CentOS/ ...

  7. SSR服务端一键安装脚本

    支持新协议混淆,SSR服务端一键安装脚本   Shadowsocks-R 是项目 shadowsocks 的增强版,用于方便地产生各种协议接口.实现为在原来的协议外套一层编码和解码接口,不但可以伪装成 ...

  8. CentOS 升级 Python3 (附带: 一键升级脚本)

      升级环境   应用名称 版本 Python 3.5.2 Syatem CentOS 6.7         升级方法   [1]下载 Python 3: wget http://mirrors.s ...

  9. 源码编译安装Apache-附一键部署脚本

    1.进入apache官网https://www.apache.org/,点击Download 2.如图选择 3.选择httpd 4.下载两个包,2.2为CentOS6使用,2.4为CentOS7使用 ...

随机推荐

  1. MathType requires a newer version of MT Extra等MathType问题的不兼容性解决方案

    常见问题解决方法: 1.MathType 6.0与office 2007兼容问题 由于Office软件安装时默认是不安装公式编辑器的,在安装完MathType 6.0之后,需要将\MathType 6 ...

  2. #if 条件编译

    1.格式: #if constant-expression statements #elif constant-expression statements #else statements #endi ...

  3. adb 设备不识别

    android真坑 有两台测试机 都能连能snapdragon profiling了 忽然 一台不能识别了 adb devices 就不存在 一台一直是好的 kill server start ser ...

  4. 解决Mac安装M2Crypto提示无法找到openssl头文件问题

    大概是这种问题 running build running build_py running build_ext building'M2Crypto.__m2crypto' extension swi ...

  5. 我与小娜(36):人机大战第五局,AlphaGo必胜!

    我与小娜(36):人机大战第五局,AlphaGo必胜!       小娜知道,细致阅读论文"Mastering the game of Go with deep neural network ...

  6. Gentoo:startx出现Failed to load module问题

    安装完xorg-server后,startx启动桌面环境,出现缺少模块错误. 查看log: cat /var/log/Xorg.0.log | grep EE [75.403] (EE) Failed ...

  7. 字典树-HDOJ-1247-Hat’s Words

    Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  8. JVM源码分析之FinalReference完全解读

    Java对象引用体系除了强引用之外,出于对性能.可扩展性等方面考虑还特地实现了4种其他引用:SoftReference.WeakReference.PhantomReference.FinalRefe ...

  9. react-native fetch 请求封装

    1.fetch 函数封装 fetch.js /** * 请求头 * @type {{Accept: string, Content-Type: string}} */ const header = { ...

  10. Docker镜像Export导出和Import导入

    在使用Docker时最头痛的无非无法获取仓库镜像,我们可以通过Export导出镜像备份,通过import导入镜像.导出镜像是通过容器进行导出,下面来看镜像对应的容器: root@default:~# ...