Created
January 20, 2014 15:37
-
-
Save bibineko/8522140 to your computer and use it in GitHub Desktop.
pexpectを使ってyamaha routerからsshで情報を取ってくる
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 python | |
# -*- coding:utf-8 -*- | |
""" [NAME] に続くScriptの簡易説明文 | |
[DESCRIPTION] Scriptの詳細説明文 | |
ユーザ名とパスワードの保存にpitを使っている。 | |
username: sshのユーザ | |
password: パスワード | |
adminpassword: 特権パスワード | |
pitのインストールは次のようにする。 | |
$ sudo pip install pit | |
$ export EDITOR=vim | |
$ python | |
-- in python shell -- | |
from pit import Pit | |
Pit.get('label名', {'require':{'username':'your USERNAME','password':'your PASSORD','adminpassword':'adminpass'}}) | |
-- from here -- | |
""" | |
__author__ = 'Tomofumi Oga <bibineko@gmail.com>' | |
__version__ = '0.1' | |
import datetime | |
import pexpect | |
import sys | |
import tempfile | |
from pit import Pit | |
class Bpex(pexpect.spawn): | |
""" | |
[CLASSES] Class説明文 | |
""" | |
address = None | |
logfile_name = None | |
last_log = None # 最後に実行したコマンドのログ | |
def output(self): | |
""" [CLASSES] 標準出力に | |
""" | |
self.temp_file = tempfile.TemporaryFile() | |
self.logfile_read = self.temp_file | |
def stdout(self): | |
""" [CLASSES] ログを標準出力から吐き出す。 | |
""" | |
self.logfile_read = sys.stdout | |
def sende(self, cmd): | |
"""[CLASSES] sende | |
cmd - 実行コマンド | |
コマンドを送信してecho-backを待つ。 | |
""" | |
self.sendline(cmd) | |
return self.expect(cmd) | |
def sendep(self, cmd, pr = [u"\$", u"#", u">"]): | |
"""[CLASSES] | |
[DESCRIPTION] | |
1行送信して、プロンプトを待つ。 | |
コマンド実行結果を.last_logに保存する。 | |
""" | |
if self.logfile_read: | |
f = self.logfile_read | |
i = f.tell() | |
self.sende(cmd) | |
res = self.expect(pr) | |
if self.logfile_read: | |
f.seek(i) | |
self.last_log = f.read() | |
return res | |
def sendln(self, *args, **kwargs): | |
""" | |
echo-backを待つ。 | |
""" | |
try: | |
self.sendline(args, kwargs) | |
self.expect(args[0] + u"\n") | |
except: | |
raise | |
def login_rtx_defe( self , address ): | |
"""[CLASS] login_rtx_defe | |
[DESCRIPTION] | |
デフォルトのログイン情報を使ってログイン | |
""" | |
conf = Pit.get('ssh-logins') # Pitでログイン情報を取得 | |
username = conf['username'] # user名 | |
passwd = conf['password'] # password | |
adminpassword = conf['adminpassword'] # password | |
res_login = -1 | |
while res_login < 0: | |
self.flush() #h鵜ラッシュ | |
cmd = 'ssh ' + username + u'@' + address | |
self.sende(cmd) | |
res = 0 | |
while res == 0: | |
res = self.expect(['connecting','ssword:']) | |
if res == 0: | |
# print u'add ssh key' | |
self.sendln(u'yes') | |
else: | |
# print u"send password" | |
# print res | |
self.sendline(passwd) | |
try: | |
# print "wait rtx" | |
res_login = self.expect([u'RTX1200',u'RTX810']) | |
# print res_login | |
except: | |
res_login = -1 | |
# print "wiat >" | |
self.expect('>') | |
self.sende('administrator') # 特権ユーザになる | |
self.expect('Password') | |
self.sendline(adminpassword) | |
self.expect('#') # adminプロンプトを待つ | |
self.flush() | |
return self | |
@classmethod | |
def login_rtx_def( self , address, logfile=None ): | |
pex = Bpex('bash') # shell | |
pex.address = address | |
if logfile != None: | |
pex.logfile_read = logfile | |
else: | |
pex.set_default_log() | |
pex.login_rtx_defe(address) | |
return pex | |
def logout_rtx(self, save_config = 'Y'): | |
""" [CLASSES] 関数説明文 | |
Keyword arguments: | |
hoge -- 引数説明 | |
""" | |
res = 1 | |
while res != 0: | |
self.flush() | |
self.sendline('') | |
res = self.expect(['>','#']) | |
if res == 1: | |
self.sende('exit') | |
res2 = self.expect(['>','(Y/N)']) | |
if res2 == 1: | |
self.sendline(save_config) | |
self.sende('exit') | |
self.expect('closed') | |
self.logfile_read.close() | |
def set_default_log(self, prefix=u'log/'): | |
""" [CLASSES] set_default_log | |
[DESCRIPTION] | |
ログファイルに設定する。 | |
""" | |
s = prefix + self.address + u"_" + datetime.datetime.now().strftime(u"%Y%m%d-%H%M%S") + u".txt" | |
self.logfile_name = s | |
f = open(s, "w+") | |
# self.logfile = f | |
self.logfile_read = f | |
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
# -*- coding: utf-8 -*- | |
""" [NAME] cron | |
[DESCRIPTION] すべてのルータからコマンドを取得 | |
""" | |
__author__ = 'Tomofumi Oga <bibineko@gmail.com>' | |
__version__ = '0.1' | |
from django.core.management.base import BaseCommand | |
from zabbix_worker.models import Gateway | |
from zabbix_worker.bpex import Bpex | |
class Command(BaseCommand): | |
def handle(self, *args, **options): | |
"""[NAME] handle | |
[DESCRIPTION] | |
""" | |
for gateway in Gateway.objects.all(): | |
pex = Bpex.login_rtx_def( gateway.ip_addr) | |
pex.sendep("show status ospf nei") | |
i = pex.last_log.count(u"FULL/") | |
print gateway.ip_addr + " ospf_neighbors " + str( i ) | |
for line in pex.last_log.split("\n"): | |
if line.find("FULL/") >= 0: | |
print gateway.ip_addr + " ospf_peer " + line.strip().split(" ")[0] | |
pex.sendep("show ipv6 address") | |
for line in pex.last_log.split("\n"): | |
if line.find("global") >= 0: | |
print gateway.ip_addr + " ipv6_addr " + line.strip().replace(" ","").split(" ")[1] | |
pex.logout_rtx(u'N') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment