-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
69 lines (57 loc) · 2.44 KB
/
main.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
"""
Raspberry Pi Service Monitoring
Autor: Cente m - https://github.com/410g0n3
Acknowledgment: Esteban aka elt10@h4ck
"""
#Import libraries
from getpass import getpass
import paramiko
import base64
#Define devices params.
# HOST IP PORT USERNAME DEVICE
rpi_1=['192.168.2.231', '22', 'debian','Raspberry Pi 1']
rpi_2=['192.168.1.9', '22', 'ubuntu', 'Raspberry Pi 3']
#Temporaly password
p="aG9sYW11bmRvCg=="
def ssh_conn(a):
#Start ssh connection with paramiko library
client = paramiko.SSHClient()
#Set default policy to locate host key locally
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#Set connection
try:
#PASS_1 = getpass(a[3] + " password: ")
client.connect(hostname=a[0], port=a[1], username=a[2], password=base64.b64decode(p).decode('utf-8').rstrip())
print("\033[1;35m\n[+] Connection succesful with {}.\n\tListing services:\033[0;m".format(a[3]))
#exec_command returns stdin, stdout and stderr
entrada, salida, error= client.exec_command("systemctl list-units --state=running | grep service | awk '{print $5,$6,$7,$8,$9}'")
ssh_conn.out = salida.read().decode()
entrada2, salida2, error2 = client.exec_command("uptime -p | awk '{$1 = \"\"; print}'")
salida2 = salida2.read().decode()
#Print if we have an error in execute command
print(error2.read().decode())
#Print succesful output of execute command
print("\033[0;36m"+ssh_conn.out+"\033[0;m\n\t\033[1;35m Uptime machine:\033[0;m\n\033[0;36m"+ salida2 +"\033[0;m")
print(error.read().decode())
#Print if password doesn't match
except paramiko.ssh_exception.AuthenticationException :
print('\033[1;33m[!] Wrong pass\033[0;m')
ssh_conn(a)
#Print if we cannot connect to any device
except paramiko.ssh_exception.NoValidConnectionsError :
print('\033[1;31m[!] Cannot connect to {}\033[0;m'.format(a[3]))
#Asigned empty value to ssh_conn out because if this error happen, file.write log an other error
ssh_conn.out=""
pass
def f(m,n):
with open("services.txt", m) as file:
file.write(n+"\n")
file.write("\n\n" + ssh_conn.out + "\n")
if __name__ == "__main__":
try:
ssh_conn(rpi_1)
f('w',rpi_1[3])
ssh_conn(rpi_2)
f('a', rpi_2[3])
except KeyboardInterrupt:
print("\n\033[0;31m[-] Exit...\033[0;m")