Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

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

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


hishaam.ab at gmail

Aug 11, 2008, 3:39 AM

Post #1 of 4 (550 views)
Permalink
How to execute commands in internal zones of solaris using python running from the global zone ?

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
--
http://mail.python.org/mailman/listinfo/python-list


ironfroggy at gmail

Aug 11, 2008, 7:42 AM

Post #2 of 4 (505 views)
Permalink
Re: How to execute commands in internal zones of solaris using python running from the global zone ? [In reply to]

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.

On Mon, Aug 11, 2008 at 6:39 AM, Hishaam <hishaam.ab [at] gmail> 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?
>
>
> Hishaam
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://ironfroggy-code.blogspot.com/
--
http://mail.python.org/mailman/listinfo/python-list


news1234 at free

Aug 13, 2008, 12:46 AM

Post #3 of 4 (491 views)
Permalink
Re: How to execute commands in internal zones of solaris using python running from the global zone ? [In reply to]

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



--
http://mail.python.org/mailman/listinfo/python-list


news1234 at free

Aug 13, 2008, 2:22 PM

Post #4 of 4 (490 views)
Permalink
Re: How to execute commands in internal zones of solaris using python running from the global zone ? [In reply to]

> 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
>
>
>
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.