aboutsummaryrefslogtreecommitdiff
path: root/steuermann/rexec.py
diff options
context:
space:
mode:
authorsienkiew <sienkiew@d34015c8-bcbb-4646-8ac8-8ba5febf221d>2012-02-24 17:58:55 -0500
committersienkiew <sienkiew@d34015c8-bcbb-4646-8ac8-8ba5febf221d>2012-02-24 17:58:55 -0500
commit6787ea30126ef097db80430f151ae0848dc25a9e (patch)
tree3379c847619ec9d518592f3db4193414a0a8d05f /steuermann/rexec.py
parente80782b878fcc80f32a7aec6993c2cbdd5201885 (diff)
downloadsteuermann-6787ea30126ef097db80430f151ae0848dc25a9e.tar.gz
steuermann rexec
git-svn-id: https://svn.stsci.edu/svn/ssb/etal/steuermann/trunk@556 d34015c8-bcbb-4646-8ac8-8ba5febf221d
Diffstat (limited to 'steuermann/rexec.py')
-rw-r--r--steuermann/rexec.py105
1 files changed, 105 insertions, 0 deletions
diff --git a/steuermann/rexec.py b/steuermann/rexec.py
new file mode 100644
index 0000000..30e58dc
--- /dev/null
+++ b/steuermann/rexec.py
@@ -0,0 +1,105 @@
+import sys
+import urllib
+import urllib2
+import pandokia.helpers.easyargs as easyargs
+import os.path
+
+# http://ss64.com/nt/ - language of cmd.exe
+
+
+# URLs are
+# http://
+# hostname
+# :port
+# /password/
+# directive
+# args
+#
+# method=GET directive=run/
+# args = url encoded command line
+#
+# method=POST directive=upload
+# no args
+# post ...
+
+def urlbase( host, password ) :
+ if not ':' in host :
+ host = host + ':7070'
+ url = "http://%s/%s/"%(host,urllib.quote_plus(password))
+ return url
+
+def run( host, cmd, password, directory ):
+ url = urlbase(host,password) + "run"
+
+ data = urllib.urlencode( { 'password' : password,
+ 'dirname' : directory,
+ 'cmd' : cmd
+ }
+ )
+ req = urllib2.Request(url, data)
+ try :
+ f = urllib2.urlopen(req)
+ except urllib2.HTTPError, e:
+ print "HTTP ERROR",e.code
+ print e.read()
+ return 1
+ print f.read()
+ return 0
+
+def upload( host, filename, password, directory) :
+ url = urlbase(host, password) + "upload"
+
+ f = open(filename,"r")
+
+ data = urllib.urlencode( { 'password' : password,
+ 'dirname' : directory,
+ 'filename' : os.path.basename(filename),
+ 'data' : f.read(),
+ }
+ )
+ req = urllib2.Request(url, data)
+ try :
+ f = urllib2.urlopen(req)
+ except urllib2.HTTPError, e:
+ print "HTTP ERROR",e.code
+ print e.read()
+ return 1
+ print f.read()
+ return 0
+
+if __name__ == '__main__' :
+ opt, args = easyargs.get( {
+ '-p' : '=', # password
+ '-f' : '=', # password from file
+ '-u' : '', # upload
+ '-d' : '=', # upload destination directory (default .)
+ '-h' : '=', # host name
+ } )
+ if '-p' in opt :
+ password = opt['-p']
+ elif '-f' in opt :
+ password = open(opt['-f'],'r').readline().strip()
+
+ if not ( '-h' in opt ) :
+ print "must give host name with -h"
+ sys.exit(2)
+ host = opt['-h']
+ if '-d' in opt :
+ directory = opt['-d']
+ else :
+ directory = "."
+
+ if opt['-u'] :
+ ex = 0
+ for x in args :
+ ex |= upload(host = host , filename=x, directory=directory, password=password)
+ sys.exit(ex)
+ else :
+ x = run( host=host,
+ cmd = ' '.join(args),
+ password = password,
+ directory = directory
+ )
+ sys.exit(x)
+
+