Commit 2 - SaigeDev || Fixed Local Confs.

Fixed Local Confs:
Better SSH now has the option to use Local Configurations Stored at "$HOME/.ssh_confs".
Usage: 
bssh -o FILENAME
bssh --offline FILENAME

Added Local Configuration Maker:
Better SSH can now Make Local Configurations through a simple Command.
Usage: 
bssh -m
bssh --make
This commit is contained in:
Saige Bowerman 2024-07-02 22:32:11 -05:00
parent 3b3ca8f5a9
commit 754528fb3a
1 changed files with 73 additions and 51 deletions

124
bssh.sh
View File

@ -1,40 +1,52 @@
#!/usr/bin/env bash #!/usr/bin/env bash
SERVER_URL="http://ssh.andresdev.online" SERVER_URL="http://ssh.andresdev.online"
CONFIG_DIR="$HOME/.ssh_confs" CONF_DIR="$HOME/.ssh_confs"
create_conf_directory() {
if [ ! -d "$CONF_DIR" ]; then
mkdir -p "$CONF_DIR"
echo "Configuration directory created at $CONF_DIR."
fi
}
show_help() { show_help() {
echo "Usage: bssh [options] <CONF_NAME>" echo "Usage: bssh [options] <CONF_NAME>"
echo "" echo ""
echo "Options:" echo "Options:"
echo " -h, --help Show this help message and exit" echo " -h, --help Show this help message and exit"
echo " -l, --list List all available configurations" echo " -l, --list List all available configurations (local and cloud)"
echo " -u, --user Login with username" echo " -u, --user Login with username"
echo " -p, --password Password for login" echo " -p, --password Password for login"
echo " -s, --save Save configuration to Cloud" echo " -s, --save Save configuration to Cloud"
echo " -o, --offline Use local configuration file" echo " -o, --offline Use local configuration"
echo " -m, --make Create a new local configuration"
echo "" echo ""
echo "Examples:" echo "Examples:"
echo " bssh -u kimeru -p password Login with user kimeru" echo " bssh -u kimeru -p password Login with user kimeru"
echo " bssh -l List all available configurations" echo " bssh -l List all available configurations"
echo " bssh -s web2 web2.yml Save configuration web2.yml to Cloud" echo " bssh -s web2 web2.yml Save configuration web2.yml to Cloud"
echo " bssh -o web2 Connect using local configuration web2.yml" echo " bssh -o CONF_NAME Use local configuration"
echo " bssh --make Create a new local configuration"
} }
list_cloud_configs() { list_configs() {
echo "Cloud Configurations Available:" echo "Cloud Configurations Available:"
curl -s "${SERVER_URL}/configurations" curl -s "${SERVER_URL}/configurations"
} echo ""
list_local_configs() {
echo "Local Configurations Available:" echo "Local Configurations Available:"
ls "$CONFIG_DIR"/*.yml 2>/dev/null | xargs -n 1 basename --suffix=".yml" if [ "$(ls -A "$CONF_DIR"/*.yml 2>/dev/null)" ]; then
for conf in "$CONF_DIR"/*.yml; do
basename "$conf" .yml
done
else
echo "None"
fi
} }
login_user() { login_user() {
local username="$1" local username="$1"
local password="$2" local password="$2"
response=$(curl -s -X POST -d "username=${username}&password=${password}" "${SERVER_URL}/login") response=$(curl -s -X POST -d "username=${username}&password=${password}" "${SERVER_URL}/login")
echo "$response" echo "$response"
} }
@ -42,56 +54,63 @@ login_user() {
save_configuration() { save_configuration() {
local name="$1" local name="$1"
local filename="$2" local filename="$2"
content=$(cat "$CONF_DIR/$filename")
content=$(cat "$CONFIG_DIR/$filename")
response=$(curl -s -X POST -d "name=${name}&content=${content}" "${SERVER_URL}/save-configuration") response=$(curl -s -X POST -d "name=${name}&content=${content}" "${SERVER_URL}/save-configuration")
echo "$response" echo "$response"
} }
connect_ssh() { create_local_configuration() {
local conf_name="$1" read -p "Enter host: " host
local conf_file="$CONFIG_DIR/${conf_name}.yml" read -p "Enter port: " port
read -p "Enter username: " username
if [ ! -f "$conf_file" ]; then read -p "Enter authentication method (password/key): " auth
echo "Configuration file $conf_name.yml not found locally. Attempting to retrieve from cloud..."
cloud_conf=$(curl -s "${SERVER_URL}/configuration/${conf_name}")
if [ -z "$cloud_conf" ]; then
echo "Configuration $conf_name not found in cloud."
exit 1
fi
echo "$cloud_conf" > "$conf_file"
fi
host=$(grep 'host:' "$conf_file" | sed 's/^[ \t]*host:[ \t]*//' | awk '{$1=$1};1')
port=$(grep 'port:' "$conf_file" | sed 's/^[ \t]*port:[ \t]*//' | awk '{$1=$1};1')
username=$(grep 'username:' "$conf_file" | sed 's/^[ \t]*username:[ \t]*//' | awk '{$1=$1};1')
auth=$(grep 'auth:' "$conf_file" | sed 's/^[ \t]*auth:[ \t]*//' | awk '{$1=$1};1')
if [ "$auth" == "password" ]; then if [ "$auth" == "password" ]; then
password=$(grep 'password:' "$conf_file" | sed 's/^[ \t]*password:[ \t]*//' | awk '{$1=$1};1') read -sp "Enter password: " password
echo "Connecting to $host as $username using password authentication..." echo ""
sshpass -p "$password" ssh "$username@$host" -p "$port" echo -e "host: $host\nport: $port\nusername: $username\nauth: $auth\npassword: $password" > "$CONF_DIR/$username.yml"
elif [ "$auth" == "key" ]; then elif [ "$auth" == "key" ]; then
key=$(sed -n '/key:/,/^ *$/p' "$conf_file" | sed '1d;$d' | awk '{$1=$1};1') read -p "Enter path to private key: " key_path
echo "Connecting to $host as $username using key authentication..." echo -e "host: $host\nport: $port\nusername: $username\nauth: $auth\nkey: |" > "$CONF_DIR/$username.yml"
sed 's/^/ /' "$key_path" >> "$CONF_DIR/$username.yml"
key_file=$(mktemp) else
echo "$key" > "$key_file" echo "Unknown authentication method: $auth"
chmod 600 "$key_file" exit 1
ssh -i "$key_file" "$username@$host" -p "$port" fi
rm "$key_file"
echo "Configuration saved to $CONF_DIR/$username.yml"
}
use_local_conf() {
local conf_name="$1"
local conf_file="$CONF_DIR/$conf_name.yml"
if [ ! -f "$conf_file" ]; then
echo "Configuration $conf_name not found."
exit 1
fi
host=$(grep '^host: ' "$conf_file" | awk '{print $2}')
port=$(grep '^port: ' "$conf_file" | awk '{print $2}')
username=$(grep '^username: ' "$conf_file" | awk '{print $2}')
auth=$(grep '^auth: ' "$conf_file" | awk '{print $2}')
if [ "$auth" == "password" ]; then
password=$(grep '^password: ' "$conf_file" | awk '{print $2}')
sshpass -p "$password" ssh -o StrictHostKeyChecking=no "$username@$host" -p "$port"
elif [ "$auth" == "key" ]; then
key=$(awk '/^key: \|/{flag=1; next} /-----END/{flag=0} flag' "$conf_file")
echo "$key" > "$CONF_DIR/temp_key"
chmod 600 "$CONF_DIR/temp_key"
ssh -i "$CONF_DIR/temp_key" -o StrictHostKeyChecking=no "$username@$host" -p "$port"
rm "$CONF_DIR/temp_key"
else else
echo "Unknown authentication method: $auth" echo "Unknown authentication method: $auth"
exit 1 exit 1
fi fi
} }
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then create_conf_directory
mkdir -p "$CONFIG_DIR"
echo "Created directory $CONFIG_DIR for storing local configurations."
fi
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
show_help show_help
@ -104,8 +123,7 @@ case $1 in
exit 0 exit 0
;; ;;
-l|--list) -l|--list)
list_cloud_configs list_configs
list_local_configs
exit 0 exit 0
;; ;;
-u|--user) -u|--user)
@ -133,7 +151,11 @@ case $1 in
echo "Error: Configuration name required." echo "Error: Configuration name required."
exit 1 exit 1
fi fi
connect_ssh "$2" use_local_conf "$2"
exit 0
;;
-m|--make)
create_local_configuration
exit 0 exit 0
;; ;;
*) *)
@ -143,4 +165,4 @@ esac
echo "Unknown option: $1" echo "Unknown option: $1"
show_help show_help
exit 1 exit 1