Skip to content

Instantly share code, notes, and snippets.

@mipsparc mipsparc/ix_tools.py

Last active Apr 23, 2016
Embed
What would you like to do?
NEC IXシリーズのルータから諸情報を取得するやつ。実行にはPython3が必要。IX2015以外かホスト名を変更している場合は、prompt1,2を変更すること。ex)$ ./ix_uptime.py 192.168.0.1 username password
#!/usr/bin/env python3
from telnetlib import Telnet
from sys import argv
from time import sleep
prompt1 = b"IX2015# "
prompt2 = b"IX2015(config)# "
command1 = b"show uptime\r"
command2 = b"show arp entry\r"
command3 = b"en\rshow ip dhcp lease\rexit\r"
command4 = b"show clock\r"
command5 = b"show env\r"
#HOST = input("Enter your IX\'s IP addr: ")
#user = input("Enter your remote account: ").encode('ascii')
#password = getpass.getpass().encode("ascii")
HOST = argv[1]
user = argv[2].encode('ascii')
password = argv[3].encode('ascii')
def send_command(command):
#delete prompt
def check_prompt(txt):
if txt.find(prompt1.decode("ascii"))==\
txt.find(prompt2.decode("ascii"))== -1:
return txt.replace("\r", "\n")
else:
return ''
tn.write(command)
raw_msg = tn.read_until(prompt1).decode('ascii')
#remove the first line
clear_msg = ''.join(list(map(check_prompt, raw_msg.split('\n')[1:])))
return clear_msg
tn = Telnet()
#block any option
tn.set_option_negotiation_callback(lambda x,y,z:None)
tn.open(HOST)
#login and cut welcome messages
tn.read_until(b"login: ")
tn.write(user + b"\r")
tn.read_until(b"Password: ")
tn.write(password + b"\r")
tn.read_until(prompt1)
tn.write(b"\r")
tn.read_until(prompt1)
print("-----clock-----")
print(send_command(command4))
print("-----uptime-----")
print(send_command(command1))
print("-----env-----")
print(send_command(command5))
print("\n-----ARP-----")
print(send_command(command2))
print("\n-----DHCP-----")
print(send_command(command3))
tn.write(b"exit\r")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.