Mikrotik python mac telnet,无法发送命令
我正在通过Python文件为mikrotik配置编写脚本。我可以通过SSH、Telnet和mac Telnet连接到mikrotik,但我无法向mikrotik发送命令,输出被中断。你能帮我用mac telnet推送python脚本中的命令吗?我主要是在测试mactelnet的功能 我包括脚本也 非常感谢Mikrotik python mac telnet,无法发送命令,python,shell,mikrotik,Python,Shell,Mikrotik,我正在通过Python文件为mikrotik配置编写脚本。我可以通过SSH、Telnet和mac Telnet连接到mikrotik,但我无法向mikrotik发送命令,输出被中断。你能帮我用mac telnet推送python脚本中的命令吗?我主要是在测试mactelnet的功能 我包括脚本也 非常感谢 import sys, posix, time, md5, binascii, socket, select import pexpect import os class Login
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尝试使用此mikrotik python api模块:。以下示例摘自回购协议中的README.md:
$ pip install routeros
我已经用Python3.6.4对其进行了测试,并且效果良好。请注意,必须在路由器上的IP->Services中启用api访问
从routeros导入登录
routeros=login('user','password','10.1.0.1')
sample=routeros.query('/ip/pool/print').equal(name='dhcp'))
routeros.close()
打印(样本)
输出示例:
({'.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'})
或者使用ansible。有几个库使用ansible使用ssh与Mikrotik路由器进行对话。一个
os.system()
调用运行一个命令并在该命令完成时返回——在一个调用和下一个调用之间没有连续性。因此,您不能使用os.system()
进行此调用subprocess.Popen()
是一个更好的工具。我正在考虑在通过mac telnet登录mikrotik后使用API,即发送/interface/print,但我无法通过is.system()库完成,你能给我一些建议吗?我已经做了。使用该库启动一个拥有连接的进程,然后向该进程发送其他命令。既然还有SSH接口,您也可以考虑。