Community discussions

MikroTik App
 
Artnet44
just joined
Topic Author
Posts: 7
Joined: Tue Jan 12, 2021 5:56 pm

Automation tool for quick tasks

Tue Oct 18, 2022 1:22 pm

Hello everyone,

Was wondering if anyone might be looking for a little tool to perform some actions on mikrotik units.
The tool is extremely rudimentary at the current point in time, but I am open to suggestions from any of the devs/programmers within the community.

Apologies if this is in the wrong section, not too sure where to share information like this.
Hope this can help somebody out there to save some time or maybe give an idea that can be taken further.

PS: I am only a python hobbyist so apologies for the non-pythonic coding ^^

Link is at: https://github.com/4rt-Net/Mikrotik-Aut ... wissTik.py

Code is as follows:

import argparse
import paramiko
import re
import time
from time import sleep

parser = argparse.ArgumentParser(description='\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Created by Art-Net ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThe lazy-admin tool to quickly and easily perform functions on RouterOS.\nSpecifically created to save you time and sanity while changing settings remotely.\n\nThis tool uses SSH to open a connection to the target\nDue to SSH being used this tool is sensitive to ssh configuration:\nrequirements: port 22 needs to be open on RouterOS\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~', formatter_class=argparse.RawTextHelpFormatter)

parser.add_argument(
"--command",
"-c",
default="None",
help=(
"List of commands:\n"
" check-mac\n"
" check-identity\n"
" check-scanlist\n"
" change-radioname\n"
" create-ssl\n"
))

parser.add_argument(
"--username",
"-u",
default="admin",
help="specify username for login"
	)

parser.add_argument(
"--password",
"-p",
default="None",
help="specify password for login"
	)

parser.add_argument(
"--target",
"-t",
default="0.0.0.0",
help="specify IP address to target"
	)

parser.add_argument(
"--ssl",
"-s",
default="Cert-1",
help="Specify name for ssl certificate"
)

parser.add_argument(
"--radioname",
"-r",
default="None",
help="specify radio-name for wlan1 interface"
	)

def MacRegex():
	macregex = "([0-9a-fA-F]{2}:){5}[1-9a-fA-F]{2}"
	file = open('temp-maccheck.txt', 'r')
	for lines in file:
   	 matches = re.search(macregex, lines)
   	 if matches != None:
   	   print(matches.group())

def ScanlistRegex():
	scanregex = "scan-list=\d{4}-\d{4}"
	file = open('temp-scanlist.txt', 'r')
	for lines in file:
   	 matches = re.search(scanregex, lines)
   	 if matches != None:
   	   print(matches.group())

def main(args):
	#Check mac address of wlan1 interface
	if args.command == "check-mac":
		try:
			client = paramiko.SSHClient()
			client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
			client.connect(hostname = args.target , port='22', username= args.username ,password= args.password ,look_for_keys=False)
			print("Connected to: "+(args.target))
			stdin,stdout,stderr = client.exec_command('/interface wireless print')
			output_read = stdout.readlines()
			file=open('temp-maccheck.txt','w')
			file.write(''.join(output_read))
			file.close()
			stdin,stdout,stderr.flush()
		except:
			print("# Connection failed to: ", args.target)
			print("check that the port 22 is open on the target")
		finally:
			client.close()
			MacRegex()

	#Check scanlist of unit		
	elif args.command == "check-scanlist":
		try:
			client = paramiko.SSHClient()
			client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
			client.connect(hostname = args.target , port='22', username= args.username ,password= args.password ,look_for_keys=False)
			print("Connected to: "+(args.target))
			stdin,stdout,stderr = client.exec_command('/interface wireless print')
			output_read = stdout.readlines()
			file=open('temp-scanlist.txt', 'w')
			file.write(''.join(output_read))
			file.close()
			stdin,stdout,stderr.flush()
		except:
			print("# Connection failed to: ", args.target)
			print("check that the port 22 is open on the target")
		finally:
			client.close()
			ScanlistRegex()

	#Check system identity of unit
	elif args.command == "check-identity":
		try:
			client = paramiko.SSHClient()
			client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
			client.connect(hostname = args.target , port='22', username= args.username ,password= args.password ,look_for_keys=False)
			print("Connected to: "+(args.target))
			stdin,stdout,stderr = client.exec_command('/system identity print')
			output_read = stdout.readlines()
			print(output_read)
			stdin,stdout,stderr.flush()
		except:
			print("# Connection failed to: ", args.target)
			print("check that the port 22 is open on the target")
		finally:
			client.close()

	elif args.command == "change-radioname":
		try:
			client = paramiko.SSHClient()
			client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
			client.connect(hostname = args.target , port='22', username= args.username ,password= args.password ,look_for_keys=False)
			print("Connected to: "+(args.target))
			stdin,stdout,stderr = client.exec_command('/interface wireless set wlan1 radio-name='+str(args.radioname))
			print("Unit is reconnecting, radio-name changed to:"+str(args.radioname))
			print("closing ssh connection...")
			client.close()
			stdin,stdout,stderr.flush()
		except:
			print("# Connection failed to: ", args.target)
			print("check that the port 22 is open on the target")

	elif args.command == "create-ssl":
		try:
			client = paramiko.SSHClient()
			client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
			client.connect(hostname = args.target , port='22', username= args.username ,password= args.password ,look_for_keys=False)
			print("Connected to: "+(args.target))
			print("Creating SSL Certificate...")
			stdin,stdout,stderr = client.exec_command('/certificate add name='+(args.ssl)+' common-name='+(args.ssl)+' key-usage=key-cert-sign,crl-sign key-size=2048 trusted=yes days-valid=1175')
			print("Done")
			stdin,stdout,stderr = client.exec_command(('/certificate sign '+(args.ssl)+' name='+(args.ssl)+' ca-crl-host='+(args.target)))
			print("Signing certificate, please wait...")
			time.sleep(20)
			print("Done")
			print("Assigning certificate to API-SSL service...")
			stdin,stdout,stderr = client.exec_command('/ip service set api-ssl certificate='+(args.ssl))
			print("SSL Certificate completed!")
			client.close()
			stdin,stdout,stderr.flush()
		except:
			print("# Connection failed to: ", args.target)
			print("check that the port 22 is open on the target")

