export attempt in mikrotik with python

Question:

I am trying to export some files to my local pc with python, i tried several alternatives and i got several errors, but i got stuck on this error, couldn’t find the cause, does anyone have any ideas? I was seeing that it is entered by tftp, but I can’t find a practical way to do it by code either

My code:

`

import paramiko
from getpass import getpass
import time

HOST = 'xxx.xx.xx.xx'
PORT ='xx'
USER = 'xxx'

if __name__ == '__main__':
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            
    password = getpass ('Ingrese su contraseña: ')
    client.connect(HOST, PORT, username=USER, password=password, banner_timeout=200)
            
    stdin, stdout, stderr = client.exec_command('export file=/flash/prueba_export.backup to=C:/mikrotik')
            
    time.sleep(1)
            
    result = stdout.read().decode()
    print(result)

error: expected end of command (line 1 column 41)

Also try via tftpy:

`

    import tftpy

# Crea un cliente TFTP
client = tftpy.TftpClient('ip', 69)

# Descarga el archivo de configuración de Mikrotik
client.download('flash/prueba_export.backup', 'C:\mikrotik/prueba_export.backup')

# Cierra la conexión TFTP
client.close()
Asked By: mayra

||

Answers:

You can use the paramiko library in unison with the python scp library. It is very simple to use.

install using: pip install scp

import paramiko
from scp import SCPClient

def sshClient():
    # your paramiko code to connetc client
    ...
    client.connect(HOST, PORT, username=USER, password=password)
    return client

ssh = sshClient()
scp = SCPClient(ssh.get_transport())

Then simply use scp.get(...) to download your wanted file.

Also see scp’s github page. You can also use with statements to make it cleaner:

from paramiko import SSHClient
from scp import SCPClient

with SSHClient() as ssh:
    ssh.load_system_host_keys()
    ssh.connect('example.com')

    with SCPClient(ssh.get_transport()) as scp:
        scp.put('test.txt', 'test2.txt')
        scp.get('test2.txt')
Answered By: Hein Gertenbach
    import paramiko

client = paramiko.SSHClient()


client.connect('ip', port='22', username='admin', password='pass')

#Setup sftp connection and transmit this script 
print (client)

sftp = client.open_sftp() 
sftp.get('/ruta_mikrotik','ruta_local')


sftp.close()
enter code here`
Answered By: mayra
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.