Python wrapper for running instances of trisurf-ng
Samo Penic
2017-01-27 20db829c3a764ae1ea8ff79412bc7e0e9a6c92a9
commit | author | age
8ab985 1 import paramiko
d7e21a 2 import os.path
8ab985 3
SP 4 class Connection:
5     def __init__(self, hostname, port=22, username=None, password=None):
6         self.hostname=hostname
7         self.port=port
8         if(username!=None):
9             self.username=username
10         else:
11             self.username=''
12         if(password!=None):
13             self.password=password
14         else:
15             self.password=''
16         self.ssh=paramiko.SSHClient()
17         self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
18         self.connected=False
19         return
20     
21     def connect(self, Timeout=5):
22         if(not self.connected):
52e871 23 #            try:
SP 24             print("Trying to connect to: "+self.username+"@"+self.hostname+":"+str(self.port)+".")
25             self.ssh.connect(self.hostname, username=self.username, password=self.password, port=self.port, timeout=Timeout)
26             self.connected=True
27 #            except:
28 #                print("Error establishing connection with "+self.username+"@"+self.hostname+":"+str(self.port)+".")
29 #                exit(1)
8ab985 30         else:
SP 31             print("Already connected!")
32         return
33
34     def disconnect(self):
35         if(self.connected):
52e871 36             self.ssh.close()
8ab985 37         else:
SP 38             print("Cannot disconect. Already disconnected.")
39         self.connected=False
40
41     def execute(self,command):
42         if(self.connected):
43             try:
44                 stdin,stdout,stderr=self.ssh.exec_command(command)
45                 output=stdout.readlines()
46                 errors=stderr.readlines()
47             #    print(errors)
48                 return(output)
49             except:
50                 print("Cannot execute remote commands")
51         else:
52             print("Cannot execute remote commands. Connect first.")
53
54     def send_file(self, local, remote):
52e871 55         pass
SP 56 #        sftp=self.ssh.open_sftp()
57 #        sftp.put(local,remote)
58 #        sftp.close()
8ab985 59
SP 60     def receive_file(self,remote,local):
52e871 61         pass
SP 62 #        sftp=self.ssh.open_sftp()
63 #        sftp.get(remote,local)
64 #        sftp.close()
8ab985 65
SP 66     def mkdir_remote(self,directory):
67         sftp=self.ssh.open_sftp()
68         sftp.mkdir(directory)
69         sftp.close()
70
d7e21a 71     def mkdir_p(self,sftp, remote_directory):
SP 72         """Change to this directory, recursively making new folders if needed.
73         Returns True if any folders were created. Recursive algorithm."""
74         if remote_directory == '/':
75             # absolute path so change directory to root
76             sftp.chdir('/')
77             return
78         if remote_directory == '':
79             # top-level relative directory must exist
80             return
81         try:
82             sftp.chdir(remote_directory) # sub-directory exists
83         except IOError:
84             dirname, basename = os.path.split(remote_directory.rstrip('/'))
85             self.mkdir_p(sftp, dirname) # make parent directories
86             sftp.mkdir(basename) # sub-directory missing, so created it
87             sftp.chdir(basename)
88         return True
8ab985 89     
d7e21a 90     def send_multiple_files_in_directory(self,local_files,directory):
52e871 91         pass