Rayon wrote:
Well what I am doing is connecting to a telnet session sending some
commands, exiting and returning some data.
What I would to do is send the commands and return the data without exiting
the session.
I would like to keep the same session and just send commands and return
data.
Change the work-flow from:
repeatedly:-
log in
write data
log out
to:
log in
repeatedly:
write data
log out
This is untested, but it should point you in the right direction. For
brevity, anything unchanged will be left out.
#!/usr/bin/env python
import telnetlib
class hlr_com():
#get host and command
def __init__(self):
"""init host and command """
self.user_name = '********'
self.password = '**********'
self.host = '172.20.50.176'
self.command = ''
self._connected = False
def fix_return(self,hia_return):
# UNCHANGED FROM YOUR VERSION
#set host ip address
def set_host(self,host):
# UNCHANGED FROM YOUR VERSION
def connect(self):
# If already connected, do nothing.
if self._connected:
return
try:
hlr_tel = telnetlib.Telnet(self.host)
hlr_tel.read_until('login:')
hlr_tel.write(self.user_name+"\r")
hlr_tel.read_until('Password:')
hlr_tel.write(self.password+"\r")
self._connected = True
except Exception,error:
logs("error",str(err),'null')
def execute(self, command):
"""execute command"""
try:
self._execute_or_fail(command)
except Exception,error:
logs("error",str(err),'null')
def _execute_or_fail(self, command):
if not self._connected:
# This is probably the wrong exception type
raise ValueError('you must connect first')
# otherwise execute the command
hlr_tel.read_until('maint@atcaHLRds0 /public/users/maint>')
hlr_tel.write(command+'\r')
def end_session(self):
data = hlr_tel.read_until(
'maint@atcaHLRds0 /public/users/maint>'
)
hlr_tel.write('exit'+'\r')
data = self.fix_return(data)
return data
def logs(self,log_type,log_data,ip_address):
# UNCHANGED FROM YOUR VERSION
To use it:
instance = hlr_com()
instance.connect()
instance.execute("fe")
instance.execute("fi")
instance.execute("fo")
instance.execute("fum")
instance.end_session()
As I said, untested. Good luck!
--
Steven
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor