0

I want to create single Telnet object which will be used in multiple functions.

tn = (Telnet object declaration only)

def Function1(): #for connection only
    tn = telnetlib.Telnet(ip)
    #code

def Function2(): #to run command 1
    tn.write()

def Function3(): #to run command 2
    tn.write()

Function1() #Call for telnet connection
Function2() #Call to execute command 1
Function3() #call to execute command 2

What could be the solution for this?

1 Answers1

0

If you are asking how to reference the same telnet object in all of the functions, you need to use a global variable. Add the declaration "global tn" inside any functions which binds tn to a new object.

Try this:

import telnetlib

tn = None

def Function1(): #for connection only
    global tn
    tn = telnetlib.Telnet(ip)
    #code

def Function2(): #to run command 1
    tn.write()

def Function3(): #to run command 2
    tn.write()

Function1() #Call for telnet connection
Function2() #Call to execute command 1
Function3() #call to execute command 2
Robᵩ
  • 163,533
  • 20
  • 239
  • 308