1 | import socket
|
---|
2 | import platform
|
---|
3 | import subprocess
|
---|
4 | import os
|
---|
5 | from MatlabFuncs import *
|
---|
6 |
|
---|
7 | def issmssh(host,login,port,command):
|
---|
8 | """
|
---|
9 | ISSMSSH - wrapper for OS independent ssh command.
|
---|
10 |
|
---|
11 | usage:
|
---|
12 | issmssh(host,command)
|
---|
13 | """
|
---|
14 |
|
---|
15 | #first get hostname
|
---|
16 | hostname=socket.gethostname().lower().split('.')[0]
|
---|
17 |
|
---|
18 | #if same as host, just run the command.
|
---|
19 | if strcmpi(host,hostname):
|
---|
20 | subprocess.call(command,shell=True)
|
---|
21 | else:
|
---|
22 | if 'Windows' in platform.system():
|
---|
23 | #use the putty project plink.exe: it should be in the path.
|
---|
24 |
|
---|
25 | #get ISSM_DIR variable
|
---|
26 | if 'ISSM_DIR_WIN' in os.environ:
|
---|
27 | ISSM_DIR=os.environ['ISSM_DIR_WIN'][1:-2]
|
---|
28 | else:
|
---|
29 | raise OSError("issmssh error message: could not find ISSM_DIR_WIN environment variable.")
|
---|
30 |
|
---|
31 | username=raw_input('Username: (quoted string) ')
|
---|
32 | key=raw_input('Key: (quoted string) ')
|
---|
33 |
|
---|
34 | subprocess.call('%s/externalpackages/ssh/plink.exe -ssh -l "%s" -pw "%s" %s "%s"' % (ISSM_DIR,username,key,host,command),shell=True);
|
---|
35 |
|
---|
36 | else:
|
---|
37 | #just use standard unix ssh
|
---|
38 | if port:
|
---|
39 | subprocess.call('ssh -l %s -p %d localhost "%s"' % (login,port,command),shell=True)
|
---|
40 | else:
|
---|
41 | subprocess.call('ssh -l %s %s "%s"' % (login,host,command),shell=True)
|
---|
42 |
|
---|