有什么方法只能获取telnetlib中命令的结果吗?

时间:2019-05-31 13:13:43

标签: python python-3.x parsing telnetlib

通过Putty在telnet中

我写命令:ip link show eth0 | awk '/ether/ {print $2}'并且我拥有计算机的MAC 我正在寻找的是获取许多计算机的MAC并将它们存储在文件中,但是当我使用telnetlib时,我得到的是命令和命令的结果。

import sys
import telnetlib

user = "root"
password = "root"

file = open("input.txt", "r")
for line in file:

    line = line.rstrip("\n")
    tn = telnetlib.Telnet(line)
    tn.read_until(b"login: ")
    tn.write(user.encode('ascii') + b"\n")
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")

    tn.write(b"ip link show eth0 | awk '/ether/ {print $2}'\n")
    tn.write(b"exit\n")

    #output =  tn.read_until(b"exit\n").decode('ascii')
    output =  tn.read_all().decode('ascii')

    #print(output)

    for i in output.split("\n"):
        print(i)

    stdout=open(line + ".txt","w")
    stdout.write(output)

我期望什么:

02:45:4b:52:0a:04

但是我得到的是:

ip link show eth0 | awk '/ether/ {print $2}'
exit
root@cpu_ekor:~ ip link show eth0 | awk '/ether/ {print $2}'
02:45:4b:52:0a:04
root@cpu_ekor:~ exit
logout

或somtimes:

root@cpu_ekor:~ ip link show eth0 | awk '/ether/ {print $2}'
02:45:4b:52:0a:04
root@cpu_ekor:~ exit
logout

0 个答案:

没有答案