Advertisement
s243a

bottle remote cmd via get on linux

Nov 5th, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.91 KB | None | 0 0
  1. import bottle
  2. from bottle import route, run, Bottle
  3. import sys, os, re, urllib
  4.  
  5. app = Bottle()
  6.  
  7. #m1="'"
  8.  
  9. def delim_filter(config):
  10.     bottle.debug(True)
  11.     print("Entering delim_filter")
  12.     ''' Matches a comma separated list of numbers. '''
  13.     aconfig = config or "%20"
  14.     print ("aconfig=" + aconfig)
  15.     delimiter = urllib.unquote(aconfig).decode('utf8')
  16.     print("delimiter=" + delimiter)  
  17.     regexp = r'(?!{a_delim})+({a_delim}(?!{a_delim})+)*'.format(a_delim=re.escape(delimiter))
  18.     print("regexp="+regexp)
  19.     def to_python(match):
  20.         print("Converting Match")
  21.         print("Math=" + match)
  22.         ms = map(urllib.unquote(aconfig).decode,match.split())
  23.         print( "ms=" + ms )
  24.         return ms
  25.  
  26.     def to_url(astr):
  27.         print("Converting to URL")
  28.         print ("astr=" + astr)
  29.         astr2 = delimiter.join(map(urllib.unquote(aconfig).decode,astr))
  30.         print("astr2="+astr2)
  31.         print  astr2
  32.         return astr2
  33.  
  34.     return regexp, to_python, to_url
  35.  
  36. app.router.add_filter('delim', delim_filter)
  37.  
  38. def split_urlArgs(url,delim=" ",
  39.                   decode='All',
  40.                   xfm_de=lambda a: urllib.unquote(a).decode('utf8'),
  41.                   xfm_split=lambda a, b: re.split(a,b), #Typically a is a regular expression and b a string
  42.                   fn_regexp = lambda a: r'(?!{a_delim})+({a_delim}(?!{a_delim})+)*'.format(a_delim=a) ):
  43.     bottle.debug(True)
  44.     ''' Matches a comma separated list of numbers. '''
  45.     print("delimiter=" + delim)  
  46.     if decode == "Args":
  47.         ms = xfm_split(fn_regexp(a),url)
  48.     elif decode == "All":
  49.         url2 = xfm_de(url)    #Decode the URL
  50.         delim= xfm_de(delim) #Decode the delimitor
  51.         ms = xfm_split(fn_regexp(delim),url)
  52.     elif decode == "None":
  53.         ms = xfm_split(fn_regexp(delim),url) #SPlit the URL based on a regular expression.
  54.            
  55.     print( "ms=" + "["+",".join(ms) +"]" )    
  56.     return ms, delim
  57.  
  58. def combine_urlArgs(args,delim=" ",
  59.                     encode='All',
  60.                     xfm_encode=lambda a: urllib.quote(a, safe=''),
  61.                     xfm_join=lambda a, b: a.join(b)): #Typically a is the delimiter and b is a list
  62.     print("Converting to URL")
  63.     print ("astr=" + args)
  64.     if decode == "Args":
  65.         astr2 = xfm_join(delim,(map(lambda a: xfm_encode,args)))
  66.     elif decode == "All":
  67.         astr2 = urllib.unquote(xfm_join(delim,args))
  68.     elif decode=="None":
  69.         astr2 = xfm_join(delim,args)      
  70.     print("astr2="+astr2)
  71.     return astr2    
  72.  
  73.  
  74. #@app.route('/<key>/%09/<cmd>/<args:delim:%09>') # %09 == tab
  75. #@app.route('/<key>/%20/<cmd>/<args:delim:%20>') # %20 == ' '
  76. #@app.route('/<key>/ /<cmd>/<args:delim: >') # %20 == ' '
  77. #@app.route('/<key>/%3B/<cmd>/<args:delim:%3B>') # %3B == ';'
  78. #@app.route('/<key>/;/<cmd>/<args:delim:;>') # %3B == ';'
  79. #@app.route('/<key>/%2C/<cmd>/<args:delim:%2C>') # %2C == ','
  80. #@app.route('/<key>/,/<cmd>/<args:delim:,>') # %2C == ','
  81. @app.route('/<key>/<sep>/<cmd>/<args:path>') # %2C == ','
  82. def mystery(key,cmd,args,sep=""):
  83.     bottle.debug(True)
  84.     print("engering mystery")
  85.     print("sep=" + sep)
  86.     if len(sep) > 0 : # True if seperator is a wild card
  87.         args, sep = split_urlArgs(args,sep)
  88.     if key == '1234': # %2C == ','
  89.         print(key)
  90.         print(cmd)
  91.         print(args)
  92.         #delimiter = urllib.unquote(aconfig).decode('utf8')
  93.         #print(delimiter)            
  94.         result1 = os.popen("IFS=<a_dilim>".format(a_dilim=sep)).read()
  95.         print("result1=" + result1)
  96.         result2 = result1 + "<br>"
  97.         print("result2=" + result2)    
  98.         cmd_str=cmd + sep + sep.join(args)
  99.         print(cmd_str)  
  100.         result3 = result2 +os.popen(cmd_str).read()
  101.         print("result3=" + result3)              
  102.         return "<pre>" + result3 + "</pre>"
  103.         #return result3.replace("\n","<br>")
  104.  
  105. app.run(host='localhost', port=8082, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement