1

I've been trying to write a code that allows me to get a file from a telnet server and I want to if we can include variables in telnet write function.

Here is my code.

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())

This the command that is being sent and it should be in a single string.

copy command-output "show tech all" tftp 192.168.1.10 test.txt

Instead of test.txt I want to make it host.txt

For example in the above code host == 192.168.1.1, so essentially the file name should be 192.168.1.1.txt, is this possible?

1 Answers1

0
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())