ssh 免密码登录实现批量处理
搭建集群的时候ssh 免密码登录是一个问题以下脚本将实现批量处理
文件1主机名:host
17.19.18.11:123
17.19.18.12:123
文件2: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)
文件3: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)
文件4: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
文件5: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
步骤:
1.将此文件夹内文件拷贝到/home目录下
2.host内添加所有待处理的 ip:密码
3.安装expect 如果没有 yum install expect
4.执行./ssh_setup.py
5.执行./ssh_distribute.py
ssh 免密码登录实现批量处理的更多相关文章
- Shell脚本实现SSH免密登录及批量配置管理
本节索引 场景分析 ssh免密登录 pssh工具批量管理 SHELL自动化脚本 本篇总结 场景分析 作为一个运维工程师,不是每个人工作的环境都想阿里.腾讯那样,动不动就上亿的PV量,上万台服务器.我们 ...
- ssh免密码登录之分发密钥
ssh免密码登录之分发密钥 1.ssh免密码登录 密码登录和密钥登录有什么不同? 密码登录(口令登录),每次登录都需要发送密码(ssh) 密钥登录,分为公钥和私钥,公钥相当于锁,私钥相当于钥匙 1.1 ...
- ssh免密码登录机器(使用公钥和秘钥进行加密来实现)
ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 登录的原理: 有机器A(192.168.1.155),B(192.168.1. ...
- Mac下到Linux主机ssh免密码登录
最近忙得忘乎所以,写篇博客放松放松,RT,直接上命令好了 # Local ssh-keygen -t rsa scp ~/.ssh/id_rsa.pub username@server:~/.ssh/ ...
- 如何配置ssh免密码登录
[TOC] 如果你在管理一堆unix机器,每次登录都要输入密码是挺烦的事情,一方面为了安全我们一般不会将所有机器的密码都设置成一样,另一方面就算一样每次都输入一遍也很麻烦. 这种情况下我们一般是用ss ...
- linux 配置ssh免密码登录
1.确保主机名唯一 主机名修改方法: a.修改/etc/sysconfig/network,HOSTNAME=想要设置的主机名称 b.修改/etc/hosts,127.0.0.1 localhos ...
- centos ssh 免密码登录
最近在学习的过程中遇到这个问题: A主机和B主机: A 免密码登录B: 首先在A的 ~/.ssh 目录中 执行 ssh-keygen -t rsa 一路回车 最后生成连个文件: 将id_rsa.pub ...
- ssh免密码登录记录
做mha.hadoop安装过程中都要用ssh免密码登陆,查过一些资料,踩过很多坑,下面用简单记录一下 首先要安装ssh linux : centOS 6.5 yum -y install *ssh* ...
- Linux下SSH免密码登录
转自:http://haitao.iteye.com/blog/1744272 ssh配置 主机A:10.0.5.199 主机B:10.0.5.198 需要配置主机A无密码登录主机A,主机B 先确保所 ...
随机推荐
- python写入mysql
import pymysql conn = pymysql.connect(host='192.168.70.129',port=3306,user='root',passwd='123456', ...
- 20191114PHP文件操作
<meta charset="utf-8"><?php// $fn=fopen("c:\\abc.txt","w"); / ...
- MySQL语句优化方法(简单版)
基础回顾: sql语句是怎么样运行的? 一般来说,客户端发送sql语句到数据库服务器——数据库服务器进行运算并返回结果——客户端显示sql语句运行结果. 在本地运行时以workbench为例,客户端为 ...
- Sass--调用混合宏
在 Sass 中通过 @mixin 关键词声明了一个混合宏,那么在实际调用中,其匹配了一个关键词“@include”来调用声明好的混合宏.例如在你的样式中定义了一个圆角的混合宏“border-radi ...
- java定义类
package java04; /* * 定义一个类,用来模拟“学生”,其中包含量两个组合部分 * * 格式: * public class ClassName{ * //成员变量 * //成员方法 ...
- Matlab中利用null函数解齐次线性方程组
摘自:http://blog.csdn.net/masibuaa/article/details/8119032 有齐次线性方程AX=0,且rank(A)=r<n时,该方程有无穷多个解, 可以用 ...
- 【leetcode】981. Time Based Key-Value Store
题目如下: Create a timebased key-value store class TimeMap, that supports two operations. 1. set(string ...
- 在Eclipse中配置安卓的开发环境 (踩过的坑)
这个学期学校有门安卓程序设计课需要安装安卓开发环境. 一开始安装的是Andriod Studio,但是过程很坎坷很心酸,遇到各种各样的问题,最后还没有解决. 没办法决定用Eclipse配置安卓环境,虽 ...
- CSS text-indent 属性
text-indent 属性首行文本缩进,有点像padding-left的效果.
- 【Dart学习】--之Runes与Symbols相关方法总结
一,Runes 在Dart中,Runes代表字符串的UTF-32字符集, 另一种Strings Unicode为每一个字符.标点符号.表情符号等都定义了 一个唯一的数值 由于Dart字符串是UTF-1 ...