这里发一个自己图省事搞的一个批量打通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

最新文章

  1. 用PS去除图片中文字的6个方法
  2. WebView---Android与js交互实例
  3. 在xilinxFPGA上使用microblaze及自写GPIO中断
  4. Android反编译(三)之重签名
  5. Spark集群 + Akka + Kafka + Scala 开发(2) : 开发一个Spark应用
  6. Timer的用法
  7. JavaScript学习笔记及知识点整理_3
  8. java生成解析xml的另外两种方法JAXB
  9. 前端之JavaScript第一天学习(3)-JavaScript输出
  10. Struts2文件下载
  11. jQuery Mobile 连接外部连接或切换动画
  12. C#当中的多线程_线程池
  13. C#邮件发送(最坑爹的邮箱-QQ邮箱)---转发(SmallFlyElephant)
  14. requestAnimationFrame动画方法
  15. 贝塞尔曲线:原理、自定义贝塞尔曲线View、使用!!!
  16. sed使用详解
  17. Autoconf/Automake工具简介
  18. zoj2818 Root of the Problem 简单数学 开方
  19. WEB框架-Django框架学习-关联管理器(RelatedManager)
  20. Alpha(2/10)

热门文章

  1. 【转】C++调用Matlab的.m文件
  2. Ubuntu16.04 -- 后台进程Nohup
  3. wireshark问题现象分析
  4. 2017.9.5 postgresql加密函数的使用
  5. Android 在同一个手机上安装多个同样的apk,便于调试
  6. Unity3D的三种坐标系
  7. Android学习(十) SQLite 基于内置函数的操作方式
  8. 【LeetCode】Sort Colors 数组排序
  9. 字符串截取 及 substr 和 substring 的区别
  10. python例子三