void

- enter the void 🪐
git clone git://git.acid.vegas/archlinux.git
Log | Files | Refs | Archive

dbc (2545B)

      1 #!/bin/sh
      2 # Dropbear Connect Script (DBC) - Developed by acidvegas (https://git.acid.vegas/void)
      3 
      4 # Dropbear config must be stored in pass in the following format:
      5 #   NAME USER HOST PORT JUMP
      6 #
      7 # JUMP is optional and can be used to specify a host that should use your jump host.
      8 # If JUMP is set to x, the script will use the jump host to connect to the end host.
      9 # There should only be one jump host in the config file and it should be named 'jump'.
     10 #
     11 # Example:
     12 #   jump    acidvegas 68.192.37.5   5902
     13 #   hatebox acidvegas 100.151.45.10 2023 x
     14 #   aws     admin     45.16.150.203 22
     15 #
     16 # Useful commands:
     17 #   Git usage            : git config core.sshCommand "dbclient -i ~/.ssh/key"
     18 #   Generate private key : dropbearkey -t ed25519 -f ~/.dropbear/key | grep "ssh-ed25519"
     19 #   Get public key       : dropbearkey -y -f ~/.dropbear/key | head -n 2 | tail -n 1
     20 
     21 load_host() {
     22     CONFIG_DATA="$1"
     23     NAME="$2"
     24 
     25     # Use grep to find the matching line
     26     MATCHING_LINES=$(printf "%s\n" "$CONFIG_DATA" | grep "^$NAME ")
     27 
     28     # Check if exactly one matching line is found
     29     LINE_COUNT=$(printf "%s\n" "$MATCHING_LINES" | wc -l)
     30     if [ "$LINE_COUNT" -ne 1 ]; then
     31         echo "Error: The NAME '$NAME' matches multiple or no lines." && return 1
     32     fi
     33 
     34     # Remove extra whitespace from the matching line
     35     MATCHING_LINES=$(printf "%s\n" "$MATCHING_LINES" | tr -s '[:space:]' ' ')
     36 
     37     # Read parameters from the matching line
     38     line_name=$(echo $MATCHING_LINES | cut -d ' ' -f 1)
     39     line_user=$(echo $MATCHING_LINES | cut -d ' ' -f 2)
     40     line_host=$(echo $MATCHING_LINES | cut -d ' ' -f 3)
     41     line_port=$(echo $MATCHING_LINES | cut -d ' ' -f 4)
     42     line_jump=$(echo $MATCHING_LINES | cut -d ' ' -f 5)
     43 
     44     # Output the result
     45     printf "%s@%s^%s%s" "$line_user" "$line_host" "$line_port" "$line_jump"
     46 }
     47 
     48 # Check if the name argument is provided
     49 if [ $# -ne 1 ]; then
     50     echo "usage: $0 [name]" && exit 1
     51 fi
     52 
     53 # Read the name argument
     54 NAME=$1
     55 
     56 # Read the config data
     57 CONFIG_DATA=$($HOME/.scripts/pass dropbear)
     58 
     59 # Check if the config data is read successfully
     60 if [ $? -ne 0 ]; then
     61     echo "error: can not read config data" && exit 1
     62 fi
     63 
     64 # Load the host data
     65 JUMP_HOST=$(load_host "$CONFIG_DATA" "jump")
     66 END_HOST=$(load_host "$CONFIG_DATA" "$NAME")
     67 JUMP_CHECK=$(printf "$END_HOST" | rev | cut -c1)
     68 
     69 # Connect to the host
     70 if [ $JUMP_CHECK = "x" ]; then
     71     END_HOST=$(printf $END_HOST | rev | cut -c2- | rev)
     72     dbclient -K 60 -i $HOME/.dropbear/key $JUMP_HOST,$END_HOST
     73 else
     74     dbclient -K 60 -i $HOME/.dropbear/key $END_HOST
     75 fi