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



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,

.



Relevant Pages

  • Re: Button list control
    ... can load all DLL's, found in a given directory, and the plug-ins will register themselves for further use. ... Implementing 7zip, or some other new compression format, only requires to add the according DLL to the app's directory. ... The host application can ... the user's commands and parameters back to the plugin. ...
    (comp.lang.pascal.delphi.misc)
  • RE: sftp question
    ... Passwordless login is kind of unsafe, one way to login without being ... Prompted repeteadly is to use ssh-agent ... On host A from where you want to connect to another machine host B ... From the same shell you can do ssh as many times without being prompted ...
    (SSH)
  • Re: remsh a function in the current script
    ... Assuming your shell on the remote host is ksh, ... If your shell on the remote host isn't ksh, ...
    (comp.unix.shell)
  • !!! Path to docroot not valid: public
    ... We are working on an application which is hosted on Slice. ... The application was previously working fine but the host went down. ... I have tried the following commands by logging in as root. ... Path to pid file not valid: ...
    (comp.lang.ruby)
  • /etc/resolv.conf advice
    ... If I edit resolv.conf to manually input my ISPs DNS server, all commands ... resolv.conf entry is reset by DHCP to 192.168.2.1. ... What has happened to stop host and dig resolving using 192.168.2.1? ...
    (uk.comp.os.linux)