Python實(shí)現(xiàn)堡壘機(jī)模式下遠(yuǎn)程命令執(zhí)行操作示例
本文實(shí)例講述了Python實(shí)現(xiàn)堡壘機(jī)模式下遠(yuǎn)程命令執(zhí)行操作。分享給大家供大家參考,具體如下:
一 點(diǎn)睛
堡壘機(jī)環(huán)境在一定程度上提升了運(yùn)營(yíng)安全級(jí)別,但同時(shí)也提高了日常運(yùn)營(yíng)成本,作為管理的中轉(zhuǎn)設(shè)備,任何針對(duì)業(yè)務(wù)服務(wù)器的管理請(qǐng)求都會(huì)經(jīng)過此節(jié)點(diǎn),比如SSH協(xié)議,首先運(yùn)維人員在辦公電腦通過SSH協(xié)議登錄堡壘機(jī),再通過堡壘機(jī)SSH跳轉(zhuǎn)到所有的業(yè)務(wù)服務(wù)器進(jìn)行維護(hù)操作。
我們可以利用paramiko的invoke_shell機(jī)制來實(shí)現(xiàn)通過堡壘機(jī)實(shí)現(xiàn)服務(wù)器操作,原理是SSHClient.connect到堡壘機(jī)后開啟一個(gè)新的SSH會(huì)話 (session),通過新的會(huì)話運(yùn)行“ssh user@IP”去實(shí)現(xiàn)遠(yuǎ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ù)堡壘機(jī) bluser="root" blpasswd="123456" port=22 passinfo='\'s password: ' # 輸入服務(wù)器密碼的前標(biāo)志串 paramiko.util.log_to_file('syslogin.log') ssh=paramiko.SSHClient() # ssh登錄堡壘機(jī) ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=blip,username=bluser,password=blpasswd) #new session channel=ssh.invoke_shell() # 創(chuàng)建會(huì)話,開啟命令調(diào)用 channel.settimeout(10) # 會(huì)話命令執(zhí)行超時(shí)時(shí)間,單位為秒 buff = '' resp = '' channel.send('ssh '+username+'@'+hostname+'\n') # 執(zhí)行ssh登錄業(yè)務(wù)主機(jī) while not buff.endswith(passinfo): # ssh登錄的提示信息判斷,輸出串尾含有"\'s password:"時(shí)退出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"時(shí)發(fā)送"yes"并回車 channel.send('yes\n') print(buff) print("*************************************************************************************") channel.send(password+'\n') # 發(fā)送業(yè)務(wù)主機(jī)密碼 buff='' while not buff.endswith('# '): # 輸出串尾為"# "時(shí)說明校驗(yàn)通過并退出while循環(huán) resp = channel.recv(9999) if not resp.find(passinfo)==-1: # 輸出串尾含有"\'s password: "時(shí)說明 密碼不正確,要求重新輸入 print 'Error info: Authentication failed.' channel.close() # 關(guān)閉連接對(duì)象后退出 ssh.close() sys.exit() buff += resp channel.send('ifconfig\n') # 認(rè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 ~]#
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python字符串操作技巧匯總》、《Python常用遍歷技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python socket套接字實(shí)現(xiàn)C/S模式遠(yuǎn)程命令執(zhí)行功能案例
- Python實(shí)現(xiàn)SSH遠(yuǎn)程登陸,并執(zhí)行命令的方法(分享)
- python SSH模塊登錄,遠(yuǎn)程機(jī)執(zhí)行shell命令實(shí)例解析
- python利用paramiko連接遠(yuǎn)程服務(wù)器執(zhí)行命令的方法
- python中執(zhí)行shell命令的幾個(gè)方法小結(jié)
- Python執(zhí)行Linux系統(tǒng)命令的4種方法
- Python實(shí)現(xiàn)ssh批量登錄并執(zhí)行命令
- python實(shí)現(xiàn)多線程的方式及多條命令并發(fā)執(zhí)行
- python文件讀寫操作與linux shell變量命令交互執(zhí)行的方法
- 對(duì)python中執(zhí)行DOS命令的3種方法總結(jié)
- 日常整理python執(zhí)行系統(tǒng)命令的常見方法(全)
相關(guān)文章
python+tkinter實(shí)現(xiàn)學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python+tkinter實(shí)現(xiàn)學(xué)生管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08Python線性點(diǎn)運(yùn)算數(shù)字圖像處理示例詳解
這篇文章主要為大家介紹了數(shù)字圖像處理基本運(yùn)算如何用Python詳細(xì)點(diǎn)運(yùn)算來處理數(shù)字圖像有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09Python中關(guān)于面向?qū)ο笾欣^承的詳細(xì)講解
面向?qū)ο缶幊?(OOP) 語言的一個(gè)主要功能就是“繼承”。繼承是指這樣一種能力:它可以使用現(xiàn)有類的所有功能,并在無需重新編寫原來的類的情況下對(duì)這些功能進(jìn)行擴(kuò)展2021-10-10Sklearn調(diào)優(yōu)之網(wǎng)格搜索與隨機(jī)搜索原理詳細(xì)分析
這篇文章主要介紹了Sklearn調(diào)優(yōu)之網(wǎng)格搜索與隨機(jī)搜索原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02python?opencv實(shí)現(xiàn)影像拼接
這篇文章主要介紹了python?opencv實(shí)現(xiàn)影像拼接,主要包括內(nèi)容又垂直影像拼接vconcat和水平影像拼接hconcat以及縱向拼接多個(gè)不同圖片,下面詳細(xì)的相關(guān)內(nèi)容,需要的朋友可以參考一下2022-03-03yolov5 win10 CPU與GPU環(huán)境搭建過程
這篇文章主要介紹了yolov5 win10 CPU與GPU環(huán)境搭建過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04