#!/usr/bin/python3

import sys
import telnetlib
import time
import re

HOST = "localhost"
PORT = "5657"
USERNAME = "admin"
PASSWORD = "asc@t185"

tn = telnetlib.Telnet(HOST, PORT, timeout=15)

if b'Username:' in tn.read_until(b'Username:', timeout=5):
    tn.write(bytes(USERNAME, 'ascii') + b"\r\n")
    
    if b'Password:' in tn.read_until(b'Password:', timeout=5):
        time.sleep(0.2)
        tn.write(bytes(PASSWORD, 'ascii') + b"\r\n")
        
        time.sleep(1)
        tn.write(bytes("sh", 'ascii') + b"\r\n")
        
        time.sleep(1)
        tn.write(bytes("pppoe show", 'ascii') + b"\r\n")
        
        time.sleep(1)
        tn.write(bytes("exit", 'ascii') + b"\r\n")
        
        result_tmp = tn.read_all().decode('ascii').splitlines()
        
        re_ip = re.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
        ips = re.findall(re_ip, result_tmp[len(result_tmp) - 2])
        
        print(ips)
