0

I have a Bash Script which I want execute the commands like:

  1. Download apache service
    sudo apt-get install apache2 -y > /dev/null 2>&1
    
  2. Download mysql service
    sudo apt install mysql-server -y > /dev/null 2>&1
    

But this Bash Script are out of server where that two commands will be executed.

For example, Bash Script file are on server 192.168.10.2 and remote server is IP 172.143.20.4

Script will connect to that server automatically via ssh (no prompt password), but NO use sshpass (becouse command not available on server where Bash Script file are allocated).

I tried multiple things for Login without promp (and all failed):

How I can do that (login automatically and execute that two commands)?

1
  • 1
    Hello and welcome to StackExchange. Your approach can be simplified by 2 things. Make sure you can ssh to the remote machine using public key authentication so you are not prompted for a password. If that is fixed try ssh [email protected] 'bash -s' < bash_script. Though ssh'ing as root user is also highly discouraged. Jun 6, 2023 at 18:40

1 Answer 1

0

Update: After re-reading the question, it has become apparent that I've missed the bit about there being a server in the middle. I would move the setup script to my own machine and apply it using a ProxyJump parameter. Answer updated to reflect that. All that would be required is to add the pubkey to the servers.

I personally like executing one thing only, so whenever there's a need to for - I place all things in a script and execute said script. So there could be a script ~/git/server-provisioning/script-to-upload.sh:

#!/usr/bin/env bash 

set -euo pipefail
IFS=$'\n\t'

apt-get install -y apache2
apt-get install -y mysql-server

And you could have a ~/git/server-provisioning/setup-remote-server.sh script:

declare scriptFile="~/git/server-provisioning/script-to-upload.sh"
[ -x "$scriptFile ] || chmod 700 "$scriptFile"
scp -o StrictHostKeyChecking=no "scriptFile" "$1":/tmp/
echo "myHardPassword" | \
    ssh "$1" -tt \
        -o StrictHostKeyChecking=no \
        "sudo bash /tmp/script-to-upload.sh > /dev/null 2>&1"

And place your host in in ~/.ssh/config:

Host server1
    Hostname 172.143.20.4
    ProxyJump 192.168.0.2
    User root

Make the ~/git/server-provisioning/setup-remote-server.sh executable with

chmod 700 ~/git/server-provisioning/setup-remote-server.sh

Execute the setup script:

./git/server-provisioning/setup-remote-server.sh server1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .