hi everyone i have py script like:
import re
import os
import sys
import time
import telnetlib
class ZteC320():
def init(self):
self.zte_unregistered_onts = {}
self.setup_logging()
print('nProgram for auto-registration ONU for gpon zte c320n')
def setup_logging(self):
self.log_file = open('ont_config.log', 'a')
def log_successful_config(self, serial_number, port, onu_index):
log_message = f"Successfully configured serial {serial_number} on port gpon-olt_{port} with index {onu_index}n"
self.log_file.write(log_message)
self.log_file.flush() # Ensure the log is written immediately
def info(self):
for gpon_port in self.zte_unregistered_onts:
print('nOn port gpon_olt_' + gpon_port + ' serial numbers were found: ' + ', '.join(self.zte_unregistered_onts[gpon_port]['serials']))
def zte_c320_ping(self):
response = os.system('ping 10.3.1.10 -n 1')
if response == 0:
print('nnOLT Tersedia, Mulain')
time.sleep(1)
return True
else:
print('nnGPON - 10.3.1.10 does not respond to icmp requests.n')
time.sleep(1)
return False
def zte_telnet_authentication(self):
self.__login = 'username'
self.__password = 'password'
self.zte_cli_in = telnetlib.Telnet('ipTELNET', 23, 2)
time.sleep(.1)
self.zte_cli_out = self.zte_cli_in.read_until(b'Username:', timeout=3)
if self.zte_cli_out:
self.zte_cli_in.write(self.__login.encode('ascii') + b'n')
time.sleep(.5)
self.zte_cli_out = self.zte_cli_in.read_until(b'Password:', timeout=3)
if self.zte_cli_out:
self.zte_cli_in.write(self.__password.encode('ascii') + b'n')
time.sleep(.5)
self.zte_cli_in.read_until(b'#' or b'>', timeout=3)
return True
def zte_vendor_validation(self):
self.zte_cli_in.write(b'show shelfn')
time.sleep(.3)
self.zte_cli_out = self.zte_cli_in.read_until(b'C320_SHELF', timeout=3)
if self.zte_cli_out:
return True
def zte_show_onu_uncfg(self):
i = 0
self.zte_cli_in.write(b'show gpon onu uncfgn')
time.sleep(2)
self.zte_cli_out = self.zte_cli_in.read_very_eager()
self.zte_uncfg_onts = re.findall(r'gpon-onu_(d/d/d):(d)s+(w+)', self.zte_cli_out.decode('ascii'))
if self.zte_uncfg_onts:
for string in self.zte_uncfg_onts:
current_gepon_port = string[0]
current_onu_number = string[1]
current_onu_serial = string[2]
self.zte_unregistered_onts.setdefault(current_gepon_port, {'serials': [], 'busy_onts_id': [], 'vlan': None})
self.zte_unregistered_onts[current_gepon_port]['serials'].append(current_onu_serial)
i += 1
print('nTotal ONU yang belum di config: ' + str(i) + 'n')
for gpon_port in self.zte_unregistered_onts:
print('Serial yang ditemukan pada port gpon-olt_' + gpon_port + ': ' + ', '.join(self.zte_unregistered_onts[gpon_port]['serials']))
return True
else:
print('Tidak ada onu yang belum di Confign')
sys.exit()
def zte_check_free_onu_id(self):
for key in self.zte_unregistered_onts.keys():
self.zte_cli_in.write(b' show running-config interface gpon-olt_' + key.encode('ascii') + b' | include typen')
time.sleep(2)
i = 0
while i < 3:
self.zte_cli_in.write(b' n')
time.sleep(.3)
i += 1
self.zte_cli_out = self.zte_cli_in.read_very_eager()
onts = re.findall(r'onu (d+) type (S+) sn (w+)', self.zte_cli_out.decode('ascii'))
if onts:
for string in onts:
current_onu_id = string[0]
current_onu_type = string[1]
current_onu_serial = string[2]
self.zte_unregistered_onts[key]['busy_onts_id'].append(current_onu_id)
return True
else:
print('Tidak ada ONU yang terdaftar pada port gpon-olt_' + key + '.')
sys.exit()
def get_pppoe_credentials_from_user(self):
self.pppoe_username = input('Masukkan username PPPoE: ')
self.pppoe_password = input('Masukkan password PPPoE: ')
def zte_auto_add_onts(self):
self.get_pppoe_credentials_from_user() # Meminta input dari pengguna
self.zte_successful_ont_counter = 0
self.zte_cli_in.write(b'configure terminaln')
time.sleep(1)
for gpon_port in self.zte_unregistered_onts.keys():
i = 1
for current_onu_serial in self.zte_unregistered_onts[gpon_port]['serials']:
for i in range(i, 129):
if str(i) not in self.zte_unregistered_onts[gpon_port]['busy_onts_id']:
self.zte_cli_in.write(b'interface gpon-olt_' + gpon_port.encode('ascii') + b'n')
time.sleep(1)
self.zte_cli_in.write(b'onu ' + str(i).encode('ascii') + b' type ALL sn ' + current_onu_serial.encode('ascii') + b'n')
time.sleep(1)
self.zte_cli_in.write(b'!n')
time.sleep(.3)
self.zte_cli_in.write(b'interface gpon-onu_' + gpon_port.encode('ascii') + b':' + str(i).encode('ascii') + b'n')
time.sleep(.3)
self.zte_cli_in.write(b'name onu_' + str(i).encode('ascii') + b'n')
time.sleep(.3)
self.zte_cli_in.write(b'description CONF by ACSn')
time.sleep(.3)
self.zte_cli_in.write(b'tcont 1 profile 1Gn')
time.sleep(.3)
self.zte_cli_in.write(b'gemport 1 tcont 1n')
time.sleep(.3)
self.zte_cli_in.write(b'switchport mode hybrid vport 1n')
time.sleep(.3)
self.zte_cli_in.write(b'service-port 1 vport 1 user-vlan 1038 vlan 1038 n')
time.sleep(.3)
self.zte_cli_in.write(b'!n')
time.sleep(.3)
self.zte_cli_in.write(b'pon-onu-mng gpon-onu_' + gpon_port.encode('ascii') + b':' + str(i).encode('ascii') + b'n')
time.sleep(.3)
self.zte_cli_in.write(b'wan-ip 1 mode pppoe username ' + self.pppoe_username.encode('ascii') + b' password ' + self.pppoe_password.encode('ascii') + b' vlan-profile pppoe host 1n')
time.sleep(.3)
self.zte_cli_in.write(b'wan-ip 1 ping-response enable traceroute-response enablen')
time.sleep(.3)
self.zte_cli_in.write(b'security-mgmt 1 state enable mode forward protocol web telnet tr069n')
time.sleep(.3)
self.zte_cli_in.write(b'service tr069 gemport 1 vlan 1038n')
time.sleep(.3)
self.zte_cli_in.write(b'tr069-mgmt 1 state unlockn')
time.sleep(.3)
self.zte_cli_in.write(b'tr069-mgmt 1 acs http://172.16.20.6:7547 validate basic username admin password adminn')
time.sleep(.3)
self.zte_cli_in.write(b'tr069-mgmt 1 tag pri 7 vlan 1038n')
time.sleep(.3)
self.zte_cli_in.write(b'endn')
time.sleep(.3)
self.zte_cli_in.write(b'wrn')
time.sleep(.3)
self.zte_successful_ont_counter += 1
self.log_successful_config(current_onu_serial, gpon_port, i)
print(f'Serial {current_onu_serial} berhasil dikonfigurasi pada index {i}')
break
if self.zte_successful_ont_counter == len(self.zte_unregistered_onts[gpon_port]['serials']):
print('ONUs Sukses di daftarkan silahkan cek GenieACS')
if self.zte_successful_ont_counter < len(self.zte_unregistered_onts[gpon_port]['serials']):
print('Beberapa ONU Belum Sukses di daftarkan.')
def zte_auto_add_onts_init(self):
while True:
if not self.zte_c320_ping():
input('nPress enter to exit')
sys.exit()
if not self.zte_telnet_authentication():
print('nTidak dapat Login ke OLTn')
sys.stdin.close()
sys.exit()
if not self.zte_vendor_validation():
print('nVendor mismatch for 10.3.1.10n')
sys.stdin.close()
sys.exit()
if not self.zte_show_onu_uncfg():
sys.stdin.close()
sys.exit()
if not self.zte_check_free_onu_id():
print('Error checking free ONU IDn')
sys.stdin.close()
sys.exit()
if not self.zte_auto_add_onts():
sys.stdin.close()
sys.exit()
if name == ‘main‘:
zte_c320 = ZteC320()
while True:
zte_c320.zte_auto_add_onts_init()
Its runing normaly when i manualed execute script, my question how to execute the script from telegram bot with username and ppoe pasword from user and send all respond to user?
i’ve been try with telegram-bot-python and im stuck. any other ways to fix that?
user25964160 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.