Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
scrapper-portabilidad
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
2
Issues
2
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Javier
scrapper-portabilidad
Commits
6a172079
Commit
6a172079
authored
Apr 22, 2022
by
Javier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Búsqueda de IP y descarga de Backup
parent
b56f7b72
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
99 additions
and
8 deletions
+99
-8
launch.json
.vscode/launch.json
+2
-3
utils.py
pbx/utils.py
+97
-5
No files found.
.vscode/launch.json
View file @
6a172079
...
@@ -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"
,
...
...
pbx/utils.py
View file @
6a172079
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
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__"
:
if
__name__
==
"__main__"
:
print
(
obtain_ip_address
(
'TP-LINK Gigabit Ethernet USB Adapter'
))
print
(
arp_scan
(
interface
=
'TP-LINK Gigabit Ethernet USB Adapter'
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment