IT How Tos

Home

About

Sitemap

Contact

How to Run a Command via an SSH Tunnel

  • This will explain how to run a command via an SSH tunnel by
    1. Opening the tunnel
    2. Running the command
    3. Closing the tunnel
  • Assuming the script is call sshtunncomm this script can be used by running following:
    • Please note that port forwarding to be handled via ssh config or other external
    sshtunncomm  
#!/bin/bash

function usage {
        echo "Usage $basename $0  
                Please note that port forwarding to be handled via ssh config or other external
        This requires backgcomm"
}

if [ -z "$2" ];then
        usage
else
    titl=sshop`date +%Y%m%d%H%M%S`
    tunsvr=$1
    comm="${@: 2}"
    backgcomm new ${titl} ssh ${tunsvr}
    sleep 2s # wait for ssh tunnel to establish
    ${comm}
    backgcomm stop ${titl}
fi