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

nntpman68 news1234 at free.fr
Wed Aug 13 17:22:31 EDT 2008


 > Hishaam wrote:
 >> 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?

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




nntpman68 wrote:

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



More information about the Python-list mailing list