-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
164 lines (144 loc) · 4.64 KB
/
run.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import argparse
import os
import re
import sys
from datetime import datetime
import paramiko
from dotenv import load_dotenv
def establishConnection(ssh_server, ssh_port, user, pasd, public_key_auth, pk_file_location):
ssh_client = paramiko.client.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
if public_key_auth:
print('asfd')
ssh_client.connect(
hostname=ssh_server,
port=ssh_port,
username=user,
key_filename=pk_file_location
)
else:
ssh_client.connect(hostname=ssh_server,
port=ssh_port,
username=user,
password=pasd
)
except paramiko.AuthenticationException:
print("Authentication failed. Please check your username and password.")
ssh_client.close()
return None
except paramiko.SSHException as e:
print(f"SSH error: {e}")
ssh_client.close()
return None
return ssh_client
def isCommandInstalled(base_cmd, ssh_client):
full_command = f"which {base_cmd}"
_stdin, stdout, _stderr = ssh_client.exec_command(full_command)
result = re.match(stdout.read().decode('utf-8'), 'not\ found')
return result
def runCommand(base_cmd, arguments, save_out, no_out):
try:
if save_out:
output_file_name = datetime.now().strftime(f"%d-%m-%Y_%H-%M-%S_{base_cmd}")
makedir = f"mkdir -p $HOME/offloaded_scans/ && "
out_redirect = f"| tee $HOME/offloaded_scans/{output_file_name}.txt"
full_command = f"{makedir} {base_cmd} {arguments} {out_redirect}"
else:
full_command = f"{base_cmd} {arguments}"
_stdin, stdout, _stderr = ssh_client.exec_command(full_command)
output = stdout.read().decode('utf-8')
if no_out == False:
print(output)
if save_out:
print(f"File saved on remote machine under $HOME/offloaded_scans/ with name {output_file_name}.txt", end="\n\n")
except paramiko.AuthenticationException:
print("Authentication failed. Please check your username and password.")
except paramiko.SSHException as e:
print(f"SSH error: {e}")
finally:
ssh_client.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Python script to offload resource intesive tasks to IoT devices"
)
required_args = parser.add_argument_group("required arguments")
optional_args = parser.add_argument_group("optional arguments")
required_args.add_argument(
"-b",
"--base",
dest="base",
help="Base command",
required=True
)
required_args.add_argument(
"-a",
"--args",
dest="args",
help="Arguments for the base command",
required=True
)
optional_args.add_argument(
"-s",
"--save-output-remote",
dest="sor",
help="Save to file on remote device",
action=argparse.BooleanOptionalAction,
default=False
)
optional_args.add_argument(
"-n",
"--no-output-local",
dest="nol",
help="Do not show output to in the terminal and exit as soon as command has been supplied",
action=argparse.BooleanOptionalAction,
default=False
)
optional_args.add_argument(
"-p",
"--use-public-key-remote",
dest="upkr",
help="Use public key authentication for ssh connection",
action=argparse.BooleanOptionalAction,
default=False
)
try:
args = parser.parse_args()
except:
parser.print_help()
sys.exit(1)
base_cmd = args.base
arguments = args.args
save_out = args.sor
no_out = args.nol
public_key_auth = args.upkr
load_dotenv()
server = os.environ.get("server")
port = os.environ.get("port")
username = os.environ.get("user")
password = os.environ.get("password")
pk_file_location = ""
if public_key_auth:
pk_file_location = os.environ.get("ssh_public_key")
ssh_client = establishConnection(
server,
port,
username,
password,
public_key_auth,
pk_file_location
)
if ssh_client != None:
check = isCommandInstalled(
base_cmd,
ssh_client
)
if check == None:
runCommand(
base_cmd,
arguments,
save_out,
no_out
)
else:
print(f"{base_cmd} has not been installed on the server")