#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    WA
    Posts
    29
    Rep Power
    0

    Python Telnet-like application


    So I'm trying to make a telnet like application.

    So far, I've established a connection between the server and the client app, and I've used the subprocess module to pass commands from the client (sent to the server) to bash.

    Here's my server code -
    Code:
    #! /usr/bin/env python
    import socket,sys,subprocess
    HOST = ''
    PORT = 335
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(1)
    while 1:
        try:
            conn, addr = s.accept()
            data = conn.recv(1024)
            a = data
            b = a.split()
            print ''
            retcode = subprocess.call(b)
        except sock.error:
            s.close()
        if a == "exit":
            print "Client is disconnecting"
    Client code -
    Code:
    #! /usr/bin/env python
    import socket,sys
    i = raw_input("IP: ")
    p = 335
    while 1:
            try:
                    d = raw_input("Command: ")
                    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                    s.connect((i, p))
                    s.send(d)
            except socket.error:
                    print "Error"
                    break
    So the problem here, is that the client sends the cmd's to bash on the server, and the output appears on the server instead of the client.
    So I need to send the output BACK to the client and print it.


    When I tried to solve this issue, this is the code I ended up with -
    Server -
    Code:
    #! /usr/bin/env python
    import socket,sys,subprocess
    HOST = ''
    PORT = 335
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(1)
    while 1:
        conn, addr = s.accept()
        data = conn.recv(1024)
        a = data
        b = a.split()
        print ''
        retcode = subprocess.call(b)
        string = str(retcode)
        s.send(string)
        if a == "exit":
            print "Client is disconnecting"
    Client -
    Code:
    #! /usr/bin/env python
    import socket,sys
    i = raw_input("IP: ")
    p = 335
    while 1:
            try:
                    d = raw_input("Command: ")
                    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                    s.connect((i, p))
                    s.send(d)
                    conn, addr = s.accept()
                    output = conn.recv(1024)
                    prt = int(output)
                    print prt
            except socket.error:
                    print "Error"
                    break
    This doesn't work either....

    Any help?

    Thanks!
    Poz
  2. #2
  3. No Profile Picture
    Javascript is the debbel
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    102
    Rep Power
    12
    you might consider using oneof the popen functions since that gives you access to the stdout/stdin to the subprocess. (that's what I use, and I'm barelynovice as-is) so it seems like you would just need to outpupt the stdout to the user.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    if ur doing something with telnet why not use the telnetlib and sys has acess to the two other things(which i forgot)
    Those people who think they know everything are a great annoyance to those of us who do.
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Telent is not just about sending and receiving text.

    A Telnet client is a "remote console" or terminal emulation program and telnetlib will help with writing that.

    The telnet server is another issue. For a generic solution you must create a shell environment on the server for the commands to be run in. Without that your remote commands will not work as expected in all cases. Redirecting stdin and stdout for the subprocess is a start but shell programs are more than that as a lot of programs read and write directly to the shell.

    All the OS' have decent complete Telnet servers. If your plan is to run just any command then use those. They can usually be mapped to any free port.

    grim;
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    WA
    Posts
    29
    Rep Power
    0
    Originally Posted by Grim Archon
    Telent is not just about sending and receiving text.

    A Telnet client is a "remote console" or terminal emulation program and telnetlib will help with writing that.

    The telnet server is another issue. For a generic solution you must create a shell environment on the server for the commands to be run in. Without that your remote commands will not work as expected in all cases. Redirecting stdin and stdout for the subprocess is a start but shell programs are more than that as a lot of programs read and write directly to the shell.

    All the OS' have decent complete Telnet servers. If your plan is to run just any command then use those. They can usually be mapped to any free port.

    grim;

    Well, the purpose of this project was to expand my socket abilities.

    I realize that telnet isn't just sending and receiving text, but my 'server' IS passing commands to bash using the subprocess.call module.

    The problem exists when the server sends the command to bash, and then outputs the data.
    I need it to send the data BACK to the client to be outputed.

    Thanks grim,
    Poz
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You can redirect stdout etc. to any file like object (usually objects with a write() method). If you feel like wrapping your programs socket IO in a class then you could redirect stdout to this class and have it sent to the client. Personally I like this design .

    You could also do this with the StringIO modules and send the redirected output though your socket easily enough. Check out the StringIO module for more info:

    http://python.org/doc/2.4.1/lib/module-StringIO.html

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo