-
Notifications
You must be signed in to change notification settings - Fork 2
/
RemoteProcess.py
38 lines (34 loc) · 1.31 KB
/
RemoteProcess.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
import paramiko
class RemoteProcess():
def __init__(self, username, password, hostname):
self.cwd = ""
try:
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(hostname, username = username, password = password)
except:
print "Sorry, we can't SSH to your server. Please try again later."
def execute(self, command):
command = command[0].lower() + command[1:]
args = command.split(" ")
dirChanged = False
currentDir = ""
if (args[0] == "cd"):
dirChanged = True
currentDir = args[1]
command = "cd " + self.cwd + "&& " + command
# handle incorrect commands
try:
stdin, stdout, stderr = self.client.exec_command(command);
if stdout.channel.recv_exit_status() == 0:
output = stdout.read()
else:
output = stderr.read()
except:
output = "Error when connect to server"
# change directory if necessary
if (dirChanged):
stdin, stdout, stderr = self.client.exec_command('pwd');
self.cwd = stdout.read().rstrip() + "/" + currentDir
output = self.cwd
return output