欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)堡壘機(jī)模式下遠(yuǎn)程命令執(zhí)行操作示例

 更新時(shí)間:2019年05月09日 14:20:13   作者:cakincqm  
這篇文章主要介紹了Python實(shí)現(xiàn)堡壘機(jī)模式下遠(yuǎn)程命令執(zhí)行操作,結(jié)合實(shí)例形式分析了Python堡壘機(jī)模式執(zhí)行遠(yuǎn)程命令的原理與相關(guān)操作技巧,需要的朋友可以參考下

本文實(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ì)有所幫助。

相關(guān)文章

  • python+tkinter實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    python+tkinter實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了python+tkinter實(shí)現(xiàn)學(xué)生管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Python線性點(diǎn)運(yùn)算數(shù)字圖像處理示例詳解

    Python線性點(diǎn)運(yùn)算數(shù)字圖像處理示例詳解

    這篇文章主要為大家介紹了數(shù)字圖像處理基本運(yùn)算如何用Python詳細(xì)點(diǎn)運(yùn)算來處理數(shù)字圖像有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-09-09
  • 詳解python中的time和datetime的常用方法

    詳解python中的time和datetime的常用方法

    Python time time() 返回當(dāng)前時(shí)間的時(shí)間戳(1970紀(jì)元后經(jīng)過的浮點(diǎn)秒數(shù))。這篇文章主要介紹了python之time和datetime的常用方法 ,需要的朋友可以參考下
    2019-07-07
  • Python中關(guān)于面向?qū)ο笾欣^承的詳細(xì)講解

    Python中關(guān)于面向?qū)ο笾欣^承的詳細(xì)講解

    面向?qū)ο缶幊?(OOP) 語言的一個(gè)主要功能就是“繼承”。繼承是指這樣一種能力:它可以使用現(xiàn)有類的所有功能,并在無需重新編寫原來的類的情況下對(duì)這些功能進(jìn)行擴(kuò)展
    2021-10-10
  • pandas中8種常用的index 索引設(shè)置

    pandas中8種常用的index 索引設(shè)置

    在數(shù)據(jù)處理時(shí),經(jīng)常會(huì)因?yàn)閕ndex報(bào)錯(cuò)而發(fā)愁,本文主要介紹了pandas中8種常用的index 索引設(shè)置,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07
  • 利用Python為iOS10生成圖標(biāo)和截屏

    利用Python為iOS10生成圖標(biāo)和截屏

    這篇文章主要為大家詳細(xì)介紹了利用Python為iOS10生成圖標(biāo)和截屏的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Sklearn調(diào)優(yōu)之網(wǎng)格搜索與隨機(jī)搜索原理詳細(xì)分析

    Sklearn調(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-02
  • python?opencv實(shí)現(xiàn)影像拼接

    python?opencv實(shí)現(xiàn)影像拼接

    這篇文章主要介紹了python?opencv實(shí)現(xiàn)影像拼接,主要包括內(nèi)容又垂直影像拼接vconcat和水平影像拼接hconcat以及縱向拼接多個(gè)不同圖片,下面詳細(xì)的相關(guān)內(nèi)容,需要的朋友可以參考一下
    2022-03-03
  • Python使用py2exe打包程序介紹

    Python使用py2exe打包程序介紹

    這篇文章主要介紹了Python使用py2exe打包程序介紹,本文講解了py2exe簡(jiǎn)介、安裝、用法、指定額外文件等內(nèi)容,需要的朋友可以參考下
    2014-11-11
  • yolov5 win10 CPU與GPU環(huán)境搭建過程

    yolov5 win10 CPU與GPU環(huán)境搭建過程

    這篇文章主要介紹了yolov5 win10 CPU與GPU環(huán)境搭建過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04

最新評(píng)論