Trisurf Monte Carlo simulator
Samo Penic
2016-07-06 61d2e7bdabfdac94c533265941d4909537676f4b
commit | author | age
c54425 1 import paramiko
SP 2
3
4
5 class Connection:
6     def __init__(self, hostname, port=22, username=None, password=None):
7         self.hostname=hostname
8         self.port=port
9         if(username!=None):
10             self.username=username
11         else:
12             self.username=''
13         if(password!=None):
14             self.password=password
15         else:
16             self.password=''
17         self.ssh=paramiko.SSHClient()
18         self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
19         self.connected=False
20         return
21     
22     def connect(self, Timeout=5):
23         if(not self.connected):
24             try:
25                 print("Trying to connect to: "+self.username+"@"+self.hostname+":"+str(self.port)+".")
26                 self.ssh.connect(self.hostname, username=self.username, password=self.password, port=self.port, timeout=Timeout)
27                 self.connected=True
28             except:
29                 print("Error establishing connection with "+self.username+"@"+self.hostname+":"+str(self.port)+".")
30                 exit(1)
31         else:
32             print("Already connected!")
33         return
34
35     def disconnect(self):
36         if(self.connected):
37             try:
38                 self.ssh.close()
39             except:
40                 print("Cannot disconect. Unknown error.")
41         else:
42             print("Cannot disconect. Already disconnected.")
43         self.connected=False
44
45     def execute(self,command):
46         if(self.connected):
47             try:
48                 stdin,stdout,stderr=self.ssh.exec_command(command)
49                 output=stdout.readlines()
50                 errors=stderr.readlines()
51             #    print(errors)
52                 return(output)
53             except:
54                 print("Cannot execute remote commands")
55         else:
56             print("Cannot execute remote commands. Connect first.")
57
58     def send_file(self, local, remote):
59         sftp=self.ssh.open_sftp()
60         sftp.put(local,remote)
61         sftp.close()
62
63     def receive_file(self,remote,local):
64         sftp=self.ssh.open_sftp()
65         sftp.get(remote,local)
66         sftp.close()
67
68     def mkdir_remote(self,directory):
69         sftp=self.ssh.open_sftp()
70         sftp.mkdir(directory)
71         sftp.close()
72
73