简体   繁体   中英

Extract backups from Mikrotik with python

I have to extract backups from Mikrotik with python and save them in my server so these backups are saved in my computer and also on the servers. I've been searching for info about it but didn't have any luck. Can someone help me?

""" Authentication for remote desktops through ssh protocol  """

import paramiko
from getpass import getpass
import time

HOST = 'xxx.xxx.xxx.xxx'
PORT ='xxx'
USER = 'xxxxxxx'
""" data =dict(hostname=HOST, port=PORT, username=USER) """

if name == 'main':
    # try:
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy( paramiko.AutoAddPolicy())

        password = getpass ('Insert password: ')
        client.connect(HOST, PORT, username=USER, password=password)

        stdin, stdout, stderr = client.exec_command('ls')

        #tried everything here

        time.sleep(1)

        result = stdout.read().decode()
    # except paramiko.ssh_exception.AuthenticationException as e:
    #     print('Failed authentication')

        print(result)

You cannot do that with plain SSH. It will execute RouterOS commands (so no ls ). The task can be done with FTP or SFTP (if you prefer SSH) client:

$ sftp admin@192.0.2.1
admin@192.0.2.1's password:
Connected to 192.0.2.1.
sftp> ls
flash
sftp> cd flash
sftp> ls
MikroTik-20210509-1756.backup   MikroTik-20210509-1938.backup   skins
sftp> get MikroTik-20210509-1756.backup
Fetching /flash/MikroTik-20210509-1756.backup to MikroTik-20210509-1756.backup
MikroTik-20210509-1756.backup                    100%  345KB 808.0KB/s   00:00
sftp> exit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM