Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- # coding: u8
- import sys
- sys.path.append('../modules')
- from setup_locale import *
- import mikrotik
- import os
- import paramiko
- import cStringIO as StringIO
- import re
- import linecache
- import traceback
- def _patched_exec_command(self,
- command,
- bufsize=-1,
- timeout=None,
- get_pty=False,
- stdin_binary=True,
- stdout_binary=False,
- stderr_binary=False):
- chan = self._transport.open_session()
- if get_pty:
- chan.get_pty()
- chan.settimeout(timeout)
- chan.exec_command(command)
- stdin = chan.makefile('wb' if stdin_binary else 'w', bufsize)
- stdout = chan.makefile('rb' if stdin_binary else 'r', bufsize)
- stderr = chan.makefile_stderr('rb' if stdin_binary else 'r', bufsize)
- return stdin, stdout, stderr
- paramiko.SSHClient.exec_command = _patched_exec_command
- def printException():
- exc_type, exc_obj, tb = sys.exc_info()
- f = tb.tb_frame
- lineno = tb.tb_lineno
- filename = f.f_code.co_filename
- linecache.checkcache(filename)
- line = linecache.getline(filename, lineno, f.f_globals)
- print 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj)
- traceback.print_exc(file=sys.stdout)
- def translate(x):
- l = re.findall(r'\\[0-9a-fA-F][0-9a-fA-F]', x)
- for e in l:
- x = x.replace(e, chr(eval('0x' + e[1:])))
- return x
- def flatten_cmd(cmd):
- return cmd.replace('\r', '').replace('\n', ' ; ')
- def flatten_out(out):
- return out # при использовании print detail without-paging перенос через знак \ не делается
- def dump(cmd, out):
- print 'cmd: ', cmd
- print 'out: ', out
- def inject(address, username, password, port):
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect(address, username = username, password = password, port = port, timeout = 2)
- I,O,E = ssh.exec_command(':put [/system package get system version]')
- version = float(re.findall(r'^\d+.\d+', O.read())[0])
- rn = '\r\n'
- cmd = '/ip dhcp-client' + rn + 'print detail without-paging'
- out = ssh.exec_command(flatten_cmd(cmd))[1].read()
- dump(flatten_cmd(cmd), out)
- pairs = re.findall(r'interface=.*?\s.*?(?=address=).*?\s', out, re.DOTALL)
- for i in pairs:
- iface = re.findall(r'interface=(.+?)\s', i)[0]
- addr = re.findall(r'address=(\d+\.\d+\.\d+\.\d+)', i)[0]
- cmd = '/interface print detail without-paging where name=' + iface
- out = ssh.exec_command(cmd)[1].read()
- dump(cmd, out)
- comm = re.findall(r';;;\s(.*)', out)
- comm = comm[0] if comm else ''
- print 'fetch comment: ', comm
- if addr in comm:
- print 'comment already has true ip address, skipping'
- continue
- comm = (' | ' + comm) if comm else ''
- comm = addr + comm
- cmd = '/interface set [find name=%s] comment="%s"' % (iface, comm)
- out = ssh.exec_command(cmd)[1].read()
- dump(cmd, out)
- ssh.close()
- #re.findall(r'\d+\.\d+\.\d+\.\d+', open('pwd.txt').read())
- mts = ['5.8.200.9', '5.8.200.39', '5.8.200.172', '5.8.200.206', '5.8.201.15', '5.8.201.71', '5.8.202.242', '5.8.203.136', '5.8.203.149', '5.8.203.152', '5.8.203.158', '5.8.203.165', '5.8.203.218', '5.8.203.234', '5.8.204.35', '5.8.204.157', '5.8.204.146', '5.8.205.71', '5.8.205.200', '5.8.205.223', '5.8.206.140', '5.8.206.204', '5.8.207.35', '5.8.207.40', '5.8.207.68', '5.8.207.160', '5.8.207.197', '5.8.208.138', '5.8.212.14', '5.8.203.94', '5.8.202.218', '5.8.203.160', '5.8.205.137', '5.8.202.211', '95.83.150.154']
- pwds = [['chubatov', 'kJ3LmzVR']] * (len(mts)-1) + [['admin', 'admin33321']]
- for i in xrange(len(mts)):
- addr = mts[i]
- login, pwd = pwds[i]
- for port in [4]:
- try:
- print 'connecting to ', addr
- inject(addr, login, pwd, port)
- except Exception, msg:
- print 'error, MT: %s:%s' % (addr, port)
- printException()
- print
- print 'press enter...'
- raw_input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement