原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://laoguang.blog.51cto.com/6013350/1328891

公司有1000多台服务器,线上机器都是禁止root登录的,所以平时是用普通用户登录,然后在su到root,密码都是在excel表中存的,这样登录一台机器,输两次命令,搜两次密码,实在很麻烦,而且密码表都在大家手中不易控制,所以把密码放到数据库中,每次ssh登录自动去数据库中查密码,然后发送密码,实现交互,这样既方便了我们,又控制的密码,脚本的核心是用pexpect来实现交互,用MySQLdb去查询密码,把代码保存为zssh 给个执行权限,建立数据库,把密码表导入到数据库中,就可以使用zssh ip 来登录了,是不是很爽,来试试吧!

pexpect的用法看http://www.ibm.com/developerworks/cn/linux/l-cn-pexpect1/

代码见附件

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/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
     =  struct.pack( 'HHHH' 0 0 0 0 )
     =  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

数据库建立

1
2
create  database  sa;
create  table  password  (ip  varchar (15)  primary  key  not  null , muser  varchar (15), mpass  varchar (30), rpass  varchar (30));

将密码表的中的ip,普通用户名,密码,root密码插入库中我用的是一个脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/local/bin/python
import  MySQLdb
conn  =  MySQLdb.connect(host = 'localhost' , user = 'root' , passwd = 'Te62S#^t' , db = 'sa' )
cursor  =  conn.cursor()
=  open ( 'passwd.txt' )
num  =  0
for  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.20 monitor asdf123Sfad f(adfasdfasdf

202.106.0.21 zhswred hathell oworld

本文出自 “Free Linux, Share Linux” 博客,请务必保留此出处http://laoguang.blog.51cto.com/6013350/1328891

Python笔记:python实现跳板机相关推荐

  1. Python笔记 - Python切片

    Python笔记 - Python切片 Python切片是对一个列表取其部分元素获得一个子序列的常见操作,切片操作的返回结果类型与被切片的对象一致.要创建一个已有列表的切片,通过指定切片的第一个列表元 ...

  2. 【python】-- paramiko、跳板机(堡垒机)

    paramiko Python的paramiko模块,该模块用于连接远程服务器并执行相关命令,常用于作批量管理使用 一.下载: pip3 install paramiko 源码:查看 二.parami ...

  3. 第四周python笔记 Python封装结构 哈希查找 杨辉三角 冒泡排序

    知识点:浅拷贝 字符串  封装解构 集合 ipython 哈希查找与线性查找  代码实现:杨辉三角 冒泡排序 list复制 是浅拷贝 简单类型 新开地址 拷贝数值 引用类型 只拷贝引用 直接拷贝地址 ...

  4. python笔记-python编程优化:常用原则和技术介绍

    本人翻译自<Exper Python Programming> 'Premature optimization is the root of all evil in programming ...

  5. Python笔记 · Python语言的“动态性”

    尽管对于Python程序员来说已经司空见惯,但是当那些从非动态语言转过来的程序员初次看到形如self.xxx=xxx的语句就是在定义对象属性时往往会感到"离奇":一个未经声明的(类 ...

  6. Jumpserver跳板机

    Jumpserver跳板机 一.Jumpserver简介 umpserver 是一款由python编写开源的跳板机(堡垒机)系统,实现了跳板机应有的功能.基于ssh协议来管理,客户端无需安装agent ...

  7. php开源堡垒机,Jumpserver开源跳板机(堡垒机)

    Jumpserver跳板机是什么? 跳板机/堡垒机:即在一个特定的网络环境下,为了保障网络和数据不受来自外部和内部用户的入侵和破坏,而运用各种技术手段实时收集和监控网络环境中每一个组成部分的系统状态. ...

  8. 堡垒机、跳板机JumpServer的搭建使用

    ​ 一.简介 为了保证服务器安全,加个堡垒机,所有ssh连接都通过堡垒机来完成,堡垒机也需要有身份认证,授权,访问控制,审计等功能. Jumpserver 是一款由python编写开源的跳板机(堡垒机 ...

  9. 堡垒机、跳板机JumpServer的搭建,以及使用,图文详细

    一.简介 为了保证服务器安全,加个堡垒机,所有ssh连接都通过堡垒机来完成,堡垒机也需要有身份认证,授权,访问控制,审计等功能. Jumpserver 是一款由python编写开源的跳板机(堡垒机)系 ...

  10. php 跳板机连接mysql,使用python如何通过跳板机连接MySQL数据库

    生产环境中,为了安全起见,大多数的数据库是无法在本地直接访问的,需要先连接跳板机,然后通过跳板机访问.创业天下测试数据库也是采用这种方法连接数据的,那么我们应该怎么通过Python去连接数据库呢,代码 ...

最新文章

  1. 关于react diff 算法(译文)
  2. Spring框架分为哪七大模块以及各模块的主要功能作用
  3. 鼠标右键转圈圈_win10系统右键一直转圈
  4. Python 日期时间相关
  5. 自动Mock,让编写单元测试更简单
  6. Java会话技术之 —— cookie与session
  7. kelvin模型蠕变方程_第二章:黏弹性与波传播——2.4 力学模型和波传播
  8. 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典(DP)
  9. 推荐20个值得收藏的前端开源项目
  10. c 语言min max 归一化,归一化方法 Normalization Method
  11. user reg.php,织梦member/reg_new.php SQL注入漏洞修复
  12. 公务员考试行测——逻辑推理
  13. 从已知身份证号码中提取生日和性别
  14. python实现简单的词形统计
  15. 国内FRM证书福利政策大全
  16. Alpaca 羊驼API的使用
  17. 使用素描图像识别人脸
  18. java iw_java.lang.IndexOutOfBoundsException
  19. Kotlin 使用 Spring WebFlux 实现响应式编程
  20. 有关计算机的英语名言,有关生命力励志英文名言

热门文章

  1. TopK推荐的评价指标
  2. python计算协方差矩阵_在Python中构建协方差矩阵
  3. 看书学习感悟系列(三)
  4. 浅谈神经网络之链式法则与反向传播算法
  5. 《wish官方运营手册》读书笔记
  6. 深度学习(五):FastFCN代码运行、测试与预测
  7. 如何批量修改文件名?批量修改照片文件名和添加前缀
  8. python输出换行
  9. Chromium安装flash插件
  10. 冈萨雷斯《数字图像处理》学习笔记(七)小波变换和多分辨率处理