Search code examples
linuxbashshellsshautomation

When making an ssh connection through a shell script, how do I get a terminal logged into that connection after the script ends?


cd /path/to/folder
docker compose up -d
docker exec -it image_name bash -c "
    cd /path/to/folder
    exec bash
"

When I run this shell script everything works as expected. At the end of the execution, in the terminal where I ran the script, I am now accessing the docker environment.

But if i try this, adding the ssh connection:

#!/bin/bash

sshpass -p 'password' ssh -t user@host <<'ENDSSH'

timeout 10s echo "password" | sudo -S chmod a+rwx -R /path/to/folder || echo "Timeout reached"

echo "password" | sudo -S chmod a+rwx -R /path/to/folder2

cd /path/to/folder3
docker compose up -d
docker exec -it image_name bash -c "
    cd /path/to/folder4
    exec bash
"
ENDSSH

It gives an error. the script executes and the message that the container is running appears on the screen, but then the error "the input device is not a TTY" appears and the script execution ends.

I would like to access the docker environment via the terminal at the end of this script. How do I do this?


Solution

  • You need to provide the commands as a string instead of via standard input:

    #!/bin/bash
    
    sshpass -p 'password' ssh -t user@host "$(cat <<'ENDSSH'
    
    timeout 10s echo "password" | sudo -S chmod a+rwx -R /path/to/folder || echo "Timeout reached"
    
    echo "password" | sudo -S chmod a+rwx -R /path/to/folder2
    
    cd /path/to/folder3
    docker compose up -d
    docker exec -it image_name bash -c "
        cd /path/to/folder4
        exec bash
    "
    ENDSSH
    )"