diff --git a/SSH-VPN-VNC/connect2jet.sh b/SSH-VPN-VNC/connect2jet.sh old mode 100644 new mode 100755 index 5d771fa596fe49b224d81e0a73fe29dbde828677..28cd33d89c3163a7bebd2c00513bf91647d5e35e --- a/SSH-VPN-VNC/connect2jet.sh +++ b/SSH-VPN-VNC/connect2jet.sh @@ -1,18 +1,19 @@ #!/bin/bash # By Michael Blaschek # Date 18.01.2021 -# CC BY 4.0 International +# CC BY 4.0 International # University of Vienna, Austria # Description: # Connect to Jet via a gateway server -help(){ - cat << EOF -$0 -g [gateway] -p [port] user@remote +help() { + cat <<EOF +$0 -g [gateway] -p [port] -r [remote-port] user@remote Options: - -g [gateway] gateway server, e.g. user@login.univie.ac.at - -p [port] port to forward from jet to local, egv VNC port + -g [gateway] gateway server, e.g. user@login.univie.ac.at + -p [port] port to forward from jet to local, egv VNC port + -r [port] remote port if not the same as port Example: $0 -g [U:Account-Username]@login.univie.ac.at [Jet-Username]@jet01.img.univie.ac.at @@ -20,15 +21,18 @@ Example: EOF } -while getopts "g:p:h" flag; -do - case "${flag}" in - g ) gateway=${OPTARG}; ;; - p ) port=${OPTARG}; ;; - h|* ) help; exit 0;; - esac +while getopts "g:p:r:h" flag; do + case "${flag}" in + g) gateway=${OPTARG} ;; + p) port=${OPTARG} ;; + r) rport=${OPTARG} ;; + h | *) + help + exit 0 + ;; + esac done -shift $((OPTIND-1)) +shift $((OPTIND - 1)) command="" remote=$1 script=$(basename $0) @@ -40,25 +44,56 @@ if [ "${remote}" == "" ]; then fi if [ -n "${port}" ]; then - echo "Forwarding Port: $port" - command="-L ${port}:localhost:${port} " + # -L local_port:destination_server_ip:remote_port + if [ -n "${rport}" ]; then + echo "Forwarding Port: $rport to $port" + command="-L ${port}:localhost:${rport} " + else + echo "Forwarding Port: $port to $port" + command="-L ${port}:localhost:${port} " + rport=$port # make sure we use this + fi + +fi + +# Check if names are in .ssh/config +remote_status=false +gateway_status=false +if [ -e $HOME/.ssh/config ]; then + # check if hosts are there + cat $HOME/.ssh/config | grep -i 'Host ${remote}' > /dev/null + if [ $? -eq 0 ]; then + remote_status=true + fi + if [ -n ${gateway} ]; then + cat $HOME/.ssh/config | grep -i 'Host ${gateway}' > /dev/null + if [ $? -eq 0 ]; then + gateway_status=true + fi + fi fi if [ -n "${gateway}" ]; then - echo "$gateway" | grep '@' > /dev/null - if [ $? -eq 1 ]; then - echo "Please give: [user]@[server], not $gateway" - exit 1 + echo "$gateway" | grep '@' >/dev/null + if [ $? -eq 1 ] && [ ! $gateway_status ]; then + echo "Could fail if not: [user]@[server], trying: $gateway" + fi + if [ -n "${port}" ]; then + echo "Using gateway: $gateway to $remote " + echo "Port Forwarding via the gateway: " + ssh -L ${port}:localhost:${port} -t $gateway ssh -L ${port}:${gateway}:${rport} ${remote} + echo "$(date) | ssh -L $port:localhost:12345 -t $gateway 'ssh -L 12345:${gateway}:${rport} ${remote}'" >>.${script/.sh/.log} else echo "Using gateway: $gateway to $remote " - ssh $command -t $gateway 'ssh ${command} ${remote}' - echo "$(date) | ssh $command -t $gateway 'ssh ${command} ${remote}'" >> .${script/.sh/.log} + ssh $command -t $gateway ssh ${command} ${remote} + echo "$(date) | ssh $command -t $gateway 'ssh ${command} ${remote}'" >>.${script/.sh/.log} fi else + if [ ! $remote_status ] ; then + echo "Could fail if not: [user]@[server]" + fi echo "Direct connection to: ${remote}" ssh ${command} ${remote} - echo "$(date) | ssh ${command} ${remote}" >> .${script/.sh/.log} + echo "$(date) | ssh ${command} ${remote}" >>.${script/.sh/.log} fi -echo "Command Log in .${script/.sh/.log}" - - +echo "Command Log in .${script/.sh/.log}" \ No newline at end of file