Results 1 to 5 of 5

Thread: Script help

  1. #1
    Join Date
    Oct 2007
    Beans
    338

    Script help

    Good morning,
    I find myself having to do several things repeatedly(across @400 machines this time) and would Love to automate this task as much as possible. The following is a list
    Note: this will need to be done by ip address
    1. Logon as root
    2. copy an existing script from my pc to the root of another
    3. change permissions of this script
    4. Execute said script

    Not asking to much is it..... Hahaha Seriously though any help Greatly appreciated.

    Chad
    Last edited by chadk5utc; October 15th, 2012 at 02:21 AM. Reason: Resolved

  2. #2
    Join Date
    May 2007
    Beans
    2,342

    Re: Script help

    Hi,

    Maybe have a look at using ssh/expect, something like this:-

    Code:
    #!/bin/bash
    HOST="localhost"
    USER="myuname"
    PASS="mypassword"
    
    VAR=$(expect -c "
    spawn ssh $USER@$HOST
    expect \"password:\"
    send \"$PASS\r\"
    expect \"\\\\$\"
    send \"ls\r\"
    expect -re \"$USER.*\"
    send \"logout\"
    ")
    
    echo "==============="
    echo "$VAR"
    Maybe just make two scripts, the one above and the second that calls this script with ip address/password as parameter.

    On my servers I have a script in rc.local that checks if updates are on the update server (local to network) and calls the attached script if necessary.

    Regards
    Ian Dobson
    Walking on water and writing software to specification is easy if they're frozen.

    My corner of the internet http://www.planet-ian.com

  3. #3
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Script help

    I would use keys, with a good strong passphrase, for authentication and disable password log in.

    Then when preparing to work with the 400 or so computers, load the key first into the agent using ssh-add. Then you can automate the transfer and execution of the script.

    Code:
    scp -i /path/key_rsa /path/to/script.sh root@$HOST:/tmp/
    ssh -i /path/key_rsa root@$HOST /tmp/script.sh
    I'm pretty sure that the script will carry over its permissions in the copy.
    Last edited by Lars Noodén; October 13th, 2012 at 01:55 PM. Reason: typo

  4. #4
    Join Date
    Jun 2011
    Beans
    357

    Re: Script help

    Forgive me, but if you need to do this exact same series of steps for around 400 computers I feel you are doing it backward. Instead of having your local machine send a script to these machines, why not have the machines download the script from you?

    Assuming you have OpenSSH server running on your local machine (or if you have a server anywhere that you feel can host the script) you could put a crontab on your 400 machines that looks like this:

    Code:
    #!/bin/bash
    
    # wait for a random period of time no more than one hour
    sleep_time=$(($RANDOM % 3600))
    sleep $sleep_time
    
    # save the previous script used
    mv command_script command_script-backup
    # download new script
    scp your-hostname:scriscript_name command_script
    
    # don't run it if the script is hasn't changed
    cmp command_script command_script-backup && exit 1
    
    chmod 755 command_script
    ./command_script
    Then enable a cron job to run the script for you as root once every hour/day/week. Just make sure that the remote machines
    1) Have the script installed
    2) Have the proper crontab entry
    3) Have permission to access your machine or the server where the new script will be.

  5. #5
    Join Date
    Oct 2007
    Beans
    338

    Re: Script help

    Hi and thanks for the replies. I will at some point setup a dedicated updates server for doing this as it will be much easier. Im all for working smarter not harder. Ive taken this challenge on myself as some of the others I work with would sooner do it all by hand one at a time, I think it foolish as we all have tons of other things/projects to take care of. Once in place the script being sent will backup several things on remote machines across the country then we have to retrieve and store these files on yet another remote system. Thanks again for the replies I will gladly take any and all suggestions.

    Chad

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •