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

python使用paramiko模塊通過(guò)ssh2協(xié)議對(duì)交換機(jī)進(jìn)行配置的方法

 更新時(shí)間:2019年07月25日 10:15:46   作者:一路湘北  
今天小編就為大家分享一篇python使用paramiko模塊通過(guò)ssh2協(xié)議對(duì)交換機(jī)進(jìn)行配置的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

該代碼用的是paramiko模塊,python版本是python2.7

下面上源碼

# -*- coding: utf-8 -*-

import paramiko
import time
import os

port = '22'
username = '****'
password = '****'
ip = '****'  # 測(cè)試用的交換機(jī)ip

msg1flag = 0
mycmd1flag = 0

# 核心方法,該方法連接遠(yuǎn)程主機(jī)并打開(kāi)一個(gè)終端,并將該終端返回
def msg1(ip,mport,musername,mpassword,mflag):
  try:
    # 設(shè)置ssh連接的遠(yuǎn)程主機(jī)地址和端口
    t = paramiko.Transport(ip, mport)
    # 設(shè)置登錄名和密碼
    t.connect(username=musername, password=mpassword)
    # 連接成功后打開(kāi)一個(gè)channel
    chan = t.open_session()
    # 設(shè)置會(huì)話(huà)超時(shí)時(shí)間
    chan.settimeout(timeout=180)
    # 打開(kāi)遠(yuǎn)程的terminal
    chan.get_pty()
    # 激活terminal
    chan.invoke_shell()
    return chan
  except Exception,e:
    mflag += 1
    time.sleep(5)
    if mflag < 3:
      msg1(ip,mport,musername,mpassword,mflag)

# 黃棟淋交換機(jī)開(kāi)啟審計(jì)專(zhuān)用
def mycmd(chan,my1flag):
  try:
    chan.send('system' + '\n') # 輸入命令
    chan.send('****' + '\n')  # 輸入命令
    chan.send('****' + '\n')
    chan.send('****' + '\n')
    time.sleep(50)
    i = 1
    while i < 3:
      chan.send('\n')
      i += 1
    time.sleep(2)
    result = chan.recv(65535)   # 得到命令返回的結(jié)果
    print result
    strlen = len(result)      # 得到結(jié)果字符串的長(zhǎng)度
    print strlen
    return result

  except Exception,e:
    # print e
    my1flag += 1
    time.sleep(5)
    if my1flag < 3:
      mycmd(chan,my1flag)



nowtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) # 時(shí)間

# 測(cè)試項(xiàng)
chan_ip_test = msg1(ip,port,username,password,msg1flag)
resu_ip_test = mycmd(chan_ip_test,mycmd1flag)

以上這篇python使用paramiko模塊通過(guò)ssh2協(xié)議對(duì)交換機(jī)進(jìn)行配置的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論