SSH automatization

Discussion in 'macOS' started by Primalot, Feb 28, 2017.

  1. Primalot macrumors newbie

    Joined:
    Feb 28, 2017
    #1
    Sorry for my bad english. But i neeed your help guys!
    I need to login via ssh to ubuntu servers, easy yeah?
    But I have some troubles:
    I need to log at 200 servers [​IMG][​IMG][​IMG]... its possible manually but toooo long and boring here is my routine:
    1) ssh -i "PathToKey/Key" ubuntu@serverAddress
    Its always asking me to allow or not, and I have to manually type yes/no
    2) okey, we are connected at some sec, then i need to run script at ubuntu server, to launch/relaunch process:
    #!/bin/bash
    nohup myprocess &
    (using nohup to allow running my procces even if i close ssh session)
    3) close my terminal window
    and all this again and again...

    Im asking your help to automate all this hell, maybe create file with the list of addresses, maybe connect and one by one, or maybe 5 at one time, typing this "yes", not big timeout (to give time to connect to server), run script, again timeout over 3-5sec to give time to run script, close this session and start another till the list of my servers wont end...

    I understand its possible via apple script or automator but i'm noob who needs help)
    Thanks for you attention
     
  2. Daniel Santos macrumors newbie

    Joined:
    Nov 1, 2012
    Location:
    Portugal
    #2
    for one given server you could do :

    ssh -i "PathToKey/Key" ubuntu@serverAddress "nohup myprocess &"

    using bash scripting and awk you can make a program that reads a file with a line for each "PathToKey/Key" / user / serverAddress and execute the ssh command for each one.

    Hope that helps
     
  3. Primalot thread starter macrumors newbie

    Joined:
    Feb 28, 2017
    #3
    I understand it but i dont know how to do it)
     
  4. Daniel Santos, Mar 8, 2017
    Last edited: Mar 8, 2017

    Daniel Santos macrumors newbie

    Joined:
    Nov 1, 2012
    Location:
    Portugal
    #4
    As they always use to say : RTFM

    If you google : "bash scripting for begginers" and "awk tutorial" it should come up with something. I'll just leave these links below here for you:

    http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
    https://www.gnu.org/software/gawk/manual/gawk.html

    EDIT : Assuming you are not a bash scripter I am posting here a script to do what you want :

    1. the file batch_ssh_data.txt has a line with the parameters that vary in the ssh command. Each line has a set of 5 fields separated by the ';' character. You should edit this file putting in your data
    2. the file batch_ssh_generate_script.sh is the script that reads the previous file using awk and outputs the ssh commands with the data in it. By running this script you will have the following file:
    3. batch_ssh.sh which I am not attaching. This will be generated by the previous script and will contain the concrete ssh commands to launch your processes in the configured servers.

    Dont forget to make the file in number 2 executable before running it. And change the extensio to .sh (This forum does not allow the upload of .sh files)
    Then run the generated script in number 3.
     

    Attached Files:

Share This Page