#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Python script automation


    Need help sith pyton script for auto installation of RPM on number of redhat servers.

    so far i was successfully able to copy rpm/directory to single redhat server. i wants to know how we can transfer it to multiple server and upon transfer i can i install rpm and edit file ?

    #!/usr/bin/python
    import os
    os.system("scp -r xyz root@hostname:/root")

    above will copy xyz directory to single host.

    hints or conceptual idea would be greatly appericated.

    thank you
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    I'd use bash, not python.
    I'm sure you can
    read
    hostnames from a file as well.

    man bash

    Code:
    for h in host1 host2 
    do
      scp -r xyz root@$h:/root
    done
    If you must have python, roughly
    Code:
    import os
    hostnames = populate(a_file)  # list from a file
    for host in hostnames:
        os.system("scp -r xyz root@{}:/root".format(host))
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Thank you very much for your quick response. I tested but
    i am getting below error message while running as per you recommendation.

    did we need to import module related to "populate" ? If so what is that ?

    Traceback (most recent call lst):
    File "./xyz.py, line 3,in <module>
    hostnames=populate(a_file)
    NameError: name 'populate' is not defined.


    Also my ultimate goal is after i copy directories on various servers, that directory contains couple rpm's which i need to install on each server, open a specific file and search for word an xyz="xyz" and replace "xyz" with "hostname" and another line in the same file, starting with ddd="sss" and replace "sss" with "hostname" , restart deamons by /etc/init.d/xyz restart

    your help is very much appericated.
    thank you
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    You're supposed to write populate. I presume your list of host names is in a file. You'd read the file into a list.

    Suppose your hosts are a b and c.

    def populate():
    return ['a','b','c']


    would work.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    does not work. do you mind putting newly advised code with explanation ? thank you. a_file is where i am putting all hostnames. i found that if i use as below to copy directory to a multiple systems but i would like to avoide that as i like concept of reading all hosts from a single file and script read that file in order to login and than copy.
    os.system("scp -r xyz root@hostname:/root")
    s.system("scp -r xyz root@hostname1:/root")

    Also after directory being copied, i can i go to that xyz directory and run "rpm -ivh xyz.rpm" ( install a package ) or multiple commands like that ?

    thank you
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Code:
    def populate():
        with open('a_file','r') as inf:
            hostnames = inf.readlines()
        return [host.strip() for host in hostnames]
    For the other question, you can run ssh . You can run ssh from python. I still don't know why this extra python layer is useful.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo