python实现跳板机
公司有1000多台服务器,线上机器都是禁止root登录的,所以平时是用普通用户登录,然后在su到root,密码都是在excel表中存的,这样登录一台机器,输两次命令,搜两次密码,实在很麻烦,有一天备份6台机器上的redis,登录就把我登录烦了,于是就用python来实现登录操作,密码表存到mysql中,用python的pexpect来实现交互。
pexpect的用法看http://www.ibm.com/developerworks/cn/linux/l-cn-pexpect1/
最后用 ./passwdbox.py ip即可自动登录
#!/usr/local/bin/python
# coding: utf-8 ##导入模块
import os
import sys
import pexpect
import MySQLdb
import struct
import fcntl
import termios
import signal ##传入的参数
opt = sys.argv ##如果没跟参数,就提示
if len(opt) == 1:
print '''
----------------------------
'Useage: ./zssh.py ServerIP'
----------------------------
'''
sys.exit(2) ##下面两个函数更改pexpect模拟的窗口大小,
##参见http://guweigang.com/blog/2012/10/25/using-python-ssh-landing-module-performs-pexpect/
def sigwinch_passthrough (sig, data):
winsize = getwinsize()
global foo
foo.setwinsize(winsize[0],winsize[1]) def getwinsize():
if 'TIOCGWINSZ' in dir(termios):
TIOCGWINSZ = termios.TIOCGWINSZ
else:
TIOCGWINSZ = 1074295912L # Assume
s = struct.pack('HHHH', 0, 0, 0, 0)
x = fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, s)
return struct.unpack('HHHH', x)[0:2] ##传入的ip
ip = opt[1]
##用MySQLdb驱动连接mysql
conn = MySQLdb.connect(host='localhost', user='root', passwd='Te62S#^t', db='sa')
cursor = conn.cursor()
##查找该ip的普通用户名,密码,还有root的密码,用来ssh连接
cursor.execute('select muser,mpass,rpass from password where ip=%s', ip)
result = cursor.fetchall() ##如果没在数据库中发现该ip信息,提示用户输入,并保存,如果发现就准备连接
if len(result) == 0:
muser = raw_input('输入用户名:')
mpass = raw_input('输入用户密码: ')
rpass = raw_input('输入root密码: ')
cursor.execute('insert into password values (%s,%s,%s,%s)', (ip, muser, mpass, rpass))
conn.commit()
elif len(result) == 1:
muser = result[0][0]
mpass = result[0][1]
rpass = result[0][2] ##用pexpect模块的spawn类,连接ssh
foo = pexpect.spawn('ssh %s@%s' % (muser,ip))
while True:
##期望得到列表里的东西
index = foo.expect(['continue', 'assword', pexpect.EOF, pexpect.TIMEOUT],timeout=10)
##如果得到的是continue,也就是第一次连接输入yes/no那,那就发送yes
if index == 0:
foo.sendline('yes')
continue
##如果是提示输入password,那就发送密码
elif index == 1:
foo.sendline(mpass)
##发送密码后有两种情况,登录成功或密码错误
index2 = foo.expect(['password', ']\$'])
##如果得密码正确
if index2 == 1:
print '%s 登录成功' % muser
break
##如果密码错误,提示输入密码
elif index2 == 0:
while True:
muser = raw_input('输入用户名:')
mpass = raw_input('用户密码不对,重新输入: ')
foo.sendline(mpass)
index3 = foo.expect([']\$', 'assword'], timeout=5)
##如果密码对了,就保存到数据库
if index3 == 0:
cursor.execute('update sys_pass set muser=%s, mpass=%s where ip=%s ', (muser, mpass, ip))
conn.commit()
foo.sendline('')
break
##如果不对,再循环一次
else:
continue
else:
print '连接超时'
break ##下面su 到root与上面类似
while True:
foo.expect('$')
foo.sendline('su - root')
#index4 = foo.expect(['口令', '密码', 'assword', pexpect.TIMEOUT, pexpect.EOF],timeout=5)
foo.sendline(rpass)
index5 = foo.expect([']#', 'monitor', pexpect.EOF, pexpect.TIMEOUT], timeout=5)
if index5 == 0:
print 'root 登录成功'
foo.sendline('')
break
elif index5 == 1:
while True:
rpass = raw_input('root密码不对,请输入: ')
foo.expect('$')
foo.sendline('su - root')
#index6 = foo.expect(['口令', '密码', 'assword', pexpect.TIMEOUT, pexpect.EOF],timeout=5)
foo.sendline(rpass)
index7 = foo.expect([']#', 'monitor', pexpect.EOF, pexpect.TIMEOUT], timeout=5)
if index7 == 0:
cursor.execute('update sys_pass set rpass=%s where ip=%s', (rpass, ip))
conn.commit()
print 'root 登录成功'
break
elif index7 == 1:
continue
else:
print 'error'
else:
print 'error' ##这个是利用那两个函数来调节子线程窗口大小
signal.signal(signal.SIGWINCH, sigwinch_passthrough)
size = getwinsize()
foo.setwinsize(size[0], size[1])
##进入interact交互模式
foo.interact()
pass
数据库建立
create database sa;
create table password (ip varchar(15), muser varchar(15), mpass varchar(30), rpass varchar(30));
将密码表的中的ip,普通用户名,密码,root密码插入库中我用的是一个脚本
#!/usr/local/bin/python
import MySQLdb conn = MySQLdb.connect(host='localhost', user='root', passwd='Te62S#^t', db='sa')
cursor = conn.cursor() f = open('passwd.txt')
num = 0
for i in f:
ilist = i.split()
if len(ilist) == 4:
ip = ilist[0]
muser = ilist[1]
mpass = ilist[2]
rpass = ilist[3]
try:
cursor.execute('insert into password values (%s,%s,%s,%s)', (ip, muser, mpass, rpass))
num += 1
except:
pass print num conn.commit()
cursor.close()
conn.commit()
将密码保存到passwd.txt格式类似
IP普通用户名密码root密码
202.106.0.20monitorasdf123Sfadf(adfasdfasdf
202.106.0.21zhswredhathelloworld
python实现跳板机的更多相关文章
- Python:webshell 跳板机审计服务器
1.修改paramiko源码包实现 https://github.com/paramiko/paramiko/tree/1.10.1 下载源码包 unzip paramiko-1.10.1.zip p ...
- Python通过跳板机链接MySQL的一种方法
- 【python】-- paramiko、跳板机(堡垒机)
paramiko Python的paramiko模块,该模块用于连接远程服务器并执行相关命令,常用于作批量管理使用 一.下载: pip3 install paramiko 源码:查看 二.parami ...
- 运维堡垒机(跳板机)系统 python
相信各位对堡垒机(跳板机)不陌生,为了保证服务器安全,前面加个堡垒机,所有ssh连接都通过堡垒机来完成,堡垒机也需要有 身份认证,授权,访问控制,审计等功能,笔者用Python基本实现了上述功能. A ...
- CentOS7 开源跳板机(堡垒机) Jumpserver
开源跳板机(堡垒机)Jumpserver 环境 CentOS 7 x64 关闭 selinux firewalld jumpserver: 172.24.0.14 testserve ...
- 那就用pthon来写个跳板机吧
1.需求 程序一: 1.后台管理 - 堡垒机上创建用户和密码(堡垒机root封装的类,UserProfile表) - .bashrc /usr/bin/python3 /data/bastion.py ...
- Shell跳板机sshstack
笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 源码地址: https://github.com/sshstack/sshstack 为什么要写shell跳板机? ...
- python之堡垒机(第九天)
本节作业: 通过使用paramiko和sqlalchemy实现堡垒机功能 主要功能实现: 1.用户登录堡垒机后,无需知道密码或密钥可以SSH登录远端服务器: 2.用户对一个组内所有主机批量执行指定命令 ...
- Jump跳板机的搭建和部署
Jump跳板机简绍 概括 Jumpserver 是一款由python编写开源的跳板机(堡垒机)系统,实现了跳板机应有的功能.基于ssh协议来管理,客户端无需安装agent. Jumpserver ...
随机推荐
- 个人翻译的cedec2010基于物理的光照
作为自己介绍基于物理渲染计划的一部分,在自己总结和发布的同时,也会翻译一些国外的优秀资料做推广 本文是Tri Ace 在 cedec2010上发布的文章,主要描述了他们基于物理光照的实现方法,这 ...
- Unix时间戳(Unix timestamp)转换工具
http://tool.chinaz.com/Tools/unixtime.aspx 现在的Unix时间戳(Unix timestamp)是 1440732364 Unix时间戳( ...
- PDB文件:每个开发人员都必须知道的
PDB Files: What Every Developer Must Knowhttp://www.wintellect.com/CS/blogs/jrobbins/archive/2009/05 ...
- System.Data.OracleClient.OracleConnection已过时
解决办法如下: 1.把原来的using System.Data.OracleClient;去掉 2.在oracle安装目录下找到Oracle.DataAccess.dll 添加引用:using Ora ...
- Laravel timestamps 设置为unix时间戳
Laravel timestamps 设置为unix时间戳 class BaseModel extends Eloquent { /** * 默认使用时间戳戳功能 * * @var bool */ p ...
- embody the data item with the ability to control access to itself
Computer Science An Overview _J. Glenn Brookshear _11th Edition Such communication needs have long b ...
- java 操作数据库
package foo;import java.sql.*; public class JdbcDemo { private static Connection conn; private stati ...
- [Virtualization][SDN] 讲的很好的SDN软件定义网络视频课程
51CTO的免费课程,开始以为是扯蛋的,后来看了一下,讲的很好.注册一下,免费的. 只看了导论,挺好的. http://edu.51cto.com/course/course_id-4466.html
- [Virtualization][SDN] VXLAN到底是什么 [转]
写在转发之前: 几个月以前,在北大机房和燕园大厦直接拉了一根光钎.两端彼此为校园内公网IP.为了方便连接彼此机房,我做个一个VPN server在燕园的边界,北大机房使用client拨回.两个物理机房 ...
- GetLogicalProcessorInformation(XP3才支持)和GetLogicalProcessorInformationEx(WIN7才支持)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms683194(v=vs.85).aspx 例子执行结果如下: https://ms ...