Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to log the output from the multiple telnet sessions to separate log file

570 views
Skip to first unread message

manjunatha....@gmail.com

unread,
Oct 17, 2015, 1:47:42 AM10/17/15
to
Hello All,

I'm very much new to python.
I'm doing the automation for networking device testing , I will be opening the 4 telnet session, and doing some testing operations on each of those telnet sessions and capture or log the respective output in 4 different log files. As of now all the 4 log files have the same content kindly help me to log the output respective log file.

Code snippet is given below:
----------------------------------------
import sys
import PmTelnet2
import logging
import re
import time

class Logger():
def __init__(self,log):
self.terminal = sys.stdout
self.log = log

def write(self, message):
self.terminal.write(message)
self.log.write(message)


#This function is to open the telnet session and open the file for each device.
def IP_port(file) :
global Testbed_info
Testbed_info = []
F = open(file, 'r')
F.seek(0)
line = F.read()
tuples = re.findall(r'(.+ \d)\s+(.+?)\s+(\d+)', line)
for (dname, ip, port) in tuples :
logfile = dname.replace(" ","") + ".log"
log = open(logfile, "a")
Telnet_handle=PmTelnet2.TelnetSession(ip,port)
sys.stdout =Logger(log)
tuple = (dname, ip, port, Telnet_handle)
Testbed_info.append(tuple)
#T.append(T1)
return(Testbed_info)


# Here I'm passing the device name, IP, port details using the IP_port.txt file

file = '/users/manmahal/MANJU/IP_port.txt'
Testbed_info = IP_port(file)


#Here I'm using the telnet object to execute some command, What ever I execute here on each telnet session to be logged into separate log file.

for (dname, ip, port, Telnet, fp ) in Testbed_info :
My_handle = Telnet
My_handle.Write("\n")
My_handle.Write("show config \n")
time.sleep(2)
print My_handle.ReadBuffer()

Content of the IP_port file is as below:
-----------------------------------------------------
RSP 0 172.27.40.60 2001
RSP 1 172.27.40.60 2002
LC 0 172.27.40.60 2010
LC 1 172.27.40.59 2011


Please note that total number of the telnet session may change as requirement of Testing. Hence It would be great if some give me generic solution to log the log messages for each telnet session and output should to logged into respective log file.

Thanks in advance...!!

Regards
Manju

Chris Angelico

unread,
Oct 17, 2015, 2:06:24 AM10/17/15
to pytho...@python.org
On Sat, Oct 17, 2015 at 4:47 PM, <manjunatha....@gmail.com> wrote:
> class Logger():
> def __init__(self,log):
> self.terminal = sys.stdout
> self.log = log
>
> def write(self, message):
> self.terminal.write(message)
> self.log.write(message)
>
>
> for (dname, ip, port) in tuples :
> sys.stdout =Logger(log)

Every time you construct a Logger, it snapshots the previous value of
sys.stdout. By the time you've set them all up, sys.stdout is the last
Logger created, which will chain to the one created previous to it,
then the one before that, and finally to the actual console.

Do you need to use sys.stdout at all? It would be a lot easier to
ignore stdout and just write your logs directly to files.

ChrisA

dieter

unread,
Oct 17, 2015, 2:11:58 AM10/17/15
to pytho...@python.org
manjunatha....@gmail.com writes:
> I'm very much new to python.
> I'm doing the automation for networking device testing , I will be opening the 4 telnet session, and doing some testing operations on each of those telnet sessions and capture or log the respective output in 4 different log files.

Personally, I find it a bit strange that each (telnet) session
should get its own logfile, but, if that is what you need, I would
approach it as follows:

* define a class "TelnetSession"; create a logger in its "__init__" method;
use this logger for all operations inside a "TelnetSession"

* instantiate the class "TelnetSession" for each telnet session you
want to open.
Use those objects method to operate on the sessions.

manjunatha....@gmail.com

unread,
Oct 17, 2015, 1:20:23 PM10/17/15
to
Hello ChrisA,

Thank you so much for the replay, objective is to collect all terminal output, if we have any other option to do the same with out using stdout your most welcome to suggest me.

Regards
Manju

manjunatha....@gmail.com

unread,
Oct 17, 2015, 1:25:20 PM10/17/15
to
Hello Dieter,

Thank you so much for the reply and suggestion.. I believe this approch should work for me..!! thanks for your time..


Regards
Manju

manjunatha....@gmail.com

unread,
Oct 19, 2015, 5:49:09 PM10/19/15
to
On Friday, October 16, 2015 at 11:11:58 PM UTC-7, dieter wrote:
Hello Dieter,

I created the my own class MyLogger and passing log file name to it. I'm seeing no log is being written to passed log file instead everything is written to the logfilename [actually logfilename is variable with file name] I'm trying to create MyLogger object for each telnet session. and use that object.

MyLogger is beeing called inside the Telnetsession class. those are present in PmTelnet3.py file.


class MyLogger():
import logging
def __init__(self,logfilename):
#create logger with 'spam_application'
self.logger = self.logging.getLogger('TelnetSession')
self.logger.setLevel(self.logging.INFO)
# create file handler which logs even debug messages
#self.fh = self.logging.FileHandler(logfile)
print "The log file name is %s\n" % logfilename
self.logging.basicConfig(filename = logfilename,
level = self.logging.INFO ,
format= '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filemode='w')
# create console handler with a higher log level
self.ch = self.logging.StreamHandler()


def Log(self):
self.logger.setLevel(self.logging.INFO)
self.fh.setLevel(self.logging.INFO)
self.ch.setLevel(self.logging.INFO)
self.fh.setFormatter(self.formatter)
self.ch.setFormatter(self.formatter)
self.logger.addHandler(self.ch)


class TelnetSession:

import telnetlib
import logging

def __init__(self, inHost, inPort, Logname):
self.log = MyLogger(Logname)
self.telnet = self.telnetlib.Telnet(inHost, inPort)
----------------------------------------------------------------------------

def IP_port(file) :
global Testbed_info
Testbed_info = []
F = open(file, 'r')
F.seek(0)
line = F.read()
tuples = re.findall(r'(.+ \d)\s+(.+?)\s+(\d+)', line)
for (dname, ip, port) in tuples :
LogFileName = dname.replace(" ","") + ".log"
#Log = open(logfile, "a")
#Log = MyLogger(LogFileName)
Telnet_handle=PmTelnet3.TelnetSession(ip,port,LogFileName)
tuple = (dname, ip, port, Telnet_handle )
print tuple
Testbed_info.append(tuple)
#T.append(T1)
return(Testbed_info)


#This function is to switch the console

file = '/users/manmahal/MANJU/IP_port.txt'
Testbed_info = IP_port(file)
print "Iam done"


for (dname, ip, port, Telnet) in Testbed_info :

My_handle = Telnet
#My_log = log

My_handle.Write("\n")
My_handle.Write("show config \n")
time.sleep(2)
output = My_handle.ReadBuffer()
#My_fp.write(My_handle.ReadBuffer())
My_handle.log.logger.info(output)
My_handle.log.logger.info("HI THERE")

Kindly let me know how can I fix this..
0 new messages