Python實現(xiàn)堡壘機模式下遠程命令執(zhí)行操作示例
本文實例講述了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 ~]#
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python字符串操作技巧匯總》、《Python常用遍歷技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
- Python socket套接字實現(xiàn)C/S模式遠程命令執(zhí)行功能案例
- Python實現(xiàn)SSH遠程登陸,并執(zhí)行命令的方法(分享)
- python SSH模塊登錄,遠程機執(zhí)行shell命令實例解析
- python利用paramiko連接遠程服務(wù)器執(zhí)行命令的方法
- python中執(zhí)行shell命令的幾個方法小結(jié)
- Python執(zhí)行Linux系統(tǒng)命令的4種方法
- Python實現(xiàn)ssh批量登錄并執(zhí)行命令
- python實現(xiàn)多線程的方式及多條命令并發(fā)執(zhí)行
- python文件讀寫操作與linux shell變量命令交互執(zhí)行的方法
- 對python中執(zhí)行DOS命令的3種方法總結(jié)
- 日常整理python執(zhí)行系統(tǒng)命令的常見方法(全)
相關(guān)文章
python+tkinter實現(xiàn)學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細介紹了python+tkinter實現(xiàn)學(xué)生管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08
Python中關(guān)于面向?qū)ο笾欣^承的詳細講解
面向?qū)ο缶幊?(OOP) 語言的一個主要功能就是“繼承”。繼承是指這樣一種能力:它可以使用現(xiàn)有類的所有功能,并在無需重新編寫原來的類的情況下對這些功能進行擴展2021-10-10
Sklearn調(diào)優(yōu)之網(wǎng)格搜索與隨機搜索原理詳細分析
這篇文章主要介紹了Sklearn調(diào)優(yōu)之網(wǎng)格搜索與隨機搜索原理,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
yolov5 win10 CPU與GPU環(huán)境搭建過程
這篇文章主要介紹了yolov5 win10 CPU與GPU環(huán)境搭建過程,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04

