免密码登录服务器python脚本
在自动化运维平台没有做完之前,常需要登录服务器做很多维护操作,每次找好长好长的密码,那么多服务器,你会疯掉的,所以瞎搞了以下脚本.先解一下燃眉之急,哈哈
cat login_root.exp
#!/usr/bin/expect -c
set IP [lindex $argv 0]
set PWD [lindex $argv 1]
set timeout 2
spawn ssh root@$IP
expect "*yes/no*" {send "yes\r"}
expect "*assword*" {send "$PWD\r";interact}
用下面脚本调用 /opt/.script/login_root.exp 脚本,登录你需要登录的服务器
cat login_remote.py
#!/usr/bin/env python
#author:wangqiankun@lashou-inc.com
#name:login_remote.py
import re,time
import sys
import commands
import subprocess
def login_main(ip,pwd):
if ip != "" and pwd != "":
print "\033[32;1m start login at:%s\033[0m" %time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
p = subprocess.call("expect /opt/.script/troot.exp %s '%s'" %(ip,pwd),shell=True)
else:
print "\033[32;1m IP or the password is wrong, please handle \033[0m"
def re_ip(ipinfo):
with open('/root/.script/password.txt','r') as f:
for ipline in f.readlines():
hostinfo = re.match(r'^192.168.%s .*' %ipinfo,ipline,re.M|re.I)
if hostinfo:
print "\033[32;1m You are going to the login server IP is:%s\033[0m" %hostinfo.group().split()[0]
return hostinfo.group().split()[0],hostinfo.group().split()[1]
if __name__ == "__main__":
try:
ipinfo = sys.argv[1].strip()
if ipinfo != "":
hostinfo = re_ip(ipinfo)
try:
login_main(hostinfo[0],hostinfo[1])
except TypeError:
print "\033[31;1mThere is no matching to the correct IP address \033[0m"
except IndexError:
print "\033[31;1m You do not need to log in to the IP information input\033[0m"
#print ipinfo,
测试:
python login_remote.py 10.100
免密码登录服务器python脚本的更多相关文章
- ssh 免密码登录服务器
本机生成 ssh key ssh-keygen -t rsa -C "your_email@example.com" 上传公钥文件(假设用户为 user,服务器 ip 为 1.2. ...
- [转] ssh免密码登录服务器
1. tencent 同事写的比较靠谱 https://www.chenyudong.com/archives/ssh-using-private-public-key-no-password.htm ...
- jenkins发布项目到远程主机上,配置linux使用SSH免密码登录
一.首先要配置两台linux如何使用SSH免密码登录,这样脚本执行scp命令以及远程执行脚本都不需要输入密码: A为本地主机(即用于控制其他主机的机器,jenkins服务器) ; B为远程主机(即被控 ...
- Linux 配置ssh 免密码登录
在平常应用中,我们经常会登录到其他主机,比如说服务器,每次都需要用户名和密码. 我们可以通过ssh免密码登录服务器而不需要输入密码. 现在有一台ubuntu的阿里云服务器,称之为 server. 公 ...
- CentOS 配置集群机器之间SSH免密码登录
SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定:SSH 为建立在应用层和传输层基础上的安全协议.SSH 是目前较可靠,专为远 ...
- python实现免密码登录lunx服务器
import paramikoimport oshostname='192.168.76.10'username='root'# password='123456'ssh=paramiko.SSHCl ...
- Linux多台服务器间SSH免密码登录配置
SSH实现各个服务器间的文件相互备份,如运行scp命令,可以实现免密码登录,从而可以使用SHELL脚本实现一些自动化的处理. 假如A机要免密码登录B机,具体方法如下: 1.在A机运行:"ss ...
- 多台CentOS服务器下实现SSH免密码登录
ROOT用户下实现SSH免密码登录 第一步:进入目录/root/.ssh $ cd /root/.ssh/ 执行以下命令,会在当前目录下生成公钥(id_rsa.pub)/私钥(id_rsa)对 第二 ...
- Centos 解决SSH 免密码登录 以及Crontab制作定时SSH自动登录和关闭的脚本
一.SSH免密码登录 假设要登录的机器为192.168.1.100,当前登录的机器为192.168.1.101. 首先在101的机器上生成密钥(如果已经生成可以跳过): $ ssh-keygen -t ...
随机推荐
- erlang访问https地址
参考How do I do an HTTPS request with Erlang? 1> application:start(inets). ok 2> application:sta ...
- 如何给一块新硬盘安装grub,让它成为一个只有一个内核的系统
(1)先关机 (2)添加一块硬盘 (3)将硬盘分区,/dev/sdb1为boot分区,/dev/sdb2为swap分区, /dev/sdb3为根分区 (4)调整/dev/sdb2的分区类型为82,指定 ...
- PAT 甲级 1077 Kuchiguse
https://pintia.cn/problem-sets/994805342720868352/problems/994805390896644096 The Japanese language ...
- Jmeter 快速入门--简单的http压测
1.添加线程组 打开jmeter主窗口后,选择左侧树形结构里的"测试计划",然后右键选择添加,选择"threads(users)",选择"线程组&qu ...
- 微信小程序组件 加减号弹出框
<!-- 点击立即抢拼弹出框 --> <view class='add-rob' bindtap="setModalStatus" data-status=&qu ...
- 第203天:js---Array对象常用方法
1.shift:删除原数组的第一项,返回删除元素的值:如果数组为空则返回undefined //shift:删除原数组的第一项,返回删除元素的值:如果数组为空则返回undefined var arr ...
- Qt——数据的隐式共享
一.隐式共享类 在Qt中有很多隐式共享类( Implicitly Shared Classes ),什么是隐式共享呢,请参考官方文档的说明. 好吧,翻译一下—— 许多C++类隐式地共享数据,使得资源使 ...
- 【明哥报错簿】之【解决eclipse项目小红叉】
解决方案: 0.如果是jdk版本不一致,直接右击项目名称,选择maven里面的update project.原因一般是maven的pom.xml里面设置的编译插件org.apache.maven.pl ...
- 洛谷 P3376 【模板】网络最大流
题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行 ...
- BIOS和CMOS的区别
原文链接:https://www.cnblogs.com/boltkiller/articles/5732424.html 在日常操作和维护计算机的过程中,常常可以听到有关BIOS设置和CMOS设置的 ...