Save Mikrotik RouterOS log to file with Python

Save Mikrotik RouterOS log to file with Python
User Rating: 4 (1 votes)

Cerita nya kita mau save log nya router Mikrotik nya kita ke sebuah file menggunakan python

  1. pastikan pyton sudah terinstall tentunya (tidak dijelaskan disini..googling aja..:-) )
  2. Sekarang buatlah file berikut :

import paramiko
import time
import sys

class Logger(object):
def __init__(self, filename=”Default.log”):
self.terminal = sys.stdout
self.log = open(filename, “a”)

def write(self, message):
self.terminal.write(message)
self.log.write(message)

ip = ‘ip-router-here’
username = ‘username-route-here’
password = ‘password-router-here’
port=22 #sesuaikan dengan port ssh yang ada di ip > service
remote_conn_pre = paramiko.SSHClient()
print(‘successfuly configured the SSH client’)

# print remote_conn_pre

remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

remote_conn_pre.connect(ip, username=username, password=password, port=port)

remote_conn = remote_conn_pre.invoke_shell()
outpt = remote_conn.recv(65535) # read as much as you can.
stdin,stdout,stderr = remote_conn_pre.exec_command(‘\n’)

time.sleep(1)

stdin,stdout,stderr = remote_conn_pre.exec_command(‘/log print’)
sys.stdout = Logger(“/Users/macbookpro/Desktop/log-router.txt”)
print stdout.read()

3. Simpan dengan nama yang kita suka dengan extension .py (penting!: jangan simpan dengan nama paramiko.py)

4. lalu jalankan via terminal laptop : python [nama-file].py dan perhatikan hasilnya serta file yang dibuat.

 

#saya menggunakan python yang terinstall di macOS

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*