Skip to content

Instantly share code, notes, and snippets.

@NTICompass
Last active September 17, 2016 18:39
Show Gist options
  • Select an option

  • Save NTICompass/ca024e2c336eaa33beba to your computer and use it in GitHub Desktop.

Select an option

Save NTICompass/ca024e2c336eaa33beba to your computer and use it in GitHub Desktop.

Revisions

  1. NTICompass revised this gist Aug 12, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tron.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash

    # Should we connect vis IPv4 or IPv6?
    # Should we connect via IPv4 or IPv6?
    if ping -c 1 ipv6.google.com &> /dev/null
    then
    SERVER="tron6"
  2. NTICompass revised this gist Aug 12, 2016. 1 changed file with 55 additions and 26 deletions.
    81 changes: 55 additions & 26 deletions tron.sh
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,33 @@
    #!/bin/bash

    SERVER="tron"
    # Should we connect vis 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="/media/tron/smb"
    SMB_MOUNT="/mnt/tron/smb"
    SSHFS="/home/nticompass"
    SSHFS_MOUNT="/media/tron/ssh"

    SOCKET="/tmp/$SERVER.ssh"
    PID=""
    SSHFS_MOUNT="/mnt/tron/ssh"

    # Access to internal machines
    USERNAME="nticompass"
    HYPERCUBE="hypercube"
    CELESTIA="celestia"
    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
    @@ -29,13 +39,18 @@ checkConnection(){
    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
    @@ -45,7 +60,6 @@ closeConnection(){
    sudo umount $SMB_MOUNT
    fi


    MOUNT=`mount | grep "$SERVER:$SSHFS"`
    if [ $? -eq 0 ]; then
    fusermount -u $SSHFS_MOUNT
    @@ -56,8 +70,7 @@ closeConnection(){
    }

    # If ran without parameters, then just open a shell
    if [ $# -ne 1 ]
    then
    if [ $# -ne 1 ]; then
    openConnection
    ssh -S $SOCKET -Y $SERVER
    echo "Make sure to run '`basename $0` exit'"
    @@ -66,70 +79,85 @@ 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
    ;;
    "mount.local")
    sudo mount -t cifs $SMB $SMB_MOUNT -o user=nticompass
    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 2222:$HYPERCUBE:22 $SERVER
    ssh -p 2222 $USERNAME@localhost
    ssh -S $SOCKET -O cancel -L 2222:$HYPERCUBE:22 $SERVER
    ;;
    "celestia")
    openConnection
    ssh -S $SOCKET -O forward -L 2220:$CELESTIA:22 $SERVER
    ssh -p 2220 $USERNAME@localhost
    ssh -S $SOCKET -O cancel -L 2220:$CELESTIA:22 $SERVER
    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: running $PID"
    echo "SSH Tunnel: $SERVER $PID"

    MOUNT=`mount | grep "$SMB"`
    if [ $? -eq 0 ]
    @@ -138,7 +166,7 @@ case $1 in
    else
    echo "SMB Mount: not mounted"
    fi

    MOUNT=`mount | grep "$SERVER:$SSHFS"`
    if [ $? -eq 0 ]
    then
    @@ -150,8 +178,9 @@ case $1 in
    echo "SSH Tunnel: not running"
    fi
    ;;
    # Some basic help
    *)
    echo "Usage: `basename $0` (forward | exit | status)"
    echo "Usage: `basename $0` [command]"
    exit 1
    ;;
    esac
  3. NTICompass created this gist Feb 16, 2016.
    157 changes: 157 additions & 0 deletions tron.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,157 @@
    #!/bin/bash

    SERVER="tron"

    SMB="//tron/Tron"
    SMB_MOUNT="/media/tron/smb"
    SSHFS="/home/nticompass"
    SSHFS_MOUNT="/media/tron/ssh"

    SOCKET="/tmp/$SERVER.ssh"
    PID=""

    USERNAME="nticompass"
    HYPERCUBE="hypercube"
    CELESTIA="celestia"
    ROUTER="enigma"

    # 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

    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
    }

    openConnection(){
    checkConnection
    if [ "$PID" == "" ]; then
    ssh -S $SOCKET -MfnNTY $SERVER
    fi
    }

    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")
    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
    ;;
    "mount.local")
    sudo mount -t cifs $SMB $SMB_MOUNT -o user=nticompass
    ;;
    "umount")
    sudo umount $SMB_MOUNT
    checkConnection
    if [ "$PID" != "" ]; then
    ssh -S $SOCKET -O cancel -L 10139:localhost:139 -L 10445:localhost:445 $SERVER
    fi
    ;;
    "sshfs")
    openConnection
    sshfs $SERVER:$SSHFS $SSHFS_MOUNT -o follow_symlinks -o ControlPath=$SOCKET
    ;;
    "fusermount")
    checkConnection
    if [ "$PID" != "" ]; then
    fusermount -u $SSHFS_MOUNT
    fi
    ;;
    "forward")
    openConnection
    ssh -S $SOCKET -O forward -L 8000:localhost:80 -L 9091:localhost:9091 $SERVER
    ;;
    "unforward")
    checkConnection
    if [ "$PID" != "" ]; then
    ssh -S $SOCKET -O cancel -L 8000:localhost:80 -L 9091:localhost:9091 $SERVER
    fi
    ;;
    "hypercube")
    openConnection
    ssh -S $SOCKET -O forward -L 2222:$HYPERCUBE:22 $SERVER
    ssh -p 2222 $USERNAME@localhost
    ssh -S $SOCKET -O cancel -L 2222:$HYPERCUBE:22 $SERVER
    ;;
    "celestia")
    openConnection
    ssh -S $SOCKET -O forward -L 2220:$CELESTIA:22 $SERVER
    ssh -p 2220 $USERNAME@localhost
    ssh -S $SOCKET -O cancel -L 2220:$CELESTIA:22 $SERVER
    ;;
    "router")
    openConnection
    ssh -S $SOCKET -O forward -L 8888:$ROUTER:80 $SERVER
    ;;
    "norouter")
    checkConnection
    if [ "$PID" != "" ]; then
    ssh -S $SOCKET -O cancel -L 8888:$ROUTER:80 $SERVER
    fi
    ;;
    "exit")
    closeConnection
    ;;
    "status")
    checkConnection
    if [ "$PID" != "" ]; then
    echo "SSH Tunnel: running $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
    ;;
    *)
    echo "Usage: `basename $0` (forward | exit | status)"
    exit 1
    ;;
    esac