-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutercheck.py
98 lines (95 loc) · 3.91 KB
/
routercheck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import re, platform, os, sys
from datetime import datetime
from urllib.request import urlopen as url
try:
import paramiko
except ModuleNotFoundError: # Paramiko not installed by default
print("INSTALL MISSING MODULE")
try:
url("https://pypi.org/")
except:
print("The Internet is required to install missing module")
sys.exit(0)
from pip._internal import main as pip
pip(['install', 'paramiko'])
import paramiko
if platform.system() == "Windows":
os.system('cls')
else:
os.system('clear')
# SERVER ACCESS
HOST = '' # IP or domain
USER = '' # Username
PSWD = '' # Password
PATH = '' # Directory with backups
FAIL = '\033[91m' # Colors for terminal
ENDC = '\033[0m'
client = paramiko.SSHClient() # SSH connection
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=HOST, username=USER, password=PSWD)
sftp = client.open_sftp()
print('Connected')
lastbackups = []
print('Geting the current backups')
for ip in sftp.listdir(PATH):
max = -9999999999 # Will compare the sequence numbers or timestamps
filePath = ''
number = True
for archive in sftp.listdir(PATH + '/' + ip):
if re.fullmatch("\d+[\.]\d+-\d+", archive): # If format "IP-number"
if int(archive.split('-')[1]) > max and number:
max = int(archive.split('-')[1])
filePath = PATH + '/' + ip + '/' + archive
else: # If format "IP-date"
if number:
max = -9999999999
number = False
date = archive.replace(ip, '').split('-')[:-1] # Clear filename from ip and milliseconds
for i in range(len(date) - 2):
if date[i] == "":
date.pop(i) # If filename has 2 dashes - clear it
if re.fullmatch("[\.]\D{3}", date[i]):
date[i] = date[i].replace('.', '') # If there is a dot before the month - clear it
dateString = ''.join('-'.join(date).split('.')[0]) # Collect a standardized string
try:
dateObject = datetime.strptime(dateString, '%b-%d-%H-%M-%S').timestamp()
except ValueError: # If the seconds are more than 60 - set 00. Btw it doesn't matter
dateParse = dateString.split('-')
dateParse[-1] = "00"
dateObject = datetime.strptime('-'.join(dateParse), '%b-%d-%H-%M-%S').timestamp()
if dateObject > max:
max = dateObject
filePath = PATH + '/' + ip + '/' + archive
if filePath != '' and filePath not in lastbackups: # Avoid repeats
lastbackups.append([ip, filePath])
while True:
register = False
try:
SRCH = input('\nSearch request: ')
except KeyboardInterrupt:
print()
sys.exit(0)
if re.fullmatch('".+"', SRCH):
register = True
for backup in lastbackups:
with sftp.open(backup[1]) as file:
hostname = ''
lineNum = 0
found = False
for line in file:
lineNum += 1
if 'hostname' in line and not found: # Search for hostname or request
hostname = line.replace('hostname ', '').replace('\n', '')
found = True
if register:
if SRCH in line:
if found:
print(backup[0], hostname, 'Ln:' + str(lineNum), backup[1])
else:
print(backup[0], FAIL + 'Hostname_not_found' + ENDC, 'Ln:' + str(lineNum), backup[1])
else:
if SRCH.lower() in line.lower():
if found:
print(backup[0], hostname, 'Ln:' + str(lineNum), backup[1])
else:
print(backup[0], FAIL + 'Hostname_not_found' + ENDC, 'Ln:' + str(lineNum), backup[1])