Python wrapper for running instances of trisurf-ng
Samo Penic
2017-11-28 8b8845e452b45b765c0c74a388296d10e91787fc
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)+".")
824b3e 29 #                self.connected=False
52e871 30 #                exit(1)
8ab985 31         else:
SP 32             print("Already connected!")
33         return
34
35     def disconnect(self):
36         if(self.connected):
52e871 37             self.ssh.close()
8ab985 38         else:
SP 39             print("Cannot disconect. Already disconnected.")
40         self.connected=False
41
42     def execute(self,command):
43         if(self.connected):
44             try:
45                 stdin,stdout,stderr=self.ssh.exec_command(command)
46                 output=stdout.readlines()
47                 errors=stderr.readlines()
48             #    print(errors)
49                 return(output)
50             except:
51                 print("Cannot execute remote commands")
52         else:
53             print("Cannot execute remote commands. Connect first.")
54
55     def send_file(self, local, remote):
52e871 56         pass
SP 57 #        sftp=self.ssh.open_sftp()
58 #        sftp.put(local,remote)
59 #        sftp.close()
8ab985 60
SP 61     def receive_file(self,remote,local):
52e871 62         pass
SP 63 #        sftp=self.ssh.open_sftp()
64 #        sftp.get(remote,local)
65 #        sftp.close()
8ab985 66
SP 67     def mkdir_remote(self,directory):
68         sftp=self.ssh.open_sftp()
69         sftp.mkdir(directory)
70         sftp.close()
71
d7e21a 72     def mkdir_p(self,sftp, remote_directory):
SP 73         """Change to this directory, recursively making new folders if needed.
74         Returns True if any folders were created. Recursive algorithm."""
75         if remote_directory == '/':
76             # absolute path so change directory to root
77             sftp.chdir('/')
78             return
79         if remote_directory == '':
80             # top-level relative directory must exist
81             return
82         try:
83             sftp.chdir(remote_directory) # sub-directory exists
84         except IOError:
85             dirname, basename = os.path.split(remote_directory.rstrip('/'))
86             self.mkdir_p(sftp, dirname) # make parent directories
87             sftp.mkdir(basename) # sub-directory missing, so created it
88             sftp.chdir(basename)
89         return True
8ab985 90     
d7e21a 91     def send_multiple_files_in_directory(self,local_files,directory):
52e871 92         pass