Package: routeros-ssh-connector
MikroTik RouterOS SSH connector for PythonFeaturesA python-based SSH API for MikroTik devicesThis API allows you to get, update and create configuration on MikroTik devices plus other some extra utilities.This project is still in development and will include new functionalities in the future.Installationpip install routeros_ssh_connectorUsage1. Import modulefromrouteros_ssh_connectorimportMikrotikDevice2.  Create a new instance of themrouter=MikroTikDevice()3.  Connect to devicerouter.connect("ip_address","username","password","port")NOTE: If 'port' parameter is not passed to method the default value is 224. Call any of the following available methodsGETUPDATECREATETOOLSget_export_configurationupdate_address_poolcreate_address_poolconfigure_wlanget_identityupdate_dhcp_clientcreate_dhcp_clientdownload_backupget_interfacesupdate_dhcp_server_networkcreate_dhcp_serverdownload_exportget_ip_addressesupdate_dhcp_server_servercreate_ip_addressdownload_fileget_resourcesupdate_identitycreate_routeenable_cloud_dnsget_routesupdate_ip_addresscreate_usermake_backupget_servicesupdate_servicesreboot_deviceget_usersupdate_usersend_command.update_system.upload_fileinterfaces=router.get_interfaces()print(interfaces)5.  Disconnect from devicerouter.disconnect()6.  Deleterouterobject (optional)delrouterExamplesGet interfaces from devicefromrouteros_ssh_connectorimportMikrotikDevicerouter=MikrotikDevice()router.connect("10.0.0.1","myuser","strongpassword",2222)interfaces=router.get_interfaces()print(interfaces)router.disconnect()delrouterOutput returns a list containing so many dictionaries as interfaces are found in device :[{'status': 'running', 'name': 'ether1', 'default-name': 'ether1', 'type': 'ether', 'mtu': '1500', 'mac_address': 'AA:BB:CC:DD:EE:F0'}, {'status': 'disabled', 'name': 'pwr-line1', 'default-name': 'pwr-line1', 'type': 'ether', 'mtu': '1500', 'mac_address': 'AA:BB:CC:DD:EE:F1'}, {'status': 'disabled', 'name': 'wlan1', 'default-name': 'wlan1', 'type': 'wlan', 'mtu': '1500', 'mac_address': 'B8:69:F4:07:BE:AD'}, {'status': 'running', 'name': 'lo0', 'type': 'bridge', 'mtu': '1500', 'mac_address': 'AA:BB:CC:DD:EE:F2'}]Update FTP service to enable it, set port to 2121 and allow connections only from 192.168.1.0/24 subnetfromrouteros_ssh_connectorimportMikrotikDevicerouter=MikrotikDevice()router.connect("10.0.0.1","myuser","strongpassword",2222)print(router.update_services("ftp","no","2121","192.168.1.0/24"))router.disconnect()delrouterOutput returnsTrueif no errors are encountered. In other case, returns the error itself:TrueCreate a new enabled route to 172.16.0.0/25 with gateway 192.168.1.1 and distance of 5fromrouteros_ssh_connectorimportMikrotikDevicerouter=MikrotikDevice()router.connect("10.0.0.1","myuser","strongpassword")print(router.create_route("172.16.0.0/25","192.168.1.1","5"))router.disconnect()delrouterOutput returnsTrueif no errors are encountered. In other case, returns the error itself:TrueSend custom command to devicefromrouteros_ssh_connectorimportMikrotikDevicerouter=MikrotikDevice()router.connect("10.0.0.1","myuser","strongpassword")print(router.send_command("/system clock print"))router.disconnect()delrouterOutput returns command output without left spaces (left trim):time: 19:47:44
date: jun/01/2021
time-zone-autodetect: yes
time-zone-name: Europe/Madrid
gmt-offset: +02:00
dst-active: yesDownload backup from device to local folderfromrouteros_ssh_connectorimportMikrotikDevicerouter=MikrotikDevice()router.connect("10.0.0.1","myuser","strongpassword")# local_path examples:# For Linux: "/home/myuser"# For Windows: "C:/Users/myuser/Downloads"print(router.download_backup("/home/myuser"))router.disconnect()delrouterOutput returns a message with full path of downloaded export file:/home/mysuser/backup_Mikrotik_07-06-2021_21-38-47.backupExport full config from device to terminal outputfromrouteros_ssh_connectorimportMikrotikDevicerouter=MikrotikDevice()router.connect("10.0.0.1","myuser","strongpassword")print(router.get_export_configuration())router.disconnect()delrouterOutput returns device config export to terminal:# jun/01/2021 19:04:03 by RouterOS 6.47.9
# software id = XXXX-XXXX
#
# model = RouterBOARD mAP L-2nD
# serial number = FFFFFFFFFFF
/interface pwr-line set [ find default-name=pwr-line1 ] disabled=yes
/interface bridge add name=lo0
/interface ethernet set [ find default-name=ether1 ] l2mtu=2000
/interface wireless set [ find default-name=wlan1 ] ssid=MikroTik
/interface wireless security-profiles set [ find default=yes ] supplicant-identity=MikroTik
/ip hotspot profile set [ find default=yes ] html-directory=flash/hotspot
...Export full config from device to local folderfromrouteros_ssh_connectorimportMikrotikDevicerouter=MikrotikDevice()router.connect("10.0.0.1","myuser","strongpassword")print(router.download_export("/home/myuser"))router.disconnect()delrouterOutput returns a message with full path of downloaded export file:/home/myuser/export_Mikrotik_07-06-2021_21-42-33.rscDownload any file from devicefromrouteros_ssh_connectorimportMikrotikDevicerouter=MikrotikDevice()router.connect("10.0.0.1","myuser","strongpassword")print(router.download_file("myfile.txt","/home/myuser"))router.disconnect()delrouterOutput returns a message with full path of downloaded export file:/home/myuser/myfile.txtConfigure 2.4 GHz wlan interfacefromrouteros_ssh_connectorimportMikrotikDevicerouter=MikrotikDevice()router.connect("10.0.0.1","myuser","strongpassword")print(router.config_wlan(ssid="MySSID",password="12345678",band="2g"))router.disconnect()delrouterOutput returns a message with configuration result:2.4 GHz wlan configured sucessfully!Upgrade RouterOS softwarefromrouteros_ssh_connectorimportMikrotikDevicerouter=MikrotikDevice()router.connect("10.0.0.1","myuser","strongpassword")print(router.update_system())router.disconnect()delrouterOutput returns a message with command result:Update available!. Updating RouterOS device...