简洁的一键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
简洁的一键SSH脚本的更多相关文章
- CentOS系统优化一键配置脚本
#!/usr/bin/env bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export P ...
- etcd v3版本生产级集群搭建以及实现一键启动脚本
本专栏的上一篇文章写了<长篇图解etcd核心应用场景及编码实战>,本文继续.后续计划章节内容如下: <长篇图解etcd核心应用场景及编码实战> <搭建高可用etcd集群& ...
- CentOS 6、7下pptp vpn一键安装脚本
之前有折腾过<CentOS 6.7下IPSEC/L2TP VPN一键安装脚本>,不稳定.不支持IOS,因此换成pptp,并已经添加到<lnmp一键安装包>.这个脚本可以单独使用 ...
- centos一键优化脚本
centos一键优化脚本:细节:http://oldboy.blog.51cto.com/2561410/1336488网络状态优化:http://oldboy.blog.51cto.com/2561 ...
- LAMP最新源码一键安装脚本
Linux+Apache+MySQL+PHP (脚本可以选择是否安装+Pureftpd+User manager for PureFTPd+phpMyAdmin+memcache),添加虚拟主机请执行 ...
- LNMP、LAMP、LANMP一键安装脚本(定期更新)[转]
这个脚本是使用shell编写,为了快速在生产环境上部署LNMP/LAMP/LANMP(Linux.Nginx/Tengine.MySQL/MariaDB/Percona.PHP),适用于CentOS/ ...
- SSR服务端一键安装脚本
支持新协议混淆,SSR服务端一键安装脚本 Shadowsocks-R 是项目 shadowsocks 的增强版,用于方便地产生各种协议接口.实现为在原来的协议外套一层编码和解码接口,不但可以伪装成 ...
- CentOS 升级 Python3 (附带: 一键升级脚本)
升级环境 应用名称 版本 Python 3.5.2 Syatem CentOS 6.7 升级方法 [1]下载 Python 3: wget http://mirrors.s ...
- 源码编译安装Apache-附一键部署脚本
1.进入apache官网https://www.apache.org/,点击Download 2.如图选择 3.选择httpd 4.下载两个包,2.2为CentOS6使用,2.4为CentOS7使用 ...
随机推荐
- win10 创建安卓模拟器及启动的方法
一打开 安卓 studio 然后点击AVD manager 创建一个模拟器 二 通过命令行快速启动模拟器 D:\Android\sdk\tools\emulator.exe -netdelay non ...
- Windows下cwRsyncServer双机连续同步部署
下载cwRsyncServer服务器端与客户端的安装文件:服务端下载:cwRsyncServer_4.0.5_Installer.zip客户端下载:cwRsync_4.0.5_Installer.zi ...
- pclint在VS2013中的配置
1.安装pclint a. 从http://download.csdn.net/detail/finewind/8426979下载破解版的pclint9i版: b. 点击pclint9setuo.ex ...
- log4j输出日志到flume
现需要通过log4j将日志输出到flume,通过flume将日志写到文件或hdfs中 配置flume-config文件 将日志下沉至文件 a1.sources = r1 a1.sinks = k1 a ...
- 【Hadoop】如何形象描述大数据生态?
作者:千岁大王链接:https://www.zhihu.com/question/27974418/answer/39845635来源:知乎著作权归作者所有,转载请联系作者获得授权. Google内部 ...
- 速查笔记(Linux Shell编程<下>)
转载自: http://www.cnblogs.com/stephen-liu74/archive/2011/11/04/2228133.html 五.BASH SHELL编程: 1. 初始化顺 ...
- SharePoint 2013 表单认证使用ASP.Net配置工具加入用户
前 言 上面一篇博客,我们了解到怎样为SharePoint 2013配置表单身份认证.可是加入用户是一个麻烦事儿:事实上,我们还能够用Asp.Net的配置工具,为SharePoint 2013加入表单 ...
- 《Java程序猿面试笔试宝典》之volatile有什么作用
在由Java语言编写的程序中.有时候为了提高程序的执行效率,编译器会自己主动对其进行优化,把经常被訪问的变量缓存起来,程序在读取这个变量的时候有可能会直接从缓存(比如寄存器)中来读取这个值.而不会去内 ...
- RPC接口mock测试
转载:http://blog.csdn.net/ronghuanye/article/details/71124127 1 简介 Dubbo目前的应用已经越来越广泛.或者基于Dubbo二 ...
- d3.js封装文本实现自动换行和旋转平移等功能
我们下面话不多说,本文主要介绍的是利用D3.js封装文本实现自动换行功能的步骤,下面来一起看看吧. 一.引用 multext.js 文件 multext.js function appendMulti ...