【问题标题】:how do I add a variable into the telnet write command如何在 telnet 写入命令中添加变量
【发布时间】:2018-11-05 22:25:55
【问题描述】:

我一直在尝试编写允许我从远程登录服务器获取文件的代码,如果我们可以在远程登录写入函数中包含变量,我想这样做。

这是我的代码。

import telnetlib  
import sys  
import getpass  

host = "192.168.1.1"  

tn = telnetlib.Telnet(host, "23")  
tn.write((" \n").encode('ascii'))  
tn.write((" \n").encode('ascii'))  
tn.write(("terminal length 1000 \n").encode('ascii'))  
tn.write(("terminal width 24 \n").encode('ascii'))  
tn.write(("""copy command-output "show tech all" tftp 192.168.1.10 test.txt \n""").encode('ascii'))  
tn.write(("exit \n").encode('ascii'))  
tn.write(("exit \n").encode('ascii'))  
tn.write(("y").encode('ascii'))  

print (tn.read_all())

这是正在发送的命令,它应该在一个字符串中。

复制命令输出“show tech all”tftp 192.168.1.10 test.txt

我想将它改为 host.txt 而不是 test.txt

比如上面的代码host == 192.168.1.1,那么文件名本质上应该是192.168.1.1.txt,这样可以吗?

【问题讨论】:

  • 您可以在任何可以使用字符串(字面量)的地方使用变量(其值为字符串)。你有什么不清楚的地方?

标签: python python-3.x telnet telnetlib


【解决方案1】:
import telnetlib  
import sys  
import getpass  

host = "192.168.1.1"  

tn = telnetlib.Telnet(host, "23")  
tn.write(b"\n")
tn.write(b"\n") 
tn.write(b"terminal length 1000 \n")
tn.write(b"terminal width 24 \n")
command=f"copy command-output "show tech all" tftp {host} test.txt"
tn.write(command.encode('ascii')+b"\n")  
tn.write(b"exit\n") 
tn.write(b"exit\n")
tn.write(b"y")
print (tn.read_all())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2021-11-22
    • 2021-01-25
    • 2017-08-10
    • 2015-08-07
    • 2020-12-12
    相关资源
    最近更新 更多