I am programming script for mikrotik configuration via Python file. I can connect to mikrotik via SSH, Telnet and mac-telnet but I can't send command to mikrotik, the output is process is interupted. Can you help me please with pushing the commands in python script with mac-telnet? I was testing the mactelnet function in main.
I am including script also.
Thanks a lot.
import sys, posix, time, md5, binascii, socket, select
import pexpect
import os
class LoginManager:
""" Class representing Microtic """
def __init__(self,hostIP,login,password):
self.host = hostIP
self.username = login
self.pwd = password
#telnet na mikrotik
def loginTelnet(self,login,password):
"""
Function to login to mikrotik via telnet
:param login:
:param password:
:return:
"""
import telnetlib
try:
host = '172.16.49.2'
port = 23
telnetcon = telnetlib.Telnet(host,port)
telnetcon = telnetlib.Telnet(host)
#user input
telnetcon.read_until(b"Login: ")
telnetcon.write(login.encode()+"\n")
#user password
telnetcon.read_until(b"Password: ")
telnetcon.write(password.encode()+b"\n")
time.sleep(10)
telnetcon.close()
'''
telnetcon.read_until()
telnetcon.read_all('Please press "Enter" to continue!'+"\n")
telnetcon.write('\013')
telnetcon.read_all('/ip address print')
'''
except:
print "Cannot connect to router via telnet"
#metoda na prihlasenie pomocou SSH
def loginSSH(self,login,password):
from pexpect import pxssh,spawn,expect
import getpass
try:
#self.login = login
#self.password = password
connect = pxssh.pxssh()
server = '172.16.49.2'
login = 'admin'
password = 'admin'
port = 22
connect.login(server,login,password)
commands = pxssh.spawn()
time.sleep(10)
connect.logout()
'''
commands.expect('Please press "Enter" to continue!')
commands.sendline('\013')
connect.sendline('/ip address print')
connect.prompt()
print connect.before
'''
except pxssh.ExceptionPxssh,e:
print "Error"
print str(e)
#router = RouterOS('172.16.49.2', 'admin', 'admin')
#router.loginTelnet('admin','admin')
#os.system("mactelnet -l -t 50 > mt.output 2>&1")
#zoznam devicov
deviceList = []
loadMacAddress = False
with open("mt.output", "r") as file:
for line in file:
if loadMacAddress:
macAddress = line.split()[1]
deviceList.append(macAddress)
else:
header = line.split()
if len(header)>1:
if "IP" in header[0] and "MAC-Address" in header[1]:
loadMacAddress = True
print deviceList
username = 'admin'
password = '""'
os.system('mactelnet {} -u {} -p {}'.format(deviceList[0], username, password))
os.system("interface print")
#print udajue[0][0]
Adrian
Try using this mikrotik python api module: https://github.com/rtician/routeros . The example below was taken from the README.md in the repo:
$ pip install routeros
I've tested this with Python 3.6.4 and it worked. Please note your api access has to be enabled in IP->Services on your RouterOS.
from routeros import login
routeros = login('user', 'password', '10.1.0.1')
sample = routeros.query('/ip/pool/print').equal(name='dhcp')
routeros.close()
print(sample)
Output example:
({'.id': '*1', 'name': 'dhcp', 'ranges': '192.168.88.10-192.168.88.254'},
{'.id': '*2', 'name': 'hs-pool-8', 'ranges': '10.5.50.2-10.5.50.254'})
Or use ansible. There are a couple of libraries out there which use ansible to talk to Mikrotik routers using ssh.
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.