if __name__ == "__main__":
	main(parser.parse_args())
Last edited by Artnet44 on Tue Oct 18, 2022 3:20 pm, edited 6 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Automation tool for quick tasks

Tue Oct 18, 2022 1:59 pm

client.exec_command('/interface wireless set wlan1 radio-name='+str(args.radioname))

If radio name contain space do not work, must be radio-name="<NAME>"

SONAR? "/certificate sign sonar name="

and everywere are missing the " on winbox commands (when involved text).
 
Artnet44
just joined
Topic Author
Posts: 7
Joined: Tue Jan 12, 2021 5:56 pm

Re: Automation tool for quick tasks

Tue Oct 18, 2022 2:08 pm

client.exec_command('/interface wireless set wlan1 radio-name='+str(args.radioname))

If radio name contain space do not work, must be radio-name="<NAME>"

SONAR? "/certificate sign sonar name="

and everywere are missing the " on winbox commands (when involved text).
Hi Rextended, thanks for pointing out.
Will see if I can rework the ssl portion and edit as needed

For now I removed the parts giving issues until I can find a working solution
Last edited by Artnet44 on Tue Oct 18, 2022 2:13 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Automation tool for quick tasks

Tue Oct 18, 2022 2:09 pm

A valid RegEx for MAC is ([0-9a-fA-F]{2}:){5}[1-9a-fA-F]{2}
(?:[0-9a-fA-F]: ?){12} match also aaaaaaaaaaaa or 2003:beef:0430:
 
Artnet44
just joined
Topic Author
Posts: 7
Joined: Tue Jan 12, 2021 5:56 pm

Re: Automation tool for quick tasks

Tue Oct 18, 2022 2:35 pm

I see what you mean with the mac regex, tested it on a unit.
Thanks a mil!

I've updated the regex & changed code slightly for ease of reference. :)
The SSL problem has been resolved, will tackle radio-name with spaces issue next.

Here is the revised code:
import argparse
import paramiko
import re
import time
from time import sleep

parser = argparse.ArgumentParser(description='\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Created by Art-Net ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThe lazy-admin tool to quickly and easily perform functions on RouterOS.\nSpecifically created to save you time and sanity while changing settings remotely.\n\nThis tool uses SSH to open a connection to the target\nDue to SSH being used this tool is sensitive to ssh configuration:\nrequirements: port 22 needs to be open on RouterOS\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~', formatter_class=argparse.RawTextHelpFormatter)

parser.add_argument(
"--command",
"-c",
default="None",
help=(
"List of commands:\n"
" check-mac\n"
" check-identity\n"
" check-scanlist\n"
" change-radioname\n"
" create-ssl\n"
))

parser.add_argument(
"--username",
"-u",
default="admin",
help="specify username for login"
	)

parser.add_argument(
"--password",
"-p",
default="None",
help="specify password for login"
	)

parser.add_argument(
"--target",
"-t",
default="0.0.0.0",
help="specify IP address to target"
	)

