-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysmon.py
55 lines (45 loc) · 1.46 KB
/
sysmon.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
from flask import Flask
app = Flask(__name__)
from os import system
from paramiko import SSHClient, AutoAddPolicy, RSAKey
from paramiko.auth_handler import AuthenticationException, SSHException
from scp import SCPClient, SCPException
@app.route('/sysmon')
def sysmon():
return remote_call("hostname")
def remote_call(command):
host = "polarbear.vs.mythic-beasts.com"
username = "root"
key_path = "/Users/daveh/.ssh/id_rsa"
try:
ssh_key = RSAKey.from_private_key_file(key_path)
print(f"Found SSH key at self {key_path}")
except SSHException as e:
print(e)
try:
system(f"ssh-copy-id -i {key_path}.pub {username}@{host}>/dev/null 2>&1")
print(f"{key_path} uploaded to {host}")
except FileNotFoundError as error:
print(error)
try:
client = SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(AutoAddPolicy())
client.connect(
host,
username=username,
password="",
key_filename=key_path,
timeout=5000,
)
except AuthenticationException as e:
print(f"Authentication failed: did you remember to create an SSH key? {e}")
raise e
stdin, stdout, stderr = client.exec_command(command)
stdout.channel.recv_exit_status()
response = stdout.readlines()
for line in response:
print(line)
if client:
client.close()
return line