Advertisement
Guest User

Untitled

a guest
Aug 17th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.80 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # coding: u8
  3.  
  4. import sys
  5. sys.path.append('../modules')
  6.  
  7. from setup_locale import *
  8. import mikrotik
  9.  
  10. import os
  11. import paramiko
  12. import cStringIO as StringIO
  13. import re
  14. import linecache
  15. import traceback
  16.  
  17. def _patched_exec_command(self,
  18.                         command,
  19.                         bufsize=-1,
  20.                         timeout=None,
  21.                         get_pty=False,
  22.                         stdin_binary=True,
  23.                         stdout_binary=False,
  24.                         stderr_binary=False):
  25.  
  26.     chan = self._transport.open_session()
  27.     if get_pty:
  28.         chan.get_pty()
  29.     chan.settimeout(timeout)
  30.     chan.exec_command(command)
  31.     stdin = chan.makefile('wb' if stdin_binary else 'w', bufsize)
  32.     stdout = chan.makefile('rb' if stdin_binary else 'r', bufsize)
  33.     stderr = chan.makefile_stderr('rb' if stdin_binary else 'r', bufsize)
  34.     return stdin, stdout, stderr
  35.  
  36. paramiko.SSHClient.exec_command = _patched_exec_command
  37.  
  38. def printException():
  39.     exc_type, exc_obj, tb = sys.exc_info()
  40.     f = tb.tb_frame
  41.     lineno = tb.tb_lineno
  42.     filename = f.f_code.co_filename
  43.     linecache.checkcache(filename)
  44.     line = linecache.getline(filename, lineno, f.f_globals)
  45.     print 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj)
  46.     traceback.print_exc(file=sys.stdout)
  47.  
  48. def translate(x):
  49.     l = re.findall(r'\\[0-9a-fA-F][0-9a-fA-F]', x)
  50.     for e in l:
  51.         x = x.replace(e, chr(eval('0x' + e[1:])))
  52.     return x
  53.  
  54. def flatten_cmd(cmd):
  55.     return cmd.replace('\r', '').replace('\n', ' ; ')
  56.  
  57. def flatten_out(out):
  58.     return out # при использовании print detail without-paging перенос через знак \ не делается
  59.  
  60. def dump(cmd, out):
  61.     print 'cmd: ', cmd
  62.     print 'out: ', out
  63.  
  64. def inject(address, username, password, port):
  65.     ssh = paramiko.SSHClient()
  66.     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  67.     ssh.connect(address, username = username, password = password, port = port, timeout = 2)
  68.  
  69.     I,O,E = ssh.exec_command(':put [/system package get system version]')
  70.     version = float(re.findall(r'^\d+.\d+', O.read())[0])
  71.  
  72.     rn = '\r\n'
  73.     cmd = '/ip dhcp-client' + rn + 'print detail without-paging'
  74.     out = ssh.exec_command(flatten_cmd(cmd))[1].read()
  75.     dump(flatten_cmd(cmd), out)
  76.  
  77.     pairs = re.findall(r'interface=.*?\s.*?(?=address=).*?\s', out, re.DOTALL)
  78.     for i in pairs:
  79.         iface = re.findall(r'interface=(.+?)\s', i)[0]
  80.         addr = re.findall(r'address=(\d+\.\d+\.\d+\.\d+)', i)[0]
  81.         cmd = '/interface print detail without-paging where name=' + iface
  82.         out = ssh.exec_command(cmd)[1].read()
  83.         dump(cmd, out)
  84.         comm = re.findall(r';;;\s(.*)', out)
  85.         comm = comm[0] if comm else ''
  86.         print 'fetch comment: ', comm
  87.         if addr in comm:
  88.             print 'comment already has true ip address, skipping'
  89.             continue
  90.         comm = (' | ' + comm) if comm else ''
  91.         comm = addr + comm
  92.         cmd = '/interface set [find name=%s] comment="%s"' % (iface, comm)
  93.         out = ssh.exec_command(cmd)[1].read()
  94.         dump(cmd, out)
  95.  
  96.  
  97.     ssh.close()
  98.  
  99.  
  100. #re.findall(r'\d+\.\d+\.\d+\.\d+', open('pwd.txt').read())
  101. 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']
  102. pwds = [['chubatov', 'kJ3LmzVR']] * (len(mts)-1) + [['admin', 'admin33321']]
  103.  
  104. for i in xrange(len(mts)):
  105.     addr = mts[i]
  106.     login, pwd = pwds[i]
  107.     for port in [4]:
  108.         try:
  109.             print 'connecting to ', addr
  110.             inject(addr, login, pwd, port)
  111.         except Exception, msg:
  112.             print 'error, MT: %s:%s' % (addr, port)
  113.             printException()
  114.  
  115. print
  116. print 'press enter...'
  117. raw_input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement