📜 ⬆️ ⬇️

Add IP phones to a separate pool of IP addresses on a Mac address

Hello!
This topic is about how I used Python to write a script that performs the following actions:

  1. Unloads the list of active poppy addresses from Mikrotik
  2. Selects the IP address MAC address
  3. Places them in a separate pool of IP addresses

Who cares please under the cat.

For a start, I decided what tools I would use for this. Since I just started learning Python (as the first PS), I decided to put it into practice. Further I will give an example of a code with its commenting.

import telnetlib import time import ftplib import logging class MacToPool(): def get_export_file_from_mikrotik(self): host = "IP  " user = "_" password = "_" command_1 = '/ip dhcp-server lease print file=lease_file' command_2 = 'quit' tn = telnetlib.Telnet(host) tn.read_until(b"Login: ") tn.write(user.encode('UTF-8') + b"\n") tn.read_until(b"Password: ") tn.write(password.encode('UTF-8') + b"\n") tn.read_until(b'>') ftp = ftplib.FTP('IP  ') ftp.login('_', '_') try: ftp.delete('lease_file.txt') ftp.delete('script_mac_phone.rsc') logging.basicConfig(filename='log.txt', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logging.info(" : lease_file.txt, script_mac_phone.rsc") except Exception: logging.basicConfig(filename='log.txt', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logging.info("    .  .") time.sleep(1) tn.write(command_1.encode('UTF-8') + b"\r\n") time.sleep(1) logging.basicConfig(filename='log.txt', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logging.info("  .") tn.read_until(b'>') tn.write(command_2.encode('UTF-8') + b"\r\n") time.sleep(1) f = open('lease_file.txt', "wb") ftp.retrbinary("RETR lease_file.txt", f.write) logging.basicConfig(filename='log.txt', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logging.info("    ") lst_mac_phone = [] 

')
In the get_export_file_from_mikrotik () function, I connect to the microtic, and with the command / ip dhcp-server lease print file = lease_file I write all the active mac addresses into the file lease_ftp and then download it to FTP into my working script folder for further work.

Also here I create an empty list for poppy phone addresses. Next comes the following code.

 def get_all_mac(self): self.get_export_file_from_mikrotik(MacToPool) text = open('lease_file.txt').readlines() self.all_mac = [] for line in text: lst_value = line.split(" ") for symbol in lst_value: if len(symbol) == 17: self.all_mac.append(symbol) return self.all_mac 


In the lease_ftp file, we have a lot of unnecessary information, and therefore, using the get_all_mac function, I received a list containing only the mac addresses of all active devices. I pull out the following script from all the poppy addresses that I need (in my case these are the poppy addresses of the phones):

 def find_mac_phone(self): self.get_all_mac(MacToPool) mac_phone = open("macphone.txt", "w") for mac_address in self.all_mac: if mac_address[0:8] == "00:15:65": mac_phone.write(mac_address + "\n") self.lst_mac_phone.append(mac_address) logging.basicConfig(filename='log.txt', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logging.info("  MAC   ") 


The following commands will do the following:
The function make_mikrotik_script () creates a script file that Mikrotik can process to add poppies to the pool.
The upload_to_ftp () function uploads the finished script to the microtic FTP server.
And the last function performs the immediate launch of the script on the router.

  def make_mikrotik_script(self): ### ,           IP-Phones### script_mac_phone = open("script_mac_phone.txt", "w") script_mac_phone.write("/ip dhcp-server lease \n") self.find_mac_phone(MacToPool) for mac_address in self.lst_mac_phone: script_mac_phone.write("add address=IP-Phones mac-address=" + mac_address + " server=server1" + '\n') self.upload_to_ftp(MacToPool) logging.basicConfig(filename='log.txt', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logging.info("  script_mac_phone.rsc") def upload_to_ftp(self): ftp = ftplib.FTP('IP  ') ftp.login('_', '_') f = open("script_mac_phone.txt", "rb") ftp.storbinary("STOR script_mac_phone.rsc", f) f.close() logging.basicConfig(filename='log.txt', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logging.info("script_mac_phone.rsc   ") def load_script_to_mikrotik(self): self.make_mikrotik_script(MacToPool) host = "IP  " user = "_" password = "_" command_1 = '/import file-name=script_mac_phone.rsc' command_2 = 'quit' tn = telnetlib.Telnet(host) tn.read_until(b"Login: ") tn.write(user.encode('UTF-8') + b"\n") tn.read_until(b"Password: ") tn.write(password.encode('UTF-8') + b"\n") tn.read_until(b'>') tn.write(command_1.encode('UTF-8') + b"\r\n") time.sleep(1) logging.basicConfig(filename='log.txt', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logging.info(" . Done!") tn.read_until(b'>') tn.write(command_2.encode('UTF-8') + b"\r\n") time.sleep(1) 


Just as you might have noticed, logging is in progress.
As a result of such simple actions, all the phones are in my separate pool and now, if necessary, it is also convenient for me to work separately with them.

PS Python version 3.3 was used

Source: https://habr.com/ru/post/219463/


All Articles