Create new processes over telnet in XP

G

Godzilla

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...

Any help will be appreciated... thankyou.
 
R

Rob Wolfe

Godzilla said:
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>
 
J

Jorgen Grahn

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...

Ssh -- or even rsh -- are better choices than telnet for these things.
For some reason, they are not standard in Windows, though.

ssh somewhere some command with arguments
rsh somewhere some command with arguments

compared to

telnet somewhere

and then performing expect-like things (basically simulating
someone typing "some command with arguments" in the telnet
session).
when I terminate the telnet connection, I would what the
spawned processes to keep running until I shut it off...

That's a function of the remote OS; what happens when its terminal
goes away is not under the control of the client side.

/Jorgen
 
L

Laurent Pointal

Jorgen said:
Ssh -- or even rsh -- are better choices than telnet for these things.
For some reason, they are not standard in Windows, though.

ssh somewhere some command with arguments
rsh somewhere some command with arguments

compared to

telnet somewhere

and then performing expect-like things (basically simulating
someone typing "some command with arguments" in the telnet
session).

+ for an sshd running as a service under XP, look at CopSSH.

+ hope started process doesn't want a GUI... else, look at UltraVNC running
as daemon, and port redirection using ssh.
That's a function of the remote OS; what happens when its terminal
goes away is not under the control of the client side.

Maybe the process starting job can be done by a Python program running as
Windows service and waiting for requests on a port (or Pyro object or Corba
object...).
No need for telnet/ssh connection, no logout problem.

Just care of possible security problems :)
 
S

Shane Geiger

This reminds me of something I once wanted to do: How can I install
Python in a totally non-gui way on Windows (without the use of VNC)? I
think I was telnetted into a computer (or something like that) and I was
unable to run the usual Python installer because it uses a GUI.





Laurent said:
Jorgen Grahn wrote:



+ for an sshd running as a service under XP, look at CopSSH.

+ hope started process doesn't want a GUI... else, look at UltraVNC running
as daemon, and port redirection using ssh.



Maybe the process starting job can be done by a Python program running as
Windows service and waiting for requests on a port (or Pyro object or Corba
object...).
No need for telnet/ssh connection, no logout problem.

Just care of possible security problems :)

--
Shane Geiger
IT Director
National Council on Economic Education
(e-mail address removed) | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
 
I

Irmen de Jong

Shane said:
This reminds me of something I once wanted to do: How can I install
Python in a totally non-gui way on Windows (without the use of VNC)? I
think I was telnetted into a computer (or something like that) and I was
unable to run the usual Python installer because it uses a GUI.

Python uses a MSI (microsoft installer) based installer on windows.
This was introduced in version 2.5 I believe.

For MSI installers there's the standard MSI-way to perform a "silent" install.
Google for it, I don't know what the command line switch(es) are, but they're there.

--Irmen
 
J

Jarek Zgoda

Irmen de Jong napisa³(a):
Python uses a MSI (microsoft installer) based installer on windows.
This was introduced in version 2.5 I believe.

2.4? I recall that we installed 2.4.2 this way on >500 machines some day
at my previous work.
 
G

Godzilla

Godzilla said:
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>

Thanks guys for your input...

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

Have a nice day...
 
G

Godzilla

Godzilla said:
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>

Rob, I would be logging into another XP machine to do some software
installation... the code you provided, correct me if I'm wrong, seems
to work under Unix/Linux. Any idea how to do the equivalent in XP?
 
T

Thomas Heller

Irmen said:
Python uses a MSI (microsoft installer) based installer on windows.
This was introduced in version 2.5 I believe.

For MSI installers there's the standard MSI-way to perform a "silent" install.
Google for it, I don't know what the command line switch(es) are, but they're there.

--Irmen
Even the Wise installer that was used to build the 2.3 and earlier versions
had command line switches to do silent installs.

THomas
 
R

Rob Wolfe

Godzilla said:
Rob, I would be logging into another XP machine to do some software

I was afraid of that. :)
installation... the code you provided, correct me if I'm wrong, seems
to work under Unix/Linux.

This part of running and killing processes, yes.
Any idea how to do the equivalent in XP?

You could use windows services, for example:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/115875

But I don't know details, because this is not my favourite OS. :)
 
L

Laurent Pointal

Shane Geiger a écrit :
This reminds me of something I once wanted to do: How can I install
Python in a totally non-gui way on Windows (without the use of VNC)? I
think I was telnetted into a computer (or something like that) and I was
unable to run the usual Python installer because it uses a GUI.

Take a look at PortablePython, this may be the easy solution...

http://www.portablepython.com/


A+

Laurent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top