Python wrapper for running instances of trisurf-ng
Samo Penic
2017-02-21 c1504d4d71afcd0764bfbb51578ebed556f36aec
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):
af3d0e 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):
af3d0e 36             try:
SP 37                 self.ssh.close()
38             except:
39                 print("Cannot disconect. Unknown error.")
8ab985 40         else:
SP 41             print("Cannot disconect. Already disconnected.")
42         self.connected=False
43
44     def execute(self,command):
45         if(self.connected):
46             try:
47                 stdin,stdout,stderr=self.ssh.exec_command(command)
48                 output=stdout.readlines()
49                 errors=stderr.readlines()
50             #    print(errors)
51                 return(output)
52             except:
53                 print("Cannot execute remote commands")
54         else:
55             print("Cannot execute remote commands. Connect first.")
56
57     def send_file(self, local, remote):
af3d0e 58         sftp=self.ssh.open_sftp()
SP 59         sftp.put(local,remote)
60         sftp.close()
8ab985 61
SP 62     def receive_file(self,remote,local):
af3d0e 63         sftp=self.ssh.open_sftp()
SP 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):
af3d0e 92         sftp=self.ssh.open_sftp()
SP 93 #        try:
94 #            sftp.chdir(directory)  # Test if remote_path exists
95 #        except (IOError,FileNotFoundError):
96 #            sftp.mkdir(directory)  # Create remote_path
97 #            sftp.chdir(directory)
98         self.mkdir_p(sftp, directory)
99         for f in set(local_files):    
100             sftp.put(f, f)
101         sftp.close()