最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何在Python3中使用telnetlib模塊連接網(wǎng)絡設備

 更新時間:2020年09月21日 08:35:40   作者:chier11  
這篇文章主要介紹了如何在Python3中使用telnetlib模塊連接網(wǎng)絡設備,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

Python中專門提供了telnetlib庫,用來完成基于telnet協(xié)議的通信功能。

python3下使用telnetlib模塊連接網(wǎng)絡設備經(jīng)常會遇到字節(jié)與字符不匹配的問題

問題提示如下:

import telnetlib
Host = "10.10.10.10"
# 連接Telnet服務器
tn = telnetlib.Telnet(Host, port=23, timeout=10)
tn.set_debuglevel(0)

# 輸入登錄用戶名
tn.read_until(b'login: ')
tn.write(b"admin" + b'\n')

# 輸入登錄密碼
tn.read_until(b'Password: ')
tn.write(b"Admin@1234" + b'\n')

tn.read_until(b'#')
tn.write(b"cd /home/sd" + b'\n')

tn.read_until(b'#')
tn.write(b"ls -al" + b'\n')

r = tn.read_until(b'#').decode('ASCII')
r1 = r.split(r"\r\n")
for i in r1:
  print(i)

tn.close()

以下是設備實例:

>>> tn=telnetlib.Telnet("10.10.0.6",timeout=2)
>>> tn.read_until(b'login: ',timeout=2)
b"\r\n******************************************************************
****\r\n* Copyright (c) 2004-2018 New H3C Technologies Co., Ltd. All rig
rved.*\r\n* Without the owner's prior written consent,
    *\r\n* no decompiling or reverse-engineering shall be allowed.
     *\r\n**********************************************************
************\r\n\r\nlogin: "
>>> tn.write(b'admin'+b'\n')
>>> tn.read_until(b'Password: ',timeout=2)
b'jgtl\r\r\nPassword: '
>>> tn.write(b'Admin@123'+b'\n')
>>> tn.read_until(b'>')
b'\r\n<bangong-01>'
>>> tn.write(b'ping 10.10.0.7')
>>> tn.read_until(b'>')

以上是命令行執(zhí)行的過程。寫成腳本需要考慮兩個問題,一個是變量的替換如何編碼解封,一個是輸出結果加解碼

#-*- coding:utf-8 -*-
import telnetlib
import re
import csv
import sys
import time
from datetime import datetime

host_dict={
  "ip":"10.10.0.6",
  "user":"admin",
  "pwd":"Admin@123"
}

def get_loss(addrlist):
  host=host_dict["ip"]
  user=host_dict["user"]
  pwd=host_dict["pwd"]
  print (host)
  resultlist = []
  #try:
  tn = telnetlib.Telnet(host, timeout=2)
  print ("AA")
  if len(host_dict["pwd"]) and len(host_dict["user"]):
    print ("BB")
    tn.read_until(b"login: ", timeout=3)
    #tn.write(b"admin"+b"\n")
    tn.write(user.encode()+b"\n")
    tn.read_until(b"Password: ", timeout=3)
    #tn.write(b"Admin@123"+b"\n")
    tn.write(pwd.encode()+ b"\n")
    # p_error = re.compile("found at")

  if tn.read_until(b">", timeout=4).find(b">") != -1:
    print("Connect to {host} ...... ".format(host=host))
    tn.write(b"ping 127.0.0.1\n")
    print (tn.read_until(b'01>'))
  else:
    print("%s Wrong username or password!!!" % host)
    return ""
  #tn.read_until(b">")

  if len(addrlist) != 0:
    for i in range(len(addrlist)-1):
      tep = {}
      command = "ping " + addrlist[i]
      print("command:", command)
      tn.write(command.encode() + b"\n")
      result = str(tn.read_until(b"01>"))
      print(result)
      re_loss = re.compile("\d+\.\d+%")
      loss = re_loss.findall(result)
      tep[host] = loss[0]
      resultlist.append(tep)
      #if p_error.search(result.decode()):
      #  print("There is a error in this command: {0}".format(c.decode()))
  tn.close()
  #except Exception as e:
    #if e:
    #  print ("Connect to {host} Failed!!!".format(host=host),e)
    #return ""
  return resultlist

