Last active
April 5, 2016 15:25
-
-
Save mipsparc/212bbf4a390ad94b7f2739dda330ded6 to your computer and use it in GitHub Desktop.
NEC IXシリーズのルータから自動でuptimeを取得するやつ。実行にはPython3が必要。IX2015以外かホスト名を変更している場合は、headerを変更すること。ex)$ ./ix_uptime.py 192.168.0.1 username password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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