I have 100 servers which need a new backup server added to a text file, and then the backup agent restarted.

cmdrrickhunter@yaho.com conrad.ammon at gmail.com
Mon Jul 3 04:37:35 EDT 2006


Hehe, yeah, it does feel like swearing =)

That solution works, as long as the network behaves PERFECTLY and
you've allready configured the server to use your SSH key instead of a
password.  Expect, by its nature, waits until the right time to say
things

Sample pexpect code:
-------------  Code -------------
# assumes hosts is a list of hostnames
for host in hosts:
    child = pexpect.spawn("ssh root@%s" % host)
    child.expect ('Password:')
    child.sendline ("the correct root password")
    child.expect ('#') # root prompt
    child.sendline("echo 'the new line' >> /etc/config.file")
    child.expect('#')
    child.sendline("/etc/init.d/myservice restart")
    child.expect("#")
    child.sendline("logout")
-----------------------------

Or if you prefer a stronger split between data and code you can use
this code.  If you need even more functionality than that (such as
pausing for a few seconds after a command), you can start using
callable objects instead of tuples of text.

-------------   Code  ---------------
# commands is a list of (text-to-wait-for, response-to-send) pairs
commands = [ \
    ("Password:",   "the correct root password"),   \
    ("#",  "echo 'the new line' >> /etc/config.file"),  \
    ("#",  "/etc/init.d/myservice restart"), \
    ("#",  "logout") ]

for host in hosts:
   child = pexpect.spawn("ssh root@%s" % host)
   for cmd in commands:
      child.expect(cmd[0])
      child.sendline(cmd[1])
-------------------------------------------------

You can also tell pexpect to have a timeout on any expect() call.

Shell has its uses.  My personal experiance is shell's use is when you
dont expect the client to have a more powerful language installed, or
when a scripts duties are very limited.  If ANYTHING involves a space,
dont bother with shells, handling spaces will take more debugging than
the actual code.  For example, your quickly written code wouldn't work
because you have " inside ", when you needed single quotes.  And if any
of the commands issued have difficult parameters, properly escaping
shell strings is an utter nightmare.  A shell solution may work here,
but if the task gets any more complicated, then a new language is
needed.

I'm a toolkit programmer - I keep as many language as I can in my
toolkit.  Pexpect was 100% designed for this specific task, so why not
use it.  If I was parsing a text file, 9 times out of 10 I'll pull up
PERL, its ment for it.
Ove Pettersen wrote:
>
> I wouldn't use python at all (like swearing on this list???) .... simple
> shell-command is more than sufficient.
>
> for server in "server1 server2 server3 .... server100"
> do
>     ssh root@$server "(echo "new line" >> config.file; stop-command ;
> sleep 5 ; start-command)"
> done
>
> I would however have extended the procedure with a few more checks...
> Like:
> * only add new line to config-file if it isn't present
> * only do restart if config-file was modified
> 
> Regards,




More information about the Python-list mailing list