parser.add_argument(
"--ssl",
"-s",
default="Cert-1",
help="Specify name for ssl certificate"
)

parser.add_argument(
"--radioname",
"-r",
default="None",
help="specify radio-name for wlan1 interface"
	)

def MacRegex():
	macregex = "([0-9a-fA-F]{2}:){5}[1-9a-fA-F]{2}"
	file = open('temp-maccheck.txt', 'r')
	for lines in file:
   	 matches = re.search(macregex, lines)
   	 if matches != None:
   	   print(matches.group())

def ScanlistRegex():
	scanregex = "scan-list=\d{4}-\d{4}"
	file = open('temp-scanlist.txt', 'r')
	for lines in file:
   	 matches = re.search(scanregex, lines)
   	 if matches != None:
   	   print(matches.group())

def main(args):
	#Check mac address of wlan1 interface
	if args.command == "check-mac":
		try:
			client = paramiko.SSHClient()
			client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
			client.connect(hostname = args.target , port='22', username= args.username ,password= args.password ,look_for_keys=False)
			print("Connected to: "+(args.target))
			stdin,stdout,stderr = client.exec_command('/interface wireless print')
			output_read = stdout.readlines()
			file=open('temp-maccheck.txt','w')
			file.write(''.join(output_read))
			file.close()
			stdin,stdout,stderr.flush()
		except:
			print("# Connection failed to: ", args.target)
			print("check that the port 22 is open on the target")
		finally:
			client.close()
			MacRegex()

	#Check scanlist of unit		
	elif args.command == "check-scanlist":
		try:
			client = paramiko.SSHClient()
			client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
			client.connect(hostname = args.target , port='22', username= args.username ,password= args.password ,look_for_keys=False)
			print("Connected to: "+(args.target))
			stdin,stdout,stderr = client.exec_command('/interface wireless print')
			output_read = stdout.readlines()
			file=open('temp-scanlist.txt', 'w')
			file.write(''.join(output_read))
			file.close()
			stdin,stdout,stderr.flush()
		except:
			print("# Connection failed to: ", args.target)
			print("check that the port 22 is open on the target")
		finally:
			client.close()
			ScanlistRegex()

	#Check system identity of unit
	elif args.command == "check-identity":
		try:
			client = paramiko.SSHClient()
			client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
			client.connect(hostname = args.target , port='22', username= args.username ,password= args.password ,look_for_keys=False)
			print("Connected to: "+(args.target))
			stdin,stdout,stderr = client.exec_command('/system identity print')
			output_read = stdout.readlines()
			print(output_read)
			stdin,stdout,stderr.flush()
		except:
			print("# Connection failed to: ", args.target)
			print("check that the port 22 is open on the target")
		finally:
			client.close()

	elif args.command == "change-radioname":
		try:
			client = paramiko.SSHClient()
			client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
			client.connect(hostname = args.target , port='22', username= args.username ,password= args.password ,look_for_keys=False)
			print("Connected to: "+(args.target))
			stdin,stdout,stderr = client.exec_command('/interface wireless set wlan1 radio-name='+str(args.radioname))
			print("Unit is reconnecting, radio-name changed to:"+str(args.radioname))
			print("closing ssh connection...")
			client.close()
			stdin,stdout,stderr.flush()
		except:
			print("# Connection failed to: ", args.target)
			print("check that the port 22 is open on the target")

	elif args.command == "create-ssl":
		try:
			client = paramiko.SSHClient()
			client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
			client.connect(hostname = args.target , port='22', username= args.username ,password= args.password ,look_for_keys=False)
			print("Connected to: "+(args.target))
			print("Creating SSL Certificate...")
			stdin,stdout,stderr = client.exec_command('/certificate add name='+(args.ssl)+' common-name='+(args.ssl)+' key-usage=key-cert-sign,crl-sign key-size=2048 trusted=yes days-valid=1175')
			print("Done")
			stdin,stdout,stderr = client.exec_command(('/certificate sign '+(args.ssl)+' name='+(args.ssl)+' ca-crl-host='+(args.target)))
			print("Signing certificate, please wait...")
			time.sleep(20)
			print("Done")
			print("Assigning certificate to API-SSL service...")
			stdin,stdout,stderr = client.exec_command('/ip service set api-ssl certificate='+(args.ssl))
			print("SSL Certificate completed!")
			client.close()
			stdin,stdout,stderr.flush()
		except:
			print("# Connection failed to: ", args.target)
			print("check that the port 22 is open on the target")

if __name__ == "__main__":
	main(parser.parse_args())

Who is online

Users browsing this forum: No registered users and 13 guests