0

I have a bash script, that runs a few commands. These commands take input, and I'd like to be able to automate this, as the input is always the same.

Is it possible to send these lines, one a time, when asked for?

Y
{JustPressEnter}
{JustPressEnter}
{JustPressEnter}
yes
APassword

The full command is this (actual IP removed)

apt-get install ssh-client \
    && mkdir /var/www/.ssh/ \
    && chown www-data:www-data /var/www/.ssh/ \
    && sudo -u www-data ssh-keygen -t rsa \
    && cat /var/www/.ssh/id_rsa.pub | ssh root@1.2.3.4 'cat >> .ssh/authorized_keys' \
    && sudo -u www-data touch /var/www/.ssh/known_hosts \
    && sudo -u www-data ssh-keygen -R 1.2.3.4 \
    && sudo -u www-data ssh-keyscan -H 1.2.3.4 >> /var/www/.ssh/known_hosts
TMH
  • 6,096
  • 7
  • 51
  • 88

1 Answers1

2

Two different answers:

  • Use expect to automate commands. This answer shows an example of expect being used to automate apt-get with password input. Here's an article that talks at length about expect.

  • Write each command in batch mode. ssh-keygen can accept all those things as parameters; apt-get install -y packagename will assume "yes" on every prompt. If you write your code right, no keyboard interaction needs to take place.

Community
  • 1
  • 1
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Thanks :). I used the `-y` apt-get flag, and managed to sort `ssh-keygen` out using parameters. All I have left to do now is get the password auto-inputted to the ssh command. I'll look at expect and try and get it working with that. – TMH Jul 21 '15 at 10:01