Python Forum
How to I define a variable between strings in telnetlib write?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to I define a variable between strings in telnetlib write?
#1
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
Reply
#2
>>> 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'
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 757 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  telnetlib error 3lnyn0 1 831 Dec-06-2022, 02:46 PM
Last Post: 3lnyn0
  Define Variable xinyulon 5 1,229 Nov-03-2022, 01:12 AM
Last Post: Larz60+
  How to write a part of powershell command as a variable? ilknurg 2 1,118 Jul-26-2022, 11:31 AM
Last Post: ilknurg
  Splitting strings in list of strings jesse68 3 1,758 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  How to use a variable in Python (2.x) to define decimal part? MDRI 4 2,326 May-07-2021, 12:39 AM
Last Post: MDRI
  How to define a variable in Python that points to or is a reference to a list member JeffDelmas 4 2,643 Feb-28-2021, 10:38 PM
Last Post: JeffDelmas
  extract particular strings into a variable lokesh 3 2,108 Dec-09-2020, 11:59 AM
Last Post: lokesh
  define a variable using an if statement Margaridalopes 2 2,172 Oct-24-2020, 05:47 PM
Last Post: jefsummers
  Learning Python with telnetlib. Script seems to stall John_Williamson 1 2,720 Jul-23-2020, 05:52 PM
Last Post: John_Williamson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020