当前位置: 首页 > news >正文

简洁的一键SSH脚本

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

相关文章:

  • Page-Enter、Page-Exit的使用
  • 很认真的聊一聊程序员的自我修养(转)
  • ERP系统各种单据流水号的产生方案
  • WebSocket在spring messagemapping下获取httpsession
  • 图片的动画 ease.js
  • [翻译].net 2.0(c#)下简单的FTP应用程序(转)
  • Python Unicode 转换 字符串
  • java中关于、、|、||之间的区别和运算
  • 生成静态页面的方法
  • 项目质量量化考核建议
  • Linux命令(磁盘的卸载与挂载)
  • 手势识别论文解读
  • 用于在弹出窗口中计算窗口位置的函数
  • 《领域特定语言》一2.2为何需要DSL
  • 关注数据中心NFV性能
  • 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE
  • 分享的文章《人生如棋》
  • 【Under-the-hood-ReactJS-Part0】React源码解读
  • 【个人向】《HTTP图解》阅后小结
  • ECMAScript 6 学习之路 ( 四 ) String 字符串扩展
  • EOS是什么
  • ERLANG 网工修炼笔记 ---- UDP
  • ES6之路之模块详解
  • Facebook AccountKit 接入的坑点
  • Javascript Math对象和Date对象常用方法详解
  • Otto开发初探——微服务依赖管理新利器
  • Spring Cloud(3) - 服务治理: Spring Cloud Eureka
  • Spring技术内幕笔记(2):Spring MVC 与 Web
  • 半理解系列--Promise的进化史
  • 从0到1:PostCSS 插件开发最佳实践
  • 从重复到重用
  • 数组的操作
  • 扩展资源服务器解决oauth2 性能瓶颈
  • #多叉树深度遍历_结合深度学习的视频编码方法--帧内预测
  • (C语言)深入理解指针2之野指针与传值与传址与assert断言
  • (js)循环条件满足时终止循环
  • (pojstep1.3.1)1017(构造法模拟)
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (算法)Game
  • (转)Google的Objective-C编码规范
  • (转载)跟我一起学习VIM - The Life Changing Editor
  • .NET和.COM和.CN域名区别
  • .NET教程 - 字符串 编码 正则表达式(String Encoding Regular Express)
  • .net开发引用程序集提示没有强名称的解决办法
  • .net连接oracle数据库
  • .NET委托:一个关于C#的睡前故事
  • .net之微信企业号开发(一) 所使用的环境与工具以及准备工作
  • /etc/apt/sources.list 和 /etc/apt/sources.list.d
  • @Responsebody与@RequestBody
  • @RestControllerAdvice异常统一处理类失效原因
  • [20180224]expdp query 写法问题.txt
  • [AS3]URLLoader+URLRequest+JPGEncoder实现BitmapData图片数据保存
  • [BROADCASTING]tensor的扩散机制
  • [C++]类和对象【下】