void/scripts/dbc

75 lines
2.5 KiB
Plaintext
Raw Normal View History

2024-02-29 03:45:58 +00:00
#!/bin/sh
# Dropbear Connect Script (DBC) - Developed by acidvegas (https://git.acid.vegas/void)
# Dropbear config must be stored in pass in the following format:
# NAME USER HOST PORT JUMP
#
# JUMP is optional and can be used to specify a host that should use your jump host.
# If JUMP is set to x, the script will use the jump host to connect to the end host.
# There should only be one jump host in the config file and it should be named 'jump'.
#
# Example:
# jump acidvegas 68.192.37.5 5902
# hatebox acidvegas 100.151.45.10 2023 x
# aws admin 45.16.150.203 22
#
# Useful commands:
# Git usage : git config core.sshCommand "dbclient -i ~/.ssh/key"
2024-07-10 00:50:57 +00:00
# Generate private key : dropbearkey -t ed25519 -f ~/.dropbear/key | grep "ssh-ed25519"
2024-02-29 03:45:58 +00:00
# Get public key : dropbearkey -y -f ~/.dropbear/key | head -n 2 | tail -n 1
load_host() {
CONFIG_DATA="$1"
NAME="$2"
# Use grep to find the matching line
MATCHING_LINES=$(printf "%s\n" "$CONFIG_DATA" | grep "^$NAME ")
# Check if exactly one matching line is found
LINE_COUNT=$(printf "%s\n" "$MATCHING_LINES" | wc -l)
if [ "$LINE_COUNT" -ne 1 ]; then
echo "Error: The NAME '$NAME' matches multiple or no lines." && return 1
fi
# Remove extra whitespace from the matching line
MATCHING_LINES=$(printf "%s\n" "$MATCHING_LINES" | tr -s '[:space:]' ' ')
# Read parameters from the matching line
line_name=$(echo $MATCHING_LINES | cut -d ' ' -f 1)
line_user=$(echo $MATCHING_LINES | cut -d ' ' -f 2)
line_host=$(echo $MATCHING_LINES | cut -d ' ' -f 3)
line_port=$(echo $MATCHING_LINES | cut -d ' ' -f 4)
line_jump=$(echo $MATCHING_LINES | cut -d ' ' -f 5)
# Output the result
printf "%s@%s^%s%s" "$line_user" "$line_host" "$line_port" "$line_jump"
}
# Check if the name argument is provided
if [ $# -ne 1 ]; then
echo "usage: $0 [name]" && exit 1
fi
# Read the name argument
NAME=$1
# Read the config data
CONFIG_DATA=$($HOME/.scripts/pass dropbear)
# Check if the config data is read successfully
if [ $? -ne 0 ]; then
echo "error: can not read config data" && exit 1
fi
# Load the host data
JUMP_HOST=$(load_host "$CONFIG_DATA" "jump")
END_HOST=$(load_host "$CONFIG_DATA" "$NAME")
JUMP_CHECK=$(printf "$END_HOST" | rev | cut -c1)
# Connect to the host
if [ $JUMP_CHECK = "x" ]; then
END_HOST=$(printf $END_HOST | rev | cut -c2- | rev)
dbclient -K 60 -i $HOME/.dropbear/key $JUMP_HOST,$END_HOST
else
dbclient -K 60 -i $HOME/.dropbear/key $END_HOST
fi