如何在Telnet写入命令中添加变量

2024-05-23 18:33:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试编写一个代码,允许我从telnet服务器获取一个文件,我想知道是否可以在telnet write函数中包含变量。在

这是我的密码。在

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测试.txt在

而不是测试.txt我想去主机.txt在

例如在上面的代码host==192.168.1.1中,那么实际上文件名应该是192.168.1.1.txt,这可能吗?在


Tags: 代码importtxthostshowasciialltech