How to execute commands in internal zones of solaris using pythonrunning from the global zone ?

H

Hishaam

How to execute commands in internal zones of solaris using python
running from the global zone ?

i tried --
1> os.popen("zlogin <zone1>")
2> os.popen("zonename")

the 2nd command executes back into the global zone and not into the
internal zone
can anyone help?


Hishaam
 
C

Calvin Spealman

You might try subprocess, first of all. Use it to launch zlogin and
then treat it like a shell and write 'zonename\n' to its stdin, to
simulate running it as a user. This is a good bet, but I don't have
either available to try it. The subprocess documentation covers
invoking a process and writing to its input.

What you are doing now doesn't work, because you just invoke the two
processes and dont do anything with either of them. zlogin may not
have even completely initialized when you start the second command.
The technique you need is to start zlogin and then tell it to run the
next.
 
N

nntpman68

Hishaam said:
How to execute commands in internal zones of solaris using python
running from the global zone ?

i tried --
1> os.popen("zlogin <zone1>")
2> os.popen("zonename")

the 2nd command executes back into the global zone and not into the
internal zone
can anyone help?


Hishaam


Very probably you didn't want to start two pipes (two processes on your
host)



Can you ssh or rsh to zone 1?

Then you want probably something like:

os.popen('echo zonename | ssh <zone1>')


If not you could look at pexpect, though I didn't use it myself so far.
just look at the thread 'SSH utility' to get some examples.
you just had to replace 'ssh' with 'zlogin'

( http://www.noah.org/wiki/Pexpect )



Or you could try popen2, http://docs.python.org/lib/module-popen2.html

Strange thing is though, that popen doesn't seem to exist on my python.
Perhaps it's not a standard library.
No idea. I myself am rather new to python :-(


However if you want to try popen2 read also 17.4.2 'Flow Control Issues'
in order to be sure, that your program doesn't deadlock



[ z1_stdin , z1_stdout ] = os.popen2('ssh <zone1')
z1_stdin.write('zonename\n')
z1_stdout.readline()


bye N
 
N

nntpman68

Hmm, I was't reading enough.
popen2 can be imported with popen2

the more modern alternative to popen seems to be subprocess.Popen
http://docs.python.org/lib/node539.html

And here a short example:
#!/bin/env python
import sys
import os
import popen2

# default host / command
host="zone1"
command='uname -n'

# host / command via command line
# didn't have time to check whether
# shifting sys.argv is a common practice
if(len(sys.argv) > 1):
host=sys.argv[1]
if(len(sys.argv) > 2):
command=sys.argv[2]

# ============== solution 1 ============
# just passing command to command line of ssh / rsh / zlogin
pipe_in = os.popen('ssh ' + host + ' ' + command)
a = pipe_in.read()
print 'Result of solution 1',a

# a minor variation of solution 1 would be the next line
# in ssh the switch -T disables tty allocation and gets rid of a warning'
#pipe_in = os.popen('echo '+command+' | ssh -T' + host)

# =========== solution 2 =================
# using popen2
# if you do a little more than one command this might
# be prone to dead locks
# for more complex cases it's probably better
# to use one thread for sending the commands and one thread for
# collecting the responses
#

# if you want to see the warning remove it ;-_
[ z1_stdout , z1_stdin ] = popen2.popen2('ssh -T '+host)
z1_stdin.write(command + '\n')
z1_stdin.close()
a = z1_stdout.read()
print "Result of solution 2 is: ",a





Very probably you didn't want to start two pipes (two processes on your
host)



Can you ssh or rsh to zone 1?

Then you want probably something like:

os.popen('echo zonename | ssh <zone1>')


If not you could look at pexpect, though I didn't use it myself so far.
just look at the thread 'SSH utility' to get some examples.
you just had to replace 'ssh' with 'zlogin'

( http://www.noah.org/wiki/Pexpect )



Or you could try popen2, http://docs.python.org/lib/module-popen2.html

Strange thing is though, that popen doesn't seem to exist on my python.
Perhaps it's not a standard library.
No idea. I myself am rather new to python :-(


However if you want to try popen2 read also 17.4.2 'Flow Control Issues'
in order to be sure, that your program doesn't deadlock



[ z1_stdin , z1_stdout ] = os.popen2('ssh <zone1')
z1_stdin.write('zonename\n')
z1_stdout.readline()


bye N
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top