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
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to