Python remote control does not work

246 Views Asked by At

Hi I wrote a tool to control my computer remotely. But it just doesn't work. I get the connection between server and client but if i send commands they are not executed. Thanks for helping me. I tested client and server seperate and i think the problem is at the server (it has a client socket - confusing sorry about that) - it does not execute the batchcommands

Server:

import socket, os, time
tester = 1

def main():
    conn = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    while True:
        time.sleep(1)
        try:
            conn.connect(("internalip",50001))
            income()
        except:
            main()



def income():
    while True:
        befehl = conn.recv(2048)
        print(befehl)
        if(befehl=="upload"):
            upload()

        elif(befehl=="download"):
            download()

        elif(befehl=="cmd"):
            cmdexe()

        else:
            conn.send("unknown command")


def cmdexe():    
    command = conn.recv(2048)
    print(command)
    if(command=="exit"):
        income()

    else:
        try:
            os.system(command)
        except:
            conn.send(str(exception)+" problem with cmd")
    income()


def upload():
    name=str(conn.recv(2048))
    if(name=="exit"):
        income()
    else:
        mode==str(conn.recv(1024))
        if(mode=="exit"):
            income()
        else:
            f = open(name,mode)
            try:
                f.write(str(conn.recv(8192)))
                f.close()
            except:
                conn.send("An Error with filehandling/upload")
                f.close()
                income()



def download():
    name=str(conn.recv(2048))
    if(name=="exit"):
        income()

    f= open(name,"r")
    try:
        conn.send(str(f.read()))
        income()
    except:
        f.close()
        conn.send("An Error with filehandling/upload")
        income()

#verbesserung: 1.automatisches setup 2.mehrere commands bei cmd

main()` 

Client:

import socket
from threading import Thread

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(("",50001))
s.listen(1)

conn,addr = s.accept()

def main():
    print(str(addr)+" is connected")
    print("Choose a command")
    #command_list={"upload", "download", "cmd"}
    #for i in range(3):
    #    print(str(command_list[i]))
    print("upload", "download", "cmd")

    befehl=raw_input(": ")

    if(befehl=="exit"):
        main()
    elif(befehl=="upload"):
        upload()
    elif(befehl=="download"):
        download()
    elif(befehl=="cmd"):
        cmd()
    else:
        print("Sorry, thats not a regular command")



def upload():
    conn.send("upload")

    name=raw_input("Geben sie den Namen ein, den die Datei haben soll!")
    if(name=="exit"):
        conn.send(name)
        main()
    conn.send(name)

    mode=raw_input("Send a for append or w to write: ")
    if(name=="exit"):
        main()
    elif(mode=="a"):
        conn.send("a")
    elif(mode=="w"):
        conn.send("w")
    else:
        print("Thats not a regulare mode")
        conn.send("exit")
    uf = open(raw_input("Choose the file you want to send: "),"r")
    conn.send("uf")
    main()

"""
def errorhandling():
    print(str(conn.recv(4096)))

Thread(target=errorhandling()).start()
"""
def download():
    conn.send("download")
    name=raw_input("Geben sie den Namen, der Datei ein, die sie downloaden moechten: ")
    if(name==exit):
        conn.send(name)
        main()
    conn.send(name)
    try:
        f=open(addr+"; "+name,"a")
        f.append(conn.recv(1048576))
    except:
        print("Error with filehandling: "+exception)
        main()

def cmd():
    conn.send("cmd")
    command = raw_input("Auszufuehrendes Kommando: ")
    if(command=="exit"):
        conn.send("exit")
        main()
    conn.send(command)
    cmd()


#verbesserung: Probleme mit mehreren Verbindungen 



main()
1

There are 1 best solutions below

3
On

Any reason why you do not using an existing tool such as Fabric.

see: http://www.fabfile.org/

Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.

It provides a basic suite of operations for executing local or remote shell commands (normally or via sudo) and uploading/downloading files, as well as auxiliary functionality such as prompting the running user for input, or aborting execution.