Advertisement
Guest User

bino : pull Mikrotik wireless registration table

a guest
Dec 17th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. #by Bino Oetomo
  2. #save it to mtwlstat.py
  3. #call it --> python mtwlstat a:b@host:port, where
  4. #++ a = your API user name
  5. #++ b = your API passwword
  6. #++ host = your mikrotik AP address
  7. #++ port = your mikrotik AP API port
  8.  
  9. #REQUIRE = librouteros ( https://pypi.python.org/pypi/librouteros/ )
  10.  
  11.  
  12. import time
  13. from librouteros import connect
  14. import re
  15.  
  16.  
  17. class wlstat():
  18.     def __init__(self, inCred):
  19.         incred, inhost = inCred.strip().split('@')
  20.         self.user, self.password = incred.strip().split(':')
  21.         self.user = self.user.strip()
  22.         self.password = self.password.strip()
  23.         self.host = inhost.strip().split(':')[0].strip()
  24.         try :
  25.             self.port = inhost.strip().split(':')[1].strip()
  26.         except :
  27.             self.port = '8728'
  28.  
  29.     def poll(self) :
  30.         ts = int(time.time())
  31.         api = connect(host=self.host, port=self.port, username=self.user, password=self.password)
  32.         cmd = '/ip/neighbor/print'
  33.         results = api(cmd=cmd)
  34.         neighbors = {}
  35.  
  36.         cmd = '/interface/wireless/registration-table/print'
  37.         results = api(cmd=cmd)
  38.         clients = []
  39.         for r in results :
  40.             thisClient={}
  41.             thisClient['clientMAC'] = r['mac-address']
  42.             thisClient['clientName'] = r['radio-name']
  43.             thisClient['clientSignal'] = int(r['signal-strength'].split('@')[0])
  44.             thisClient['clientNoise'] = thisClient['clientSignal'] - r['signal-to-noise']
  45.             thisClient['clientDistance'] = r['distance']
  46.             thisClient['clientTxCCQ'] = r['tx-ccq']
  47.             thisClient['clientRxCCQ'] = r['rx-ccq']
  48.             thisClient['pollTS'] = ts
  49.             try :
  50.                 thisClient['clientIP'] = neighbors[str(thisClient['clientMAC'])]
  51.             except :
  52.                 thisClient['clientIP'] = 'unknown'
  53.             clients.append(thisClient)
  54.         return clients
  55.  
  56.  
  57. if __name__ == "__main__":
  58.    
  59.     import sys
  60.     inTarget = sys.argv[1].strip()
  61.     try :
  62.         inSleep = int(sys.argv[2].strip())
  63.     except :
  64.         inSleep = 10
  65.     poller = wlstat(inTarget)
  66.     while True :
  67.         for d in poller.poll() :
  68.             print d
  69.         time.sleep(inSleep)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement