-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsshConnect.py
More file actions
executable file
·60 lines (46 loc) · 1.46 KB
/
sshConnect.py
File metadata and controls
executable file
·60 lines (46 loc) · 1.46 KB
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
#!/bin/python3
import paramiko
import argparse
import sys
def Help():
parser = argparse.ArgumentParser(
prog = "sshConnect.py",
description = """
Scrpit connect to host via ssh connection and execute
commands
""",
)
parser.add_argument("-o", "--other", action="store_true")
parser.add_argument("hostIP")
parser.add_argument("username")
parser.add_argument("password")
args=parser.parse_args()
return args
def outputToFile(output:str):
with open("sshlog.txt", "w") as f:
f.wite(output)
def SSHconnect(hostIP:str, username:str, password:str, execCommands:list):
client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# paramiko tries to connect to a server via ssh
try:
client.connect(hostIP, port=22, username=username, password=password)
for command in execCommands:
_stdin, _stdout, _stderr = client.exec_command(command)
# if something goes wrong, a script will end with code
except:
sys.exit(-1)
output = _stdout.read().decode()
client.close()
outputToFile(output)
# main function of script
if __name__ == "__main__":
help = Help()
if help.other:
execCommands = sys.argv[4:]
else:
execCommands = ("ls", "ps",)
hostIP = sys.argv[1]
username = sys.argv[2]
password = sys.argv[3]
SSHconnect(hostIP, username, password, execCommands)