首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Telnet进行Python线程处理

用Telnet进行Python线程处理
EN

Stack Overflow用户
提问于 2021-10-05 01:58:54
回答 1查看 234关注 0票数 0

我正试图通过telnet连接到多个交换机,并获得一个与CPU使用有关的输出。一个线程工作,并显示正确的CPU使用。第二个线程什么也不做。如何使两个线程使用与第一个命令相同的命令。

代码语言:javascript
复制
import time
import telnetlib
import threading


Host1 = '192.168.1.42'
username1 = 'root'

Host2 = '192.168.86.247'
username2 = 'root'

tn = telnetlib.Telnet(Host1)

def switch1():
   tn.write(username1.encode("ascii") + b"\n")

   #confirms connection
   print("connected to %s" % Host1)

   #send command
   tn.write(b"sh cpu-usage\n")
   time.sleep(2)

   #reads clean i/o
   output = tn.read_very_eager()


   #print the command
   print (type("output"))
   print(output)
   print("done")



def switch2():
   #input username
   tn.write(username2.encode("ascii") + b"\n")
   tn.write(password.encode("ascii") + b"\n")

   #confirms connection
   print("connected to %s" % Host2)

   #send command
   tn.write(b"sh cpu-usage\n")
   time.sleep(2)

   #reads clean i/o
   output1 = tn.read_very_eager()


   #print the command
   print (type("output"))
   print(output1)
   print("done")



t1 = threading.Thread(target=switch1)



t2 = threading.Thread(target=switch2)





t1.start()
t2.start()

这是输出

代码语言:javascript
复制
[Command: python -u C:\Users\AKPY7Z\Documents\Threading\threadcpu.py]
connected to 192.168.1.42
connected to 192.168.86.247
<class 'str'><class 'str'>
b'ugoonatilaka\r\r\n         ^\r\n% Invalid input detecte'
done
b"\r\r\nswitch_a login: root\r\njanidugoonatilaka\r\nsh cpu-usage\r\n*password*\r\nifconfig\r\n\r\r\nSwitch version 2.01.2.7 03/29/18 10:36:11\r\nswitch_a>janidd at '^' marker.\r\n\r\nswitch_a>sh cpu-usage\r\r\nNow CPU Usage 17%\r\nMax CPU Usage 18%\r\nswitch_a>*password*\r\r\n         ^\r\n% Invalid input detected at '^' marker.\r\n\r\nswitch_a>ifconfig\r\r\n         ^\r\n% Invalid input detected at '^' marker.\r\n\r\nswitch_a>"
done
[Finished in 2.678s]<class 'str'>
b'\r\n'
done
[Finished in 293.505s]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-05 02:26:17

您只创建一个交换机的连接。

代码语言:javascript
复制
tn = telnetlib.Telnet(Host1)

然后,在这两个函数中使用相同的连接,每个函数都尝试使用不同的用户名和密码--其中可能只有一个函数使用了正确的值。

您应该在一个函数中运行Telnet(Host1),在另一个函数中运行Telnet(Host2),它们将尝试访问不同的交换机。

代码语言:javascript
复制
def switch1():
   tn = telnetlib.Telnet(Host1)
   # ... rest ...

def switch2():
   tn = telnetlib.Telnet(Host2)
   # ... rest ...

BTW:

您可以创建一个函数并使用不同的参数运行它。

代码语言:javascript
复制
import time
import telnetlib
import threading


host1 = '192.168.1.42'
username1 = 'root'

host2 = '192.168.86.247'
username2 = 'root'

def switch(host, username, password=None):
    tn = telnetlib.Telnet(host)
   
    tn.write(username.encode("ascii") + b"\n")
    if password:
       tn.write(password.encode("ascii") + b"\n")
        
    # confirms connection
    print("connected to %s" % host)

    # send command
    tn.write(b"sh cpu-usage\n")
    time.sleep(2)

    # reads clean i/o
    output = tn.read_very_eager()

    # print the command
    print (type("output"))
    print(output)
    print("done")

t1 = threading.Thread(target=switch, args=(host1, username1, password))
t2 = threading.Thread(target=switch, args=(host2, username2, None))

t1.start()
t2.start()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69444096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档