Create new processes over telnet in XP

Godzilla godzillaismad at gmail.com
Sat Mar 24 01:44:15 CET 2007


On Mar 24, 12:57 am, "Rob Wolfe" <r... at smsnet.pl> wrote:
> Godzilla wrote:
> > Hello,
>
> > How do you create/spawn new processes in XP over telnet using python?
> > I.e. I would like to create a new process and have it running in the
> > background... when I terminate the telnet connection, I would what the
> > spawned processes to keep running until I shut it off...
>
> > I got the os.popen method to spawn a new process running in the
> > backgroun, but not over telnet... tried os.popen[2, 3, 4] and also
> > subprocesses.popen without any luck...
>
> I don't know what kind of OS there is on that remote host you telnet
> to.
> The idea boils down to appropriate using of methods
> `read_until` and `write` from class `telnetlib.Telnet`.
>
> For more complicated stuff you can consider using pyexpect.
>
> Here is a small example of connecting to HP-UX.
> You can adjust that to your needs.
>
> <code>
> import telnetlib, time
>
> def login(tn, login, passwd, prompt):
>     tn.read_until("login: ")
>     tn.write(login + "\n")
>     if passwd:
>         tn.read_until("Password: ")
>         tn.write(passwd + "\n")
>     tn.read_until(prompt)
>     time.sleep(2)
>     print "logged in"
>
> def run_proc(tn, progname):
>     tn.write("nohup %s &\n" % progname)
>     tn.write("exit\n")
>     print "program <%s> running" % progname
>
> def kill_proc(tn, login, prompt, progname):
>     tn.write("ps -u %s\n" % login)
>     buf = tn.read_until(prompt)
>     pid = get_pid(buf, progname)
>     if not pid:
>         print "program <%s> not killed" % progname
>         tn.write("exit\n")
>         return
>     tn.write("kill -TERM %s\n" % pid)
>     tn.write("exit\n")
>     print "program <%s> killed" % progname
>
> def get_pid(buf, progname):
>     pid, comm = None, None
>     for line in buf.split("\n"):
>         try:
>             pid, _, _, comm = line.split()
>         except ValueError:
>             continue
>         if comm == progname:
>             return pid
>
> tn = telnetlib.Telnet(HOST, PORT)
> #tn.set_debuglevel(1)
> login(tn, "login", "passwd", "/home/user")
> run_proc(tn, "python ~/test.py")
> #kill_proc(tn, "login", "/home/user", "python")
> </code>
>
> --
> HTH,
> Rob

Thanks guys for your input...

Rob, I will give your example a go soon and tell you how i go...

Have a nice day...




More information about the Python-list mailing list