這篇文章主要介紹如何使用Python實現(xiàn)堡壘機模式下遠程命令執(zhí)行操作,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體如下:
一 點睛
堡壘機環(huán)境在一定程度上提升了運營安全級別,但同時也提高了日常運營成本,作為管理的中轉(zhuǎn)設(shè)備,任何針對業(yè)務(wù)服務(wù)器的管理請求都會經(jīng)過此節(jié)點,比如SSH協(xié)議,首先運維人員在辦公電腦通過SSH協(xié)議登錄堡壘機,再通過堡壘機SSH跳轉(zhuǎn)到所有的業(yè)務(wù)服務(wù)器進行維護操作。
我們可以利用paramiko的invoke_shell機制來實現(xiàn)通過堡壘機實現(xiàn)服務(wù)器操作,原理是SSHClient.connect到堡壘機后開啟一個新的SSH會話 (session),通過新的會話運行“ssh user@IP”去實現(xiàn)遠程執(zhí)行命令的操作。
二 代碼
#coding=utf-8 #!/usr/bin/env python import paramiko import os,sys,time hostname="192.168.0.120" # 定義業(yè)務(wù)服務(wù)器 username="root" password="123456" blip="192.168.0.101" # 定義業(yè)務(wù)堡壘機 bluser="root" blpasswd="123456" port=22 passinfo='\'s password: ' # 輸入服務(wù)器密碼的前標志串 paramiko.util.log_to_file('syslogin.log') ssh=paramiko.SSHClient() # ssh登錄堡壘機 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=blip,username=bluser,password=blpasswd) #new session channel=ssh.invoke_shell() # 創(chuàng)建會話,開啟命令調(diào)用 channel.settimeout(10) # 會話命令執(zhí)行超時時間,單位為秒 buff = '' resp = '' channel.send('ssh '+username+'@'+hostname+'\n') # 執(zhí)行ssh登錄業(yè)務(wù)主機 while not buff.endswith(passinfo): # ssh登錄的提示信息判斷,輸出串尾含有"\'s password:"時退出while循環(huán) try: resp = channel.recv(9999) except Exception,e: print 'Error info:%s connection time.' % (str(e)) channel.close() ssh.close() sys.exit() buff += resp if not buff.find('yes/no')==-1: # 輸出串尾含有"yes/no"時發(fā)送"yes"并回車 channel.send('yes\n') print(buff) print("*************************************************************************************") channel.send(password+'\n') # 發(fā)送業(yè)務(wù)主機密碼 buff='' while not buff.endswith('# '): # 輸出串尾為"# "時說明校驗通過并退出while循環(huán) resp = channel.recv(9999) if not resp.find(passinfo)==-1: # 輸出串尾含有"\'s password: "時說明 密碼不正確,要求重新輸入 print 'Error info: Authentication failed.' channel.close() # 關(guān)閉連接對象后退出 ssh.close() sys.exit() buff += resp channel.send('ifconfig\n') # 認證通過后發(fā)送ifconfig命令來查看結(jié)果 buff='' try: while buff.find('# ')==-1: resp = channel.recv(9999) buff += resp except Exception, e: print "error info:"+str(e) print buff # 打印輸出串 channel.close() ssh.close()
三 輸出結(jié)果
E:\Python\python_auto_maintain\venv\Scripts\python.exe E:/Python/python_auto_maintain/6_3_2.py
Last login: Thu Feb 28 22:00:07 2019 from 192.168.0.106
hello cakin24!
ssh root@192.168.0.120
[root@slave2 ~]# ssh root@192.168.0.120
root@192.168.0.120's password:
*************************************************************************************
ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.120 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::a00:27ff:fe0a:6e8a prefixlen 64 scopeid 0x20<link>
ether 08:00:27:0a:6e:8a txqueuelen 1000 (Ethernet)
RX packets 2046 bytes 179711 (175.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1408 bytes 148744 (145.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 404117 bytes 68752333 (65.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 404117 bytes 68752333 (65.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
qbr07d54630-64: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
ether 42:76:47:57:b2:75 txqueuelen 1000 (Ethernet)
RX packets 11 bytes 572 (572.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
qvb07d54630-64: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST> mtu 1450
inet6 fe80::4076:47ff:fe57:b275 prefixlen 64 scopeid 0x20<link>
ether 42:76:47:57:b2:75 txqueuelen 1000 (Ethernet)
RX packets 12 bytes 816 (816.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 648 (648.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
qvo07d54630-64: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST> mtu 1450
inet6 fe80::dcbe:efff:feb7:5d52 prefixlen 64 scopeid 0x20<link>
ether de:be:ef:b7:5d:52 txqueuelen 1000 (Ethernet)
RX packets 8 bytes 648 (648.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 12 bytes 816 (816.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]#
以上是“如何使用Python實現(xiàn)堡壘機模式下遠程命令執(zhí)行操作”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享名稱:如何使用Python實現(xiàn)堡壘機模式下遠程命令執(zhí)行操作-創(chuàng)新互聯(lián)
本文鏈接:http://www.ekvhdxd.cn/article6/doiiig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、搜索引擎優(yōu)化、自適應(yīng)網(wǎng)站、網(wǎng)站收錄、動態(tài)網(wǎng)站、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)