Skip to content

Instantly share code, notes, and snippets.

@NTICompass
Last active September 17, 2016 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NTICompass/ca024e2c336eaa33beba to your computer and use it in GitHub Desktop.
Save NTICompass/ca024e2c336eaa33beba to your computer and use it in GitHub Desktop.
SSH magic tricks
#!/bin/bash
# Should we connect via IPv4 or IPv6?
if ping -c 1 ipv6.google.com &> /dev/null
then
SERVER="tron6"
else
SERVER="tron"
fi
# Media shares and mount points
SMB="//tron/Tron"
SMB_MOUNT="/mnt/tron/smb"
SSHFS="/home/nticompass"
SSHFS_MOUNT="/mnt/tron/ssh"
# Access to internal machines
USERNAME="nticompass"
HYPERCUBE="hypercube"
ROUTER="enigma"
# Script variables
SOCKET="/tmp/$SERVER.ssh"
PID=""
# SSH can do a bunch of fun stuff in "master mode"
# https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Tunnels#Adding_or_Removing_Tunnels_within_a_Multiplexed_Connection
# https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Multiplexing
# Sets the $PID variable to the pid of the ssh process (if it exists)
checkConnection(){
PID=""
file $SOCKET > /dev/null
if [ $? -eq 0 ]; then
SSHCONN=`ssh -S $SOCKET -O check $SERVER 2>&1`
if [ $? -eq 0 ]; then
PID=`echo $SSHCONN | awk '{print $3}'`
fi
fi
}
# If the connection is *not* runningm then open it in master mode
# and throw it into the background
openConnection(){
checkConnection
if [ "$PID" == "" ]; then
ssh -S $SOCKET -MfnNTY $SERVER
PID=`ssh -S $SOCKET -O check $SERVER 2>&1 | awk '{print $3}'`
fi
}
# Close open ssh connections
# Also unmounts any network shares
closeConnection(){
checkConnection
if [ "$PID" != "" ]; then
# Make sure to unmount everything before closing the connection
MOUNT=`mount | grep "$SMB"`
if [ $? -eq 0 ]; then
sudo umount $SMB_MOUNT
fi
MOUNT=`mount | grep "$SERVER:$SSHFS"`
if [ $? -eq 0 ]; then
fusermount -u $SSHFS_MOUNT
fi
ssh -S $SOCKET -O exit $SERVER
fi
}
# If ran without parameters, then just open a shell
if [ $# -ne 1 ]; then
openConnection
ssh -S $SOCKET -Y $SERVER
echo "Make sure to run '`basename $0` exit'"
exit 0
fi
# Otherwise, figure out what to do
case $1 in
# Mount Tron's SMB via SSH tunnel
"mount")
openConnection
ssh -S $SOCKET -O forward -L 10139:localhost:139 -L 10445:localhost:445 $SERVER
sudo mount -t cifs $SMB $SMB_MOUNT -o ip=127.0.0.1,port=10445,user=nticompass,ro,fsc
;;
# Unmount tron SMB
"umount")
sudo umount $SMB_MOUNT
checkConnection
if [ "$PID" != "" ]; then
ssh -S $SOCKET -O cancel -L 10139:localhost:139 -L 10445:localhost:445 $SERVER
fi
;;
# Mount tron via SSHFS (sharing the connection)
"sshfs")
openConnection
sshfs $SERVER:$SSHFS $SSHFS_MOUNT -o follow_symlinks -o ControlPath=$SOCKET
;;
# Unmount SSHFS
"fusermount")
checkConnection
if [ "$PID" != "" ]; then
fusermount -u $SSHFS_MOUNT
fi
;;
# Fowrard ports on tron
# 8000 => Apache
# 9091 => Transmission
"forward")
openConnection
ssh -S $SOCKET -O forward -L 8000:localhost:80 -L 9091:localhost:9091 $SERVER
;;
# Close the above ports
"unforward")
checkConnection
if [ "$PID" != "" ]; then
ssh -S $SOCKET -O cancel -L 8000:localhost:80 -L 9091:localhost:9091 $SERVER
fi
;;
# SSH to my desktop, hypercube
"hypercube")
openConnection
ssh -S $SOCKET -O forward -L 2221:$HYPERCUBE:22 $SERVER
ssh -p 2221 $USERNAME@localhost
ssh -S $SOCKET -O cancel -L 2221:$HYPERCUBE:22 $SERVER
;;
# Forward ports to the router's admin page
# 8888
"router")
openConnection
ssh -S $SOCKET -O forward -L 8888:$ROUTER:80 $SERVER
;;
# Close connection to the router
"norouter")
checkConnection
if [ "$PID" != "" ]; then
ssh -S $SOCKET -O cancel -L 8888:$ROUTER:80 $SERVER
fi
;;
# Just open the connection, *without* a shell
"open")
checkConnection
if [ "$PID" == "" ]; then
openConnection
echo "SSH connection to $SERVER opened $PID"
else
echo "SSH connection *already* open: $SERVER $PID"
fi
;;
# Kill the connection and unmount everything
"exit")
closeConnection
;;
# Display information about the connection and mounted shares
"status")
checkConnection
if [ "$PID" != "" ]; then
echo "SSH Tunnel: $SERVER $PID"
MOUNT=`mount | grep "$SMB"`
if [ $? -eq 0 ]
then
echo "SMB Mount: mounted ($SMB_MOUNT)"
else
echo "SMB Mount: not mounted"
fi
MOUNT=`mount | grep "$SERVER:$SSHFS"`
if [ $? -eq 0 ]
then
echo "SSHFS Mount: mounted ($SSHFS_MOUNT)"
else
echo "SSHFS Mount: not mounted"
fi
else
echo "SSH Tunnel: not running"
fi
;;
# Some basic help
*)
echo "Usage: `basename $0` [command]"
exit 1
;;
esac
@NTICompass
Copy link
Author

Replace ping google with ip -6 a | grep inet6 | grep -Ev "(fe80|::1)"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment