python 實現 jumpserver 自動登陸

如今不少團隊都在使用 jumpserver(http://www.jumpserver.org/ )做爲跳板機,管理服務器權限,一些對安全要求比較高的團隊還會加上 Google authenticator (Google 身份驗證器)。python

可是安全和方便就是蹺蹺板的兩端,太安全了,就會不方便。安全

每次登陸跳板機還要打開手機輸入 Google authenticator 的安全碼,有點蛋疼,效率也低。服務器

SecureCRT 能夠使用 python 實現自動登陸架構

安裝 Google authenticator 的 python 實現python2.7

pip install pyotp

自動登陸腳本ssh

# $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 的安裝路徑3d

>>> 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']

>>>

而後根據把上面定義好的自動登陸腳本配置在紅框處code

login script

驗證方式選下面這兩種 server

這個自動登陸腳本,能夠直達你想去的服務器blog

# $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架構師

相關文章
相關標籤/搜索