Commit 6a172079 by Javier

Búsqueda de IP y descarga de Backup

parent b56f7b72
...@@ -9,12 +9,11 @@ ...@@ -9,12 +9,11 @@
"type": "python", "type": "python",
"request": "launch", "request": "launch",
"program": "cli.py", "program": "cli.py",
"args": ["update-plataform", "update-account","import-cdr"], "args": ["synchronize"],
"console": "integratedTerminal", "console": "integratedTerminal",
"justMyCode": true "justMyCode": true
}, },
{ {
"name": "API",
"type": "python", "type": "python",
"request": "launch", "request": "launch",
"module": "uvicorn", "module": "uvicorn",
......
from fileinput import filename
import sys import sys
from datetime import datetime from datetime import datetime
from scapy.all import srp, Ether, ARP, ICMP, IP, sr1, conf, get_if_list from scapy.all import srp, Ether, ARP, ICMP, IP, sr1, conf, get_if_list
import paramiko
import pathlib
import os
def arp_scan(interface : str, range : str = '192.168.2.0/24') -> 'list[str]': class NoBackupError(Exception):
pass
BACKUP_UPDATER_PBX ='192.168.2.66'
def arp_scan(interface: str, range: str = '192.168.2.0/24') -> 'list[str]':
"""Realiza una búsqueda de las IPs libres en el Rango indicado """Realiza una búsqueda de las IPs libres en el Rango indicado
Args: Args:
...@@ -19,7 +28,8 @@ def arp_scan(interface : str, range : str = '192.168.2.0/24') -> 'list[str]': ...@@ -19,7 +28,8 @@ def arp_scan(interface : str, range : str = '192.168.2.0/24') -> 'list[str]':
) )
return [ipa.pdst for ipa in unanswered][40:-1] return [ipa.pdst for ipa in unanswered][40:-1]
def ping(address : str, timeout : float = 4) -> bool:
def ping(address: str, timeout: float = 4) -> bool:
"""Realiza una prueba de Ping """Realiza una prueba de Ping
Args: Args:
...@@ -47,10 +57,92 @@ def obtain_ip_address(interface: str, range: str = '192.168.2.0/24') -> str: ...@@ -47,10 +57,92 @@ def obtain_ip_address(interface: str, range: str = '192.168.2.0/24') -> str:
Returns: Returns:
str: _description_ str: _description_
""" """
for address in arp_scan(interface,range): for address in arp_scan(interface, range):
if not ping(address): if not ping(address):
return address return address
if __name__ == "__main__":
print(obtain_ip_address('TP-LINK Gigabit Ethernet USB Adapter'))
def download_backup(origin_pbx_address: str) -> pathlib.Path:
"""Descarga una Copia de Seguridad de la Centralita Objetivo
Args:
origin_pbx_address (str): Dirección IP
Raises:
NoBackupError: No se ha encontrado ninguna Backup
paramiko.AuthenticationException: Ninguna clave es Válida
Returns:
pathlib.Path: Path del Fichero
"""
for password in ('_RW!2k14', 'GereDa_22', '_D@rth#Vader_'):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# Búsqueda de Fichero
ssh.connect(origin_pbx_address, username='root', password=password)
(stdin, stdout, stderr) = ssh.exec_command(
'cd /var/www/backup/ && ls -Art *.tar | tail -n 1'
)
try:
file_name = stdout.readlines()[0].strip()
except IndexError:
raise NoBackupError()
# Ruta Local
local_file_name = 'original_{address}.tar'.format(
address=origin_pbx_address.replace('.', ''))
localpath = pathlib.Path(pathlib.Path(
__file__).parent.resolve(), 'backups', local_file_name)
if localpath.exists():
os.remove(localpath)
# Descarga del Fichero
sftp = ssh.open_sftp()
sftp.get(
localpath=localpath,
remotepath='/var/www/backup/{filename}'.format(
filename=file_name),
)
# Cierre de las Conexiones
sftp.close()
ssh.close()
return localpath if localpath.exists() else None
except paramiko.AuthenticationException:
continue
raise paramiko.AuthenticationException
def update_backup(localpath : pathlib.Path):
# Conexión Máquina Backup
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(BACKUP_UPDATER_PBX, username='root', password='_RW!2k14')
remotepath = '/var/www/backup/{filename}'.format(filename=localpath.name)
# Carga de Copia de Seguridad
sftp = ssh.open_sftp()
sftp.put(
localpath=localpath,
remotepath=remotepath,
confirm=True
)
sftp.close()
# Restauración de la Copia de Seguridad
(stdin, stdout, stderr) = ssh.exec_command(
'php /var/www/html/admin/modules/backup/bin/restore.php --restore {filepath}'.format(
filepath=remotepath
)
)
ssh.close()
if __name__ == "__main__":
print(arp_scan(interface='TP-LINK Gigabit Ethernet USB Adapter'))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment