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

Discussion in 'Python' started by gavino, Jul 1, 2006.

  1. gavino

    gavino Guest

    This seems easy but I have been asking tcl and python IRC chat all day
    and no one gave an answer.
    I have 100 servers which need a new backup server added to a text file,
    and then the backup agent restarted.
    If I have a list of the servers, all with same root password, and the
    connection is ssh.
    How do I connect to the server, cat the line to the config file, stop
    adn start the agent, and then do same to next server in list and
    iterate through the 100 servers.
    What script would allow this?
    gavino, Jul 1, 2006
    #1
    1. Advertising

  2. gavino

    Ravi Teja Guest

    gavino wrote:
    > This seems easy but I have been asking tcl and python IRC chat all day
    > and no one gave an answer.
    > I have 100 servers which need a new backup server added to a text file,
    > and then the backup agent restarted.
    > If I have a list of the servers, all with same root password, and the
    > connection is ssh.
    > How do I connect to the server, cat the line to the config file, stop
    > adn start the agent, and then do same to next server in list and
    > iterate through the 100 servers.
    > What script would allow this?


    Try pxssh from pexpect. Look at the sample in the page.
    http://pexpect.sourceforge.net/pxssh.html
    It will allow you to automate ssh interactions.
    Ravi Teja, Jul 1, 2006
    #2
    1. Advertising

  3. gavino

    LittlePython Guest

    Does this require a ssh client or server?

    I use authpf to open up my PF firewall for internet use. Generally I just
    open up putty and make a connection to the FW ( which open the ports) and
    minize it till I am done. When I close putty authpf losses the ssh session
    heartbeat and will then instruct the FW to close the ports. I would love to
    write a simple client to do this form me instead of using putty. Do you
    think these mods would work for me on a windows platform?

    Thx

    "Ravi Teja" <> wrote in message
    news:...
    >
    > gavino wrote:
    > > This seems easy but I have been asking tcl and python IRC chat all day
    > > and no one gave an answer.
    > > I have 100 servers which need a new backup server added to a text file,
    > > and then the backup agent restarted.
    > > If I have a list of the servers, all with same root password, and the
    > > connection is ssh.
    > > How do I connect to the server, cat the line to the config file, stop
    > > adn start the agent, and then do same to next server in list and
    > > iterate through the 100 servers.
    > > What script would allow this?

    >
    > Try pxssh from pexpect. Look at the sample in the page.
    > http://pexpect.sourceforge.net/pxssh.html
    > It will allow you to automate ssh interactions.
    >
    LittlePython, Jul 1, 2006
    #3
  4. gavino

    gavino Guest

    list of servers L.txt
    #cat L.txt
    config file is /var/bkupexec/agent.cfg need to add "tell epobackup" to
    bottom of file
    # cat "tell epobackup" >> /var/bkupexec/agent.cfg
    agent is /etc/init.d/agent.ini stop (and then start)
    # /etc/init.d/agent.init stop
    # /etc/init.d/agent.init start
    os=redhat ent 4ES
    I intend to use root password which is same for all 100 servers.
    #now I'm stuck

    Ravi Teja wrote:
    > gavino wrote:
    > > This seems easy but I have been asking tcl and python IRC chat all day
    > > and no one gave an answer.
    > > I have 100 servers which need a new backup server added to a text file,
    > > and then the backup agent restarted.
    > > If I have a list of the servers, all with same root password, and the
    > > connection is ssh.
    > > How do I connect to the server, cat the line to the config file, stop
    > > adn start the agent, and then do same to next server in list and
    > > iterate through the 100 servers.
    > > What script would allow this?

    >
    > Try pxssh from pexpect. Look at the sample in the page.
    > http://pexpect.sourceforge.net/pxssh.html
    > It will allow you to automate ssh interactions.
    gavino, Jul 1, 2006
    #4
  5. gavino

    Ravi Teja Guest

    LittlePython wrote:
    > Does this require a ssh client or server?
    >
    > I use authpf to open up my PF firewall for internet use. Generally I just
    > open up putty and make a connection to the FW ( which open the ports) and
    > minize it till I am done. When I close putty authpf losses the ssh session
    > heartbeat and will then instruct the FW to close the ports. I would love to
    > write a simple client to do this form me instead of using putty. Do you
    > think these mods would work for me on a windows platform?
    >
    > Thx


    Pexpect needs a POSIX system. However, you can use Cygwin.

    >From the website, "Pexpect does not currently work on the standard

    Windows Python (see the pty requirement); however, it seems to work
    fine using Cygwin."
    Ravi Teja, Jul 1, 2006
    #5
  6. gavino

    LittlePython Guest

    thx,


    "Ravi Teja" <> wrote in message
    news:...
    > LittlePython wrote:
    > > Does this require a ssh client or server?
    > >
    > > I use authpf to open up my PF firewall for internet use. Generally I

    just
    > > open up putty and make a connection to the FW ( which open the ports)

    and
    > > minize it till I am done. When I close putty authpf losses the ssh

    session
    > > heartbeat and will then instruct the FW to close the ports. I would love

    to
    > > write a simple client to do this form me instead of using putty. Do you
    > > think these mods would work for me on a windows platform?
    > >
    > > Thx

    >
    > Pexpect needs a POSIX system. However, you can use Cygwin.
    >
    > >From the website, "Pexpect does not currently work on the standard

    > Windows Python (see the pty requirement); however, it seems to work
    > fine using Cygwin."
    >
    LittlePython, Jul 1, 2006
    #6
  7. gavino

    Ravi Teja Guest

    gavino wrote:
    > list of servers L.txt
    > #cat L.txt
    > config file is /var/bkupexec/agent.cfg need to add "tell epobackup" to
    > bottom of file
    > # cat "tell epobackup" >> /var/bkupexec/agent.cfg
    > agent is /etc/init.d/agent.ini stop (and then start)
    > # /etc/init.d/agent.init stop
    > # /etc/init.d/agent.init start
    > os=redhat ent 4ES
    > I intend to use root password which is same for all 100 servers.
    > #now I'm stuck


    Read L.txt from (not your shell as you seem to be doing) Python and
    connect to each one from pxssh, and send the commands you listed. I am
    afraid, you will have to readup and experiment on a smaller scale. The
    sample I pointed you to is a rather simple one and you don't seem to
    have even tried it.
    Ravi Teja, Jul 1, 2006
    #7
  8. gavino

    Guest

    I would sugest looking at http://pexpect.sourceforge.net/ The Expect
    metalanguage was specifically designed for the kind of things you are
    trying to do. I used it recently on a project to configure 25
    instances of an application, remotly, half over ssh half over telnet.
    , Jul 1, 2006
    #8
  9. gavino

    Paul McGuire Guest

    "gavino" <> wrote in message
    news:...
    > This seems easy but I have been asking tcl and python IRC chat all day
    > and no one gave an answer.
    > I have 100 servers which need a new backup server added to a text file,
    > and then the backup agent restarted.
    > If I have a list of the servers, all with same root password, and the
    > connection is ssh.
    > How do I connect to the server, cat the line to the config file, stop
    > adn start the agent, and then do same to next server in list and
    > iterate through the 100 servers.
    > What script would allow this?
    >


    Could this project help you? http://sourceforge.net/projects/distribulator/

    It sounds similar to what you describe.

    -- Paul
    Paul McGuire, Jul 2, 2006
    #9
  10. * gavino (2006-07-01 11:34 +0000)
    > This seems easy but I have been asking tcl and python IRC chat all day
    > and no one gave an answer.
    > I have 100 servers which need a new backup server added to a text file,
    > and then the backup agent restarted.
    > If I have a list of the servers, all with same root password, and the
    > connection is ssh.
    > How do I connect to the server, cat the line to the config file, stop
    > adn start the agent, and then do same to next server in list and
    > iterate through the 100 servers.
    > What script would allow this?


    Tentakel
    Thorsten Kampe, Jul 2, 2006
    #10
  11. Re: I have 100 servers which need a new backup server added to atext file, and then the backup agent restarted.

    gavino wrote:
    > This seems easy but I have been asking tcl and python IRC chat all day
    > and no one gave an answer.
    > I have 100 servers which need a new backup server added to a text file,
    > and then the backup agent restarted.
    > If I have a list of the servers, all with same root password, and the
    > connection is ssh.
    > How do I connect to the server, cat the line to the config file, stop
    > adn start the agent, and then do same to next server in list and
    > iterate through the 100 servers.
    > What script would allow this?
    >


    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,
    Ove Pettersen, Jul 2, 2006
    #11
  12. Re: I have 100 servers which need a new backup server added to a textfile, and then the backup agent restarted.

    g> This seems easy but I have been asking tcl and python IRC chat all day
    g> and no one gave an answer.
    g> I have 100 servers which need a new backup server added to a text file,
    g> and then the backup agent restarted.
    g> If I have a list of the servers, all with same root password, and the
    g> connection is ssh.
    g> How do I connect to the server, cat the line to the config file, stop
    g> adn start the agent, and then do same to next server in list and
    g> iterate through the 100 servers.
    g> What script would allow this?

    Maybe webmin could help. http://www.webmin.com/
    You can manage cluster of servers at once.

    Petr Jakes
    =?windows-1250?Q?Petr_Jake=9A?=, Jul 2, 2006
    #12
  13. Ove Pettersen <> wrote:
    > for server in "server1 server2 server3 .... server100"; do


    Two comments:

    1. Leave out the quotes(!)

    2. Either iterate as
    for server in $(seq -fserver%g 100); do
    or, probably better
    for server in $(cat server-list); do
    Michael Abbott, Jul 2, 2006
    #13
  14. gavino

    Guest

    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,
    , Jul 3, 2006
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    5
    Views:
    368
    Guest
    Mar 15, 2005
  2. Replies:
    0
    Views:
    273
  3. fred
    Replies:
    3
    Views:
    276
    Zifud
    Mar 17, 2005
  4. Replies:
    5
    Views:
    882
  5. Replies:
    0
    Views:
    577
Loading...

Share This Page