• 2 Posts
  • 36 Comments
Joined 9 months ago
cake
Cake day: June 3rd, 2024

help-circle




  • I’m not sure how to chain these 2 commands with &&, because the SSH command is being put in the background with &.

    This doesn’t work:

    SSH_CMD="ssh -N -L ${LOCAL_PORT}:127.0.0.1:${REMOTE_PORT} ${REMOTE_USER}@${REMOTE_HOST}"
    $SSH_CMD & && TEST=$(curl -o /dev/null -s -k -w "%{http_code}" -u "${USERNAME}:${PASSWORD}" "${URL}/dashboard/")
    SSH_PID=$!
    SSH_RESULT=$?
    echo $TEST
    

    Perhaps I don’t need it in the background - the goal was to establish the tunnel and then continue with the script without it hanging until the ssh command is canceled.



  • I stumbled on a possible cause, but more background is necessary to explain.

    The script actually creates an ssh tunnel (to the Traefik host) and then does the curl. So the code is like:

    SSH_CMD="ssh -N -L ${LOCAL_PORT}:127.0.0.1:${REMOTE_PORT} ${REMOTE_USER}@${REMOTE_HOST}"
    $SSH_CMD &
    SSH_PID=$!
    SSH_RESULT=$?
    
    TEST=$(curl -o /dev/null -s -k -w "%{http_code}" -u "${USERNAME}:${PASSWORD}" "${URL}/dashboard/")
    echo "${TEST}" #debug
    

    What I learned is that when i run the script, the tunnel is successfully created but the curl fails; but then if I run the script again a second tunnel is created and the curl works fine.



  • Here is the result of the script curling a known good URL (it still results in exit code 7 and thus a result of “000”), followed by a copy-paste of the curl command run in the shell (exited with “200”):

    $ ./test.sh
    curl -o /dev/null -s -k -w "%{http_code}" "https://i0.wp.com/www.notquitezen.co/wp-content/uploads/2022/07/Is-Happiness-an-Emotion.png"
    previous exit code: 7
    000
    $
    $ curl -o /dev/null -s -k -w "%{http_code}" "https://i0.wp.com/www.notquitezen.co/wp-content/uploads/2022/07/Is-Happiness-an-Emotion.png"
    200
    

  • Thanks for the suggestions.

    The script is pulling the values of USERNAME and PASSWORD from a .env. I added debug echo $USERNAME $PASSWORD in the script and it shows the correct values, so the script is pulling the values correctly and storing them in the vars correctly. I also added that echo to the subshell command, like:

    TEST=$(echo $USERNAME)
    echo $TEST
    

    …and the result was the correct USERNAME.

    The script does begin with #!/bin/bash.

    echo $PATH; which curl produces identical results when run from the shell and the script.