-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsshConnectMonitor.py
executable file
·49 lines (39 loc) · 1.25 KB
/
sshConnectMonitor.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
#!/bin/python3
import os
import time
import argparse
# help function
def Help():
parser = argparse.ArgumentParser(
prog = "sshConnectMonitor.py",
description="""
Script is cheching ssh logs via 'lastb' linux command
by never ending loop
To kill process you have to use 'kill' command
""",
)
args = parser.parser_args()
return args
# function uses linux command "lastb" to check ssh logs
def badConnectionsCounter() -> int:
# by 'os' library and 'system' method script can run linux commands
cmdOutput = os.system("sudo lastb | grep ssh")
return int(cmdOutput)
# function is checking that ssh logs have a new entry
def badConnectionsExamine():
pattern = badConnectionsCounter()
time.sleep(60)
while True:
entryNumber = badConnectionsCounter()
# if ssh logs have a new entry,
# a warning is printed on the terminal
# and new the pattern has a new value
if pattern < entryNumber:
print("WARNING: New unsuccessful connection")
pattern = entryNumber
# after a checking condition, the script sleeps for 1 minute
time.sleep(60)
# main function of program
if __name__ == "__main__":
Help()
badConnectionsExamine()