if __name__=="__main__":
  addrlist=['10.10.0.2','10.10.0.5']
  print ("get_loss",get_loss(addrlist))

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 在python win系統(tǒng)下 打開TXT文件的實例

    在python win系統(tǒng)下 打開TXT文件的實例

    下面小編就為大家分享一篇在python win系統(tǒng)下 打開TXT文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python concurrent.futures模塊使用實例

    Python concurrent.futures模塊使用實例

    這篇文章主要介紹了Python concurrent.futures模塊使用實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • Python爬蟲獲取基金列表

    Python爬蟲獲取基金列表

    這篇文章主要介紹了Python爬蟲獲取基金列表,python爬蟲用來收集數(shù)據(jù)是最直接和常用的方法,可以使用python爬蟲程序獲得大量的數(shù)據(jù),下文更多相關內(nèi)容介紹,需要的小伙伴可以參考一下
    2022-05-05
  • 用Python獲取智慧校園每日課表并自動發(fā)送至郵箱

    用Python獲取智慧校園每日課表并自動發(fā)送至郵箱

    很多小伙伴們都在為查看智慧校園課表而煩惱,今天特地整理了這篇文章,不僅可以用Python獲取智慧校園每日課表,還會自動發(fā)至你郵箱,需要的朋友可以參考下
    2021-05-05
  • Python subprocess模塊功能與常見用法實例詳解

    Python subprocess模塊功能與常見用法實例詳解

    這篇文章主要介紹了Python subprocess模塊功能與常見用法,結合實例形式詳細分析了subprocess模塊功能、常用函數(shù)相關使用技巧,需要的朋友可以參考下
    2018-06-06
  • Python+Plotly繪制精美的數(shù)據(jù)分析圖

    Python+Plotly繪制精美的數(shù)據(jù)分析圖

    Plotly?是目前已知的Python最強繪圖庫,比Echarts還強大許多。它的繪制通過生成一個web頁面完成,并且支持調(diào)整圖像大小,動態(tài)調(diào)節(jié)參數(shù)。本文將利用Plotly繪制精美的數(shù)據(jù)分析圖,感興趣的可以了解一下
    2022-05-05
  • 詳解使用python的logging模塊在stdout輸出的兩種方法

    詳解使用python的logging模塊在stdout輸出的兩種方法

    這篇文章主要介紹了詳解使用python的logging模塊在stdout輸出的相關資料,需要的朋友可以參考下
    2017-05-05
  • Python實現(xiàn)Excel和CSV之間的相互轉(zhuǎn)換

    Python實現(xiàn)Excel和CSV之間的相互轉(zhuǎn)換

    通過使用Python編程語言,編寫腳本來自動化Excel和CSV之間的轉(zhuǎn)換過程,可以批量處理大量文件,定期更新數(shù)據(jù),并集成轉(zhuǎn)換過程到自動化工作流程中,本文將介紹如何使用Python 實現(xiàn)Excel和CSV之間的相互轉(zhuǎn)換,需要的朋友可以參考下
    2024-03-03
  • 詳解Python3中的迭代器和生成器及其區(qū)別

    詳解Python3中的迭代器和生成器及其區(qū)別

    本篇將介紹Python3中的迭代器與生成器,描述可迭代與迭代器關系,并實現(xiàn)自定義類的迭代器模式。非常具有實用價值,需要的朋友可以參考下
    2018-10-10
  • 使用jupyter notebook運行python和R的步驟

    使用jupyter notebook運行python和R的步驟

    這篇文章主要介紹了使用jupyter notebook運行python和R的步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08

最新評論

信宜市| 金华市| 沽源县| 东平县| 庄浪县| 清丰县| 揭西县| 泗洪县| 中西区| 藁城市| 象山县| 双辽市| 堆龙德庆县| 玛曲县| 太保市| 柞水县| 新乡市| 兴化市| 舟曲县| 夏邑县| 阳城县| 丰台区| 保康县| 红河县| 平顺县| 潮安县| 墨玉县| 东安县| 陆良县| 漳平市| 扶余县| 瓦房店市| 富源县| 沁水县| 本溪| 北安市| 股票| 阿城市| 历史| 晋宁县| 柳江县|