Writing with tn.write in python

61 views Asked by At

How can I write below command using tn.write on OLT device?

For Exam :

ont add 0 0 sn-auth 48575443D6ACA49C omci ont-lineprofile-id 0 ont-srvprofile-id 0

and how to remove ont from OLT with telnet!

For further review i'm put the code below . How do I solve the problem?

It is like this in OLT (Xshell). But what about Python! Xshell prtSC

I tried to do this code :

import telnetlib , sys , time , re

def autofind(ip, user, password):
    print('Connecting to OLT {0}'.format(ip))
try:
    tn = telnetlib.Telnet('10.11.104.2', 23, 10)
except Exception as e:
    print("Error connecting to OLT, please check the IP address")
    sys.exit()

tn.read_until(b"name:")
tn.write("root".encode('utf-8') + b"\n")
time.sleep(.1)
tn.read_until(b"password:")
tn.write("admin".encode('utf-8') + b"\n")
time.sleep(.1)

tn.write(b"enable\n")
time.sleep(.1)

tn.write(b"config\n")
time.sleep(.1)

tn.write(b"interface gpon all\n")
time.sleep(.1)

# autofind
tn.write("display ont autofind all ".encode('utf-8') + b"\n")
return_lineid = tn.read_until('The number of GPON'.encode('utf-8'), 3).decode('utf-8')
data_return = return_lineid.splitlines()
records = []
current_record = {}
serials = []
autofind_list = []
autofind_object = {}
for line in data_return:
    line = line.strip()

    autofind_list.append(autofind_object)

    if not line:  # empty line
        records.append(current_record)
        current_record = {}
    else:

        
        if "Ont SN" in line:
            key, value = line.split(':')
            key = key.strip()
            value = value.strip()
            current_record[key] = value
            # regex for removing brackets for example (HWTC-7D85CA9E)
            COPYTXT = "ont add {pon} {num} sn-auth {value}omci ont-lineprofile-id 0 ont-srvprofile-id 0"
            serial = re.sub(r'\(.*\)', '', value)
            serials.append(serial)
            autofind_object['SN'] = serial
for i in range(len(serials)):
       print(COPYTXT.format(pon=0,num=i,value=serials[i])+"\n\n")
       #**I try to this command but it is useless !** 
       tn.write(COPYTXT.format(pon=0,num=i,value=serials[i]).encode('utf-8') + b"\n\n")
       time.sleep(.1)
      

print("-"*82)
print("SN Numbers is: ",serials)


0

There are 0 answers