q29191 发表于 2018-8-6 11:44:36

用python写的agent

  解决apache+python无法执行一些root命令的问题
  用root启动python服务器端,远程可以执行任何root命令
  #/usr/bin/python
  #Filename:agent.py
  #siyu@2012-6-29
  #example:
  #curl hostname:PORT_NUMBER/?PASSWORD?COMMAND?OPTION
  #
  import time
  import BaseHTTPServer
  import urlparse
  import os
  PASSWORD = 'hello1234'
  HOST_NAME = 'sqa.broom.cm4' # !!!REMEMBER TO CHANGE THIS!!!
  PORT_NUMBER = 8082 # Maybe set this to 9000.
  def testcommand(c):
  commandzoo = ('ls','fdisk')
  if c not in commandzoo:
  return 1
  return 0
  class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  def do_HEAD(s):
  s.send_response(200)
  s.send_header("Content-type", "text/html")
  s.end_headers()
  def do_GET(s):
  """Respond to a GET request."""
  s.send_response(200)
  s.send_header("Content-type", "text/html")
  s.end_headers()
  #s.wfile.write(&quot;<html><head><title>ngis agent</title></head>&quot;)
  print s.path
  string = s.path
  string = string.split('?')
  passwd = string
  command = string
  option = string
  if passwd == PASSWORD:
  if testcommand(command) == 1:
  s.wfile.write(&quot;command not found&quot;)
  else:
  x = &quot;&quot;
  #x = &quot;/bin/&quot;
  x = command
  x += &quot; &quot;
  x += option
  print x
  output = os.system(x)
  print &quot;result&quot;,output
  s.wfile.write(output)
  else:
  s.wfile.write(&quot;password error&quot;)
  #s.wfile.write(&quot;<p>You accessed path: %s</p>&quot; % s.path)
  #s.wfile.write(&quot;</body></html>&quot;)
  if __name__ == '__main__':
  server_class = BaseHTTPServer.HTTPServer
  httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
  print time.asctime(), &quot;Server Starts - %s:%s&quot; % (HOST_NAME, PORT_NUMBER)
  try:
  httpd.serve_forever()
  except KeyboardInterrupt:
  pass
  httpd.server_close()
  print time.asctime(), &quot;Server Stops - %s:%s&quot; % (HOST_NAME, PORT_NUMBER)
页: [1]
查看完整版本: 用python写的agent