def establish_connection(ip, username='', password='', delay=1):
 remote_conn = telnetlib.Telnet(ip, TELNET_PORT, TELNET_TIMEOUT)
 output = remote_conn.read_until('sername:', READ_TIMEOUT)
 remote_conn.write((username + '\n'))
 output = remote_conn.read_until('ssword:', READ_TIMEOUT)
 remote_conn.write((password + '\n'))
 time.sleep(delay)
 return remote_conn

def establish_connection ( ip , username = '' , password = '' , delay = 1 ) : remote_conn = telnetlib . Telnet ( ip , TELNET_PORT , TELNET_TIMEOUT ) output = remote_conn . read_until ( 'sername:' , READ_TIMEOUT ) remote_conn . write ( ( username + '\n' ) ) output = remote_conn . read_until ( 'ssword:' , READ_TIMEOUT ) remote_conn . write ( ( password + '\n' ) ) time . sleep ( delay ) return remote_conn
use paramiko to establish an ssh channel to the device must return both return_conn_pre and return_conn so that the ssh connection is not garbage collected .