python 实现 jumpserver 自动登录

本秂侑毒 提交于 2019-12-01 13:07:46

现在很多团队都在使用 jumpserver(http://www.jumpserver.org/ )作为跳板机,管理服务器权限,一些对安全要求比较高的团队还会加上 Google authenticator (Google 身份验证器)。

但是安全和方便就是跷跷板的两端,太安全了,就会不方便。

每次登录跳板机还要打开手机输入 Google authenticator 的安全码,有点蛋疼,效率也低。

SecureCRT 可以使用 python 实现自动登录

安装 Google authenticator 的 python 实现


pip install pyotp

自动登录脚本


# $language = "python"

# $interface = "1.0"



# This automatically generated script may need to be

# edited in order to work correctly.

import sys,pyotp

sys.path.insert(0, "/usr/local/lib/python2.7/site-packages") // 这里是pip install pyotp之后,pip 的 packeages 目录,不然会报 import Error

def Main():

 crt.Screen.Synchronous = True

 crt.Screen.WaitForString("Google authenticator:") // 这里输入跳板机提示输入安全码的字符串

 totp = pyotp.TOTP('base32secret3232') // 这里输入你的 Google authenticator 密钥,如果是二维码,就把二维码解析出来,把密钥粘贴进去

 crt.Screen.Send(totp.now())

 crt.Screen.Send("\015")

Main()

启动python,用下面的方法这里可以获取 pip packeages 的安装路径


>>> import sys

>>> print(sys.path)

['', '/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages']

>>>

然后根据把上面定义好的自动登录脚本配置在红框处

login script

验证方式选下面这两种

这个自动登录脚本,可以直达你想去的服务器


# $language = "python"
# $interface = "1.0"
# This automatically generated script may need to be
# edited in order to work correctly.
import sys,pyotp
sys.path.insert(0, "/usr/local/lib/python2.7/site-packages")
def Main():
    crt.Screen.Synchronous = True
    crt.Screen.WaitForString("Google authenticator:") // 这里输入跳板机提示输入安全码的字符串
    totp = pyotp.TOTP('base32secret3232')
    crt.Screen.Send(totp.now())
    crt.Screen.Send("\015")
    crt.Screen.WaitForString("Opt> ")
    crt.Screen.Send("/" + "\015")
    crt.Screen.WaitForString("Opt> ")
    crt.Screen.Send("这里填ip地址" + "\015")
Main()

再强调一下这句话,安全和方便就是跷跷板的两段,自己要做一个权衡

最后,如果是用 ssh 命令登录服务器的,可以使用 expect 脚本实现类似的效果。

更多架构、PHP、GO相关踩坑实践技巧请关注我的公众号:PHP架构师

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!