Example for Sending Commands to a Currently Active Screen Session

If you are one of the people that use my Docker VPN Tunneling Tool this can be used to automate login by appending it to your portion of the script.

^M represents pressing enter if unfamiliar. I’ll probably forget, that’s why I’m creating this post.

#sh dockerVpnTool.sh $OPENCONNECT_VPN_ENDPOINT $LOCAL_DOCKER_ADDRESS $LOCAL_DOCKER_SSH_PORT $CONTAINER_NAME $IMAGE_NAME "$(declare -p DESTINATION_LIST)"

#Begin Customization
echo -e "\n\n1\n" | ./dockerVpnTool.sh $OPENCONNECT_VPN_ENDPOINT $LOCAL_DOCKER_ADDRESS $LOCAL_DOCKER_SSH_PORT $CONTAINER_NAME $IMAGE_NAME "$(declare -p DESTINATION_LIST)"

function connectToEndpoint()
{
    #Begin Endpoint Customization 
    COMMAND_TO_RUN="winpty docker exec $CONTAINER_NAME bash -c"
    COMMAND_TO_RUN_INTERACTIVE="winpty docker exec -it $CONTAINER_NAME bash -c"

    SESSION_NAME="session_vpnTool"
    USERNAME='username'
    PASSWORD='password'
    CONNECTION_PROFILE='connectionProfile'

    $COMMAND_TO_RUN "screen -d -m -S $SESSION_NAME openconnect $OPENCONNECT_VPN_ENDPOINT"
    $COMMAND_TO_RUN "screen -S $SESSION_NAME -p 0 -X stuff \"$CONNECTION_PROFILE^M\""
    $COMMAND_TO_RUN "screen -S $SESSION_NAME -p 0 -X stuff \"$USERNAME^M\""
    $COMMAND_TO_RUN "screen -S $SESSION_NAME -p 0 -X stuff \"$PASSWORD^M\""

    #If Debug is Needed
    #$COMMAND_TO_RUN_INTERACTIVE "screen -r"
}

connectToEndpoint

Leave a comment