Skip to content

Instantly share code, notes, and snippets.

@tuxmartin
Last active October 27, 2017 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuxmartin/07d722c0c17815743420b427edeb7ff4 to your computer and use it in GitHub Desktop.
Save tuxmartin/07d722c0c17815743420b427edeb7ff4 to your computer and use it in GitHub Desktop.
Test mikrotik api
#!/usr/bin/python3
# https://github.com/vshn/tikapy
# sudo pip3 install tikapy
# python3 miktotik-tikapy.py "192.168.88.1" "admin" ""
import sys
from tikapy import TikapySslClient
from pprint import pprint
ip = sys.argv[1]
user = sys.argv[2]
password = sys.argv[3]
port = 8729
client = TikapySslClient(ip, port)
client.login(user, password)
print("________________________________________ /ip service print where name=ssh")
# /ip service print where name=ssh
pprint( client.talk(['/ip/service/print', '?name=ssh']) )
print("________________________________________/ip firewall filter print stats")
# /ip firewall filter print stats
pprint( client.talk(["/ip/firewall/filter/print"]) )
print("________________________________________/ip firewall filter print stats chain=output")
# /ip firewall filter print stats chain=output
pprint( client.talk(["/ip/firewall/filter/print", "?chain=output"]) )
print("________________________________________/system backup save name=backup123 password=""")
# /system backup save name=backup123 password=""
file_name="backup123"
pprint( client.talk(["/system/backup/save", "=name=%s" % file_name, '=password=""']) )
print("________________________________________")
/usr/bin/python3.5 /home/martin/PycharmProjects/test/miktotik-tikapy.py
________________________________________ /ip service print where name=ssh
{'4': {'.id': '*4',
'address': '',
'disabled': 'false',
'invalid': 'false',
'name': 'ssh',
'port': '22'}}
________________________________________/ip firewall filter print stats
{'1': {'.id': '*1',
'action': 'accept',
'bytes': '56',
'chain': 'input',
'comment': 'defconf: accept ICMP',
'disabled': 'false',
'dynamic': 'false',
'invalid': 'false',
'log': 'false',
'log-prefix': '',
'packets': '1',
'protocol': 'icmp'},
'2': {'.id': '*2',
'action': 'accept',
'bytes': '2654311',
'chain': 'input',
'comment': 'defconf: accept establieshed,related',
'connection-state': 'established,related',
'disabled': 'false',
'dynamic': 'false',
'invalid': 'false',
'log': 'false',
'log-prefix': '',
'packets': '33782'},
'3': {'.id': '*3',
'action': 'drop',
'bytes': '107229',
'chain': 'input',
'comment': 'defconf: drop all from WAN',
'disabled': 'false',
'dynamic': 'false',
'in-interface': 'ether1',
'invalid': 'false',
'log': 'false',
'log-prefix': '',
'packets': '1423'},
'4': {'.id': '*4',
'action': 'fasttrack-connection',
'bytes': '0',
'chain': 'forward',
'comment': 'defconf: fasttrack',
'connection-state': 'established,related',
'disabled': 'false',
'dynamic': 'false',
'invalid': 'false',
'log': 'false',
'log-prefix': '',
'packets': '0'},
'5': {'.id': '*5',
'action': 'accept',
'bytes': '0',
'chain': 'forward',
'comment': 'defconf: accept established,related',
'connection-state': 'established,related',
'disabled': 'false',
'dynamic': 'false',
'invalid': 'false',
'log': 'false',
'log-prefix': '',
'packets': '0'},
'6': {'.id': '*6',
'action': 'drop',
'bytes': '0',
'chain': 'forward',
'comment': 'defconf: drop invalid',
'connection-state': 'invalid',
'disabled': 'false',
'dynamic': 'false',
'invalid': 'false',
'log': 'false',
'log-prefix': '',
'packets': '0'},
'7': {'.id': '*7',
'action': 'drop',
'bytes': '0',
'chain': 'forward',
'comment': 'defconf: drop all from WAN not DSTNATed',
'connection-nat-state': '!dstnat',
'connection-state': 'new',
'disabled': 'false',
'dynamic': 'false',
'in-interface': 'ether1',
'invalid': 'false',
'log': 'false',
'log-prefix': '',
'packets': '0'},
'8': {'.id': '*8',
'bytes': '0',
'chain': 'forward',
'comment': 'special dummy rule to show fasttrack counters',
'dynamic': 'true',
'packets': '0'},
'9': {'.id': '*9',
'action': 'accept',
'bytes': '175344',
'chain': 'input',
'comment': 'API',
'disabled': 'false',
'dst-port': '8729',
'dynamic': 'false',
'invalid': 'false',
'log': 'false',
'log-prefix': '',
'packets': '1178',
'protocol': 'tcp'},
'B': {'.id': '*B',
'action': 'reject',
'bytes': '0',
'chain': 'output',
'disabled': 'false',
'dynamic': 'false',
'invalid': 'false',
'log': 'false',
'log-prefix': '',
'packets': '0',
'reject-with': 'icmp-network-unreachable',
'src-address': '1.2.3.4'}}
________________________________________/ip firewall filter print stats chain=output
{'B': {'.id': '*B',
'action': 'reject',
'bytes': '0',
'chain': 'output',
'disabled': 'false',
'dynamic': 'false',
'invalid': 'false',
'log': 'false',
'log-prefix': '',
'packets': '0',
'reject-with': 'icmp-network-unreachable',
'src-address': '1.2.3.4'}}
________________________________________/system backup save name=backup123 password=
{}
________________________________________
Process finished with exit code 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment