设为首页 收藏本站
查看: 767|回复: 0

[经验分享] python实现跳板机

[复制链接]

尚未签到

发表于 2018-8-3 10:48:28 | 显示全部楼层 |阅读模式
#!/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],>  
##进入interact交互模式
  
foo.interact()
  
pass

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-545794-1-1.html 上篇帖子: Python之Tkinter:动作 下篇帖子: Python火车票代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表