Python Forum

Full Version: How to I define a variable between strings in telnetlib write?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How to I use a variable in telnetlib write? I can get connected no problem. I am new to python.
for line in  ipaddress:

 Host=line.strip("\n")    
 file = Host
 
 connection.write(b"save configuration network" + file +  "1.1.1.1 \n")
I am setting file equal Host IP. This gives me an error stating I cannot concat a string to bytes. So file will chnage for every Host in ipaddress file. If I remove the '+' signs in line and one pair of double quotes, I get a backup file but the name is file.

Error:
Traceback (most recent call last): File "C:\python_scripts\Backup_multi_lx.py", line 32, in <module> connection.write(b"save configuration network" + file + "10.2.99.91 \n") TypeError: can't concat str to bytes
>>> file = "some string"
>>> output = b"save configuration network"
>>>
>>> # you can encode the string, which turns it into bytes...
...
>>> file.encode()
b'some string'
>>> # which would then be used like...
...
>>> output + file.encode()
b'save configuration networksome string'
Thanks for the help, but I figured a way around it. I used the word file so backup is called file.zip and I used os.rename() to rename it to the IP address of the device.