write a shell script to ssh to a remote machine and execute commands

I have two questions:

  • There are multiple remote linux machines, and I need to write a shell script which will execute the same set of commands in each machine. (Including some sudo operations). How can this be done using shell scripting?
  • When ssh'ing to the remote machine, how to handle when it prompts for RSA fingerprint authentication.
  • The remote machines are VMs created on the run and I just have their IPs. So, I cant place a script file beforehand in those machines and execute them from my machine.


    There are multiple remote linux machines, and I need to write a shell script which will execute the same set of commands in each machine. (Including some sudo operations). How can this be done using shell scripting?

    You can do this with ssh, for example:

    #!/bin/bash
    USERNAME=someUser
    HOSTS="host1 host2 host3"
    SCRIPT="pwd; ls"
    for HOSTNAME in ${HOSTS} ; do
        ssh -l ${USERNAME} ${HOSTNAME} "${SCRIPT}"
    done
    

    When ssh'ing to the remote machine, how to handle when it prompts for RSA fingerprint authentication.

    You can add the StrictHostKeyChecking=no option to ssh:

    ssh -o StrictHostKeyChecking=no -l username hostname "pwd; ls"
    

    This will disable the host key check and automatically add the host key to the list of known hosts. If you do not want to have the host added to the known hosts file, add the option -o UserKnownHostsFile=/dev/null .

    Note that this disables certain security checks , for example protection against man-in-the-middle attack. It should therefore not be applied in a security sensitive environment.


    There are a number of ways to handle this.

    My favorite way is to install http://pamsshagentauth.sourceforge.net/ on the remote systems and also your own public key. (Figure out a way to get these installed on the VM, somehow you got an entire Unix system installed, what's a couple more files?)

    With your ssh agent forwarded, you can now log in to every system without a password.

    And even better, that pam module will authenticate for sudo with your ssh key pair so you can run with root (or any other user's) rights as needed.

    You don't need to worry about the host key interaction. If the input is not a terminal then ssh will just limit your ability to forward agents and authenticate with passwords.

    You should also look into packages like Capistrano. Definitely look around that site; it has an introduction to remote scripting.

    Individual script lines might look something like this:

    ssh remote-system-name command arguments ... # so, for exmaple,
    ssh target.mycorp.net sudo puppet apply
    

    Install sshpass using, apt-get install sshpass then edit the script and put your linux machines IPs, usernames and password in respective order. After that run that script. Thats it ! This script will install VLC in all systems.

    #!/bin/bash
    SCRIPT="cd Desktop; pwd;  echo -e 'PASSWORD' | sudo -S apt-get install vlc"
    HOSTS=("192.168.1.121" "192.168.1.122" "192.168.1.123")
    USERNAMES=("username1" "username2" "username3")
    PASSWORDS=("password1" "password2" "password3")
    for i in ${!HOSTS[*]} ; do
         echo ${HOSTS[i]}
         SCR=${SCRIPT/PASSWORD/${PASSWORDS[i]}}
         sshpass -p ${PASSWORDS[i]} ssh -l ${USERNAMES[i]} ${HOSTS[i]} "${SCR}"
    done
    
    链接地址: http://www.djcxy.com/p/17512.html

    上一篇: 我如何放置已经

    下一篇: 编写一个shell脚本ssh到远程机器并执行命令