-
Notifications
You must be signed in to change notification settings - Fork 0
/
multissh.py
124 lines (113 loc) · 4.01 KB
/
multissh.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
serv = [["192.168.0.103", "22", "mint", "123"], ["192.168.0.102", "8022", "u1_0a", "123"]]
import paramiko
import requests
import sys
import os
version = "1.1"
#TODO: SSH Keys
#Update
def update():
path = os.getcwd()
parent = os.path.abspath(os.path.join(path, os.pardir))
os.system("cp -r update.py " + parent)
os.system("python3 " + parent + "/update.py")
sys.exit()
#Code for initiating the SSH connection
def simpleLogin(hostname, port, username, password):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(hostname, port, username, password)
print(hostname, "was \033[0;92mconnected \033[0;00msuccesfully")
except:
print(hostname, "\033[0;91mcannot connect\033[0;00m")
#Code for sending SSH Commands
def sshCommand(hostname, port, username, password, command, verbose, logsilent):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
xd = True
if xd:
client.connect(hostname, port, username, password)
stdin, stdout, stderr = client.exec_command(command)
if verbose:
print(username + "@" + hostname + "$ " + command + "\n======================\n" + stdout.read().decode() + "======================")
elif logsilent:
try:
os.system("echo '" + username + " @ " + hostname + "$ " + command + "\n======================\n" + stdout.read().decode() + "======================' >> silent_log.txt")
except:
pass
else:
pass
else:
print("Error")
#A continuation of main()
def main1(serv):
print("\n\033[0;92mBasic Multi-SSH control tool " + version + " \n\033[0;91mMade by KrisIsHere\033[0;00m\n\n")
for x in serv:
simpleLogin(x[0], x[1], x[2], x[3])
while True:
global verbose
global logsilent
print("\n")
command = input("$ ")
for x in serv:
sshCommand(x[0], x[1], x[2], x[3], command, verbose, logsilent)
#Check for updates
def up_check():
print('Checking for Updates.....', end='')
ver_url = 'https://raw.githubusercontent.com/KrisIsHere/MultiSSH/main/version.txt'
try:
ver_rqst = requests.get(ver_url)
ver_sc = ver_rqst.status_code
if ver_sc == 200:
github_ver = ver_rqst.text
github_ver = github_ver.strip()
if version == github_ver:
print('\nUp-To-Date' + '\n')
exit()
else:
print('\nAvailable: {}'.format(github_ver) + '' + '\n')
up = input("Would you like to update? (Y/N): ")
if up in ["y", "Y"]:
update()
else:
exit()
else:
print('Status: {} '.format(ver_sc) + '' + '\n')
except Exception as e:
print('\nException: ' + str(e))
exit()
#Parsing command arguments
def main():
global verbose
global logsilent
args = sys.argv[1:]
if len(args) == 1 and args[0] in ["-s", "--silent"]:
logsilent = False
verbose = False
main1(serv)
elif len(args) == 1 and args[0] in ["-l", "--loud"]:
verbose = True
logsilent = False
main1(serv)
elif len(args) == 1 and args[0] in ["-u", "--update"]:
up_check()
elif len(args) == 2 and args[0] in ["-s", "--silent"] and args[1] in ["-o", "--outlog"]:
logsilent = True
verbose = False
main1(serv)
elif len(args) == 2 and args[1] in ["-s", "--silent"] and args[0] in ["-o", "--outlog"]:
logsilent = True
verbose = False
main1(serv)
else:
pass
print("""Error: Usage: python3 multissh.py [-h] [-s] [-o] [-l] [-u]
Arguments:
-h, --help | shows this help
-s, --silent | does not show ssh output
-l, --loud | shows ssh output
-u, --update | check for updates
-o, --outlog | log output of silent mode in a file (a bit buggy)
""")
main()