IPアドレスをMacアドレスのIPアドレスの別のプールに追加する

みなさんこんにちは!

このトピックは、Pythonを使用して、以下を実行するスクリプトを作成する方法に関するものです。



  1. アクティブなポピーアドレスのリストをMikrotikからアンロードします
  2. 電話のポピーIPアドレスを選択します
  3. それらをIPアドレスの別のプールに入れます


誰も気にしないで、猫の下で歓迎してください。



まず、これに使用するツールを決定しました。 Pythonを(最初のPLとして)習い始めたので、それを実践することにしました。 さらに、コメント付きのコードの例を示します。



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 = []
      
      







get_export_file_from_mikrotik()関数で、Mikrotikに接続し、/ ip dhcp-server lease print file = lease_fileコマンドを使用して、アクティブなすべてのポピーアドレスをlease_ftpファイルに書き込み、FTPを使用して、スクリプト作業フォルダーにアップロードします。



また、ここでは、ケシの電話番号の空のリストを作成します。 次は次のコードです



 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
      
      







lease_ftpファイルには多くの追加情報があるため、get_all_mac関数を使用して、すべてのアクティブなデバイスのポピーアドレスのみを含むリストを取得しました。 次のスクリプトを使用して、すべてのポピーアドレスから必要なアドレスだけを引き出します(私の場合、これは電話のポピーアドレスです)。



 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   ")
      
      







次のコマンドは次のことを行います。

make_mikrotik_script()関数は、Mikrotikが処理してプールにポピーを追加できるスクリプトファイルを作成します。

upload_to_ftp()関数は、完成したスクリプトをMikrotik FTPサーバーにアップロードします。

最後の関数は、ルーター上でスクリプトを直接実行します。



  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)
      
      







お気づきかもしれませんが、ロギングは進行中です。

このような単純なアクションの結果として、すべての電話は別のプールにあり、必要に応じて、それらと別々に作業することも便利です。



PS使用済みPythonバージョン3.3



All Articles