This is a script to telnet to CMTS-Cisco server to get ipv6 address
The different between python2 and python3 is the byte and str
PYTHON3 we need to use tn.read_until(b"Username:")
PYTHON2 we don't need byte tn.read_until("Username:")
since we need to used byte, so we have to convert to str using decode
we can use decode utf-8 or big5, depend on our OS. If you used CHINESE you have to used BIG5, else ping will not show correct.
Manual Test:
1.Login to CMTS/Cisco Server
2. used the command to see your ipv6 addres: scm XXX.XXX.XXXX ipv6
1)Method 1 python3 just telnet and get the ipv6 of specfic mac
import getpass import re import telnetlib HOST = "192.168.1.252" #user = input("Enter your remote account: ") #password = getpass.getpass() user='guest' password='guest' tn = telnetlib.Telnet(HOST) tn.read_until(b"Username:") tn.write(user.encode('ascii') + b"\n") if password: tn.read_until(b"Password:") tn.write(password.encode('ascii') + b"\n") value = tn.read_until(b"Router#") tn.write(b"scm mac = "AAAA.BBBB.CCCC" ipv6 \n") value = tn.read_until(b"Router#") ####################################################### value=value.decode('utf8') info = "2001" matchObj = re.match(r'.*'+ info + '(.*)\n',value, re.M|re.DOTALL) #matchObj=matchObj.decode('big5') if matchObj: Ipv6_address = info + matchObj.group(1) Ipv6 = Ipv6_address.replace("\n", "") print(Ipv6) #return Ipv6 else: print ("No match!!") #################################################### tn.write(b"exit\n") #print(tn.read_all().decode('ascii'))
2)Method 2 using function and check telnet reachable or not(python3)
# -*- coding: utf-8 -*-
import telnetlib
import subprocess
import time
import re
def Telnet_Check_reachability(ip):
ping_count=3
process = subprocess.Popen(['ping', ip, '-n', str(ping_count)],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
process.wait()
stdout = process.stdout.read()
stdout=stdout.decode("big5")
#print stdout
if "TTL=" in stdout:
#print "Server reachable"
successful = 1
else:
#print "Server unreachable"
successful = 0
return successful
def Login_Telnet(HOST,username,password):
try:
tn=""
reachability=Telnet_Check_reachability(HOST)
if (reachability==1):
tn = telnetlib.Telnet(HOST,23)
tn.read_until(b"Username:")
#tn.write(username + "\n")
tn.write(username.encode('ascii') + b"\n")
if password:
tn.read_until(b"Password:")
#tn.write(password + "\n")
tn.write(password.encode('ascii') + b"\n")
time.sleep(3)
return tn
except IOError:
print ("Telnet " + HOST + " failed. Please check the server connection")
def telnet_To_CMTS(Client_IP, Client_Name, Client_Pwd, MAC):
tn =Login_Telnet(Client_IP, Client_Name, Client_Pwd)
if "telnetlib" in str(tn):
time.sleep(1)
value = tn.read_until(b"Router#")
command = "scm " + MAC + " ipv6\n"
tn.write(command.encode('ascii') + b"\n")
#tn.write(command)
value = tn.read_until(b"Router#")
#print value
tn.close()
time.sleep(1)
info = "2001"
#value=str(value)
value=value.decode('utf8')
matchObj = re.match(r'.*'+ info + '(.*)\n',value, re.M|re.DOTALL)
if matchObj:
Ipv6_address = info + matchObj.group(1)
Ipv6 = Ipv6_address.replace("\n", "")
return Ipv6
else:
print ("No match!!")
else:
print ("Telnet failed")
ip ="192.168.1.252"
username = "guest"
password = "guest"
mac = "AAAA.BBBB.CCCC"
new_IPv6 = telnet_To_CMTS(ip, username, password, mac)
print (new_IPv6)
3)Method 3 same as method2 using python2
# -*- coding: utf-8 -*-
import telnetlib
import subprocess
import time
import re
def Telnet_Check_reachability(ip):
ping_count=3
process = subprocess.Popen(['ping', ip, '-n', str(ping_count)],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
process.wait()
stdout = process.stdout.read()
#print stdout
if "TTL=" in stdout:
#print "Server reachable"
successful = 1
else:
#print "Server unreachable"
successful = 0
return successful
def Login_Telnet(HOST,username,password):
try:
tn=""
reachability=Telnet_Check_reachability(HOST)
if (reachability==1):
tn = telnetlib.Telnet(HOST,23)
tn.read_until("Username:")
tn.write(username + "\n")
if password:
tn.read_until("Password:")
tn.write(password + "\n")
time.sleep(3)
return tn
except IOError:
print "Telnet " + HOST + " failed. Please check the server connection"
def telnet_To_CMTS(Client_IP, Client_Name, Client_Pwd, MAC):
tn =Login_Telnet(Client_IP, Client_Name, Client_Pwd)
if "telnetlib" in str(tn):
time.sleep(1)
value = tn.read_until("Router#")
command = "scm " + MAC + " ipv6\n"
tn.write(command)
value = tn.read_until("Router#")
#print value
tn.close()
time.sleep(1)
info = "2001"
matchObj = re.match(r'.*'+ info + '(.*)\n',value, re.M|re.DOTALL)
if matchObj:
Ipv6_address = info + matchObj.group(1)
Ipv6 = Ipv6_address.replace("\n", "")
return Ipv6
else:
print "No match!!"
else:
print "Telnet failed"
ip ="192.168.1.252"
username = "guest"
password = "guest"
mac = "AAAA.BBBB.CCCC"
new_IPv6 = telnet_To_CMTS(ip, username, password, mac)
print new_IPv6
No comments:
Post a Comment