Skip to content

Instantly share code, notes, and snippets.

@mipsparc

mipsparc/ix_uptime.py

Last active Apr 5, 2016
Embed
What would you like to do?
NEC IXシリーズのルータから自動でuptimeを取得するやつ。実行にはPython3が必要。IX2015以外かホスト名を変更している場合は、headerを変更すること。ex)$ ./ix_uptime.py 192.168.0.1 username password
#!/usr/bin/env python3
#ex) $ ix_uptime.py 192.168.0.1 username password
import getpass
from telnetlib import Telnet
from sys import argv
header = "IX2015# "
command = "show uptime\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')
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")
if password:
tn.read_until(b"Password: ")
tn.write(password + b"\r")
tn.read_until(header.encode("ascii"))
tn.write("\r")
tn.write(command.encode("ascii"))
tn.write(b"exit\r")
raw_msg = tn.read_all().decode('ascii')
#delete header
def check_header(txt):
if txt.find(header) == -1:
return txt.replace("\r", "\n")
else:
return ''
clear_msg = ''.join(list(map(check_header, raw_msg.split('\n'))))
print(clear_msg)
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.