This commit is contained in:
93
scripts/interfaces.py
Normal file
93
scripts/interfaces.py
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
import os
|
||||||
|
import netifaces
|
||||||
|
import socket
|
||||||
|
import requests
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
LOG_DIR = "/sdsat/logs/"
|
||||||
|
|
||||||
|
def get_mac_addresses():
|
||||||
|
interfaces = netifaces.interfaces()
|
||||||
|
mac_addresses = []
|
||||||
|
|
||||||
|
for interface in interfaces:
|
||||||
|
try:
|
||||||
|
mac = netifaces.ifaddresses(interface)[netifaces.AF_LINK][0]['addr']
|
||||||
|
mac_addresses.append(mac)
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return mac_addresses
|
||||||
|
|
||||||
|
def get_interface_info(mac_address):
|
||||||
|
interfaces = netifaces.interfaces()
|
||||||
|
|
||||||
|
for interface in interfaces:
|
||||||
|
try:
|
||||||
|
addresses = netifaces.ifaddresses(interface)
|
||||||
|
mac = addresses[netifaces.AF_LINK][0]['addr']
|
||||||
|
if mac == mac_address:
|
||||||
|
ip = addresses[netifaces.AF_INET][0]['addr']
|
||||||
|
name = interface
|
||||||
|
return ip, name
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
def get_public_ip():
|
||||||
|
try:
|
||||||
|
response = requests.get('https://api.ipify.org?format=json')
|
||||||
|
data = response.json()
|
||||||
|
return data['ip']
|
||||||
|
except requests.RequestException:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def create_hostname(public_ip):
|
||||||
|
if public_ip:
|
||||||
|
return "SDSAT-" + public_ip.replace(".", "")
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_network_interface_info():
|
||||||
|
mac_addresses = get_mac_addresses()
|
||||||
|
interface_info = []
|
||||||
|
|
||||||
|
for mac_address in mac_addresses:
|
||||||
|
ip, name = get_interface_info(mac_address)
|
||||||
|
if ip and name:
|
||||||
|
interface_info.append((mac_address, ip, name))
|
||||||
|
|
||||||
|
return interface_info
|
||||||
|
|
||||||
|
def write_log(action):
|
||||||
|
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
log_entry = f"{current_time} : {action}"
|
||||||
|
log_file_path = os.path.join(LOG_DIR, "interfaces_check_log.txt")
|
||||||
|
with open(log_file_path, "a") as log_file:
|
||||||
|
log_file.write(log_entry + "\n")
|
||||||
|
|
||||||
|
# Verifier si le repertoire de logs existe, sinon le creer
|
||||||
|
if not os.path.exists(LOG_DIR):
|
||||||
|
os.makedirs(LOG_DIR)
|
||||||
|
|
||||||
|
interface_info = get_network_interface_info()
|
||||||
|
|
||||||
|
if len(interface_info) > 0:
|
||||||
|
write_log("Informations sur les interfaces reseau :")
|
||||||
|
for mac_address, ip, name in interface_info:
|
||||||
|
write_log(f"Adresse MAC : {mac_address}")
|
||||||
|
write_log(f"Adresse IP : {ip}")
|
||||||
|
write_log(f"Nom de l'interface : {name}")
|
||||||
|
write_log("")
|
||||||
|
else:
|
||||||
|
write_log("Aucune carte reseau trouvee.")
|
||||||
|
|
||||||
|
public_ip = get_public_ip()
|
||||||
|
hostname = create_hostname(public_ip)
|
||||||
|
|
||||||
|
if hostname:
|
||||||
|
write_log(f"Adresse IP publique : {public_ip}")
|
||||||
|
write_log(f"Hostname : {hostname}")
|
||||||
|
else:
|
||||||
|
write_log("Impossible de recuperer l'adresse IP publique.")
|
||||||
Reference in New Issue
Block a user