views:

164

answers:

2

Hello,

I am looking at trying to create a python server, which allows me to run root commands on a Centos Server remotely, I would also like the server to be able to respond with the result's of the command.

I have found another question on here which has a basic python server, however it throws a error, the code is:

#!/usr/bin/python
import os
import socket
print "  Loading Bindings..."
settings = {}
line = 0
for each in open('/root/actions.txt', 'r'):
 line = line + 1
  each = each.rstrip()
  if each <> "":
    if each[0] <> '#':
      a = each.partition(':')
      if a[2]:
        settings[a[0]] = a[2]
      else:
        print "    Err @ line",line,":",each
print "  Starting Server...",
port = 12345
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", port))
print "OK."
print "  Listening on port:", port
while True:
    datagram = s.recv(1024)
    if not datagram:
        break
    print "Rx Cmd:", datagram
    if settings.has_key(datagram):
      print "Launch:", settings[datagram]
      os.system(settings[datagram]+" &")
s.close()

When i run using python vzctl.py. I get the following error:

  File "vzctl.py", line 9
    each = each.rstrip()
    ^
SyntaxError: invalid syntax

Does anyone have any idea of the error, and if it would be possible to add the function of the server responding with the output of the command.

You can see the source of this script at : http://stackoverflow.com/questions/722172/how-can-i-have-a-php-script-run-a-shell-script-as-root

Thanks, Ashley

+2  A: 

you need to keep indentation at the same level for each nested statement throughout your code.

SilentGhost
Im now getting: a = each.partition(':')AttributeError: 'str' object has no attribute 'partition'After changing the indents correctly and changing <> to !=
AshleyUK
Forgot to say error is at line 12
AshleyUK
partition was added in python 2.5, if you're using earlier versions of python, you'll get this error.
SilentGhost
I now have it working, any idea on if it would be possible to send the output back?
AshleyUK
you need to ask single question per problem, this drift far too quickly into unknown.
SilentGhost
+2  A: 

On a different note: why not using TwistedMatrix?

Keeper
It seems to be a webserver, or am I reading the website incorectly?
AshleyUK
It's a network framework: you can develop your own network server. Take a look here http://twistedmatrix.com/projects/core/documentation/examples/
Keeper
Once you wrap your brain around twisted, you'll be much happier than attempting to roll your own. Be advised, however, that the documentation is a bit sparse.
DrBloodmoney