2024-07-02 14:15:02 -05:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2024-07-02 18:51:10 -05:00
|
|
|
SERVER_URL="http://ssh.andresdev.online"
|
|
|
|
CONFIG_DIR="$HOME/.ssh_confs"
|
2024-07-02 14:15:02 -05:00
|
|
|
|
|
|
|
show_help() {
|
|
|
|
echo "Usage: bssh [options] <CONF_NAME>"
|
|
|
|
echo ""
|
|
|
|
echo "Options:"
|
|
|
|
echo " -h, --help Show this help message and exit"
|
|
|
|
echo " -l, --list List all available configurations"
|
|
|
|
echo " -u, --user Login with username"
|
|
|
|
echo " -p, --password Password for login"
|
|
|
|
echo " -s, --save Save configuration to Cloud"
|
2024-07-02 18:51:10 -05:00
|
|
|
echo " -o, --offline Use local configuration file"
|
2024-07-02 14:15:02 -05:00
|
|
|
echo ""
|
|
|
|
echo "Examples:"
|
|
|
|
echo " bssh -u kimeru -p password Login with user kimeru"
|
|
|
|
echo " bssh -l List all available configurations"
|
|
|
|
echo " bssh -s web2 web2.yml Save configuration web2.yml to Cloud"
|
2024-07-02 18:51:10 -05:00
|
|
|
echo " bssh -o web2 Connect using local configuration web2.yml"
|
2024-07-02 14:15:02 -05:00
|
|
|
}
|
|
|
|
|
2024-07-02 18:51:10 -05:00
|
|
|
list_cloud_configs() {
|
2024-07-02 14:15:02 -05:00
|
|
|
echo "Cloud Configurations Available:"
|
|
|
|
curl -s "${SERVER_URL}/configurations"
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:51:10 -05:00
|
|
|
list_local_configs() {
|
|
|
|
echo "Local Configurations Available:"
|
|
|
|
ls "$CONFIG_DIR"/*.yml 2>/dev/null | xargs -n 1 basename --suffix=".yml"
|
|
|
|
}
|
|
|
|
|
2024-07-02 14:15:02 -05:00
|
|
|
login_user() {
|
|
|
|
local username="$1"
|
|
|
|
local password="$2"
|
|
|
|
|
|
|
|
response=$(curl -s -X POST -d "username=${username}&password=${password}" "${SERVER_URL}/login")
|
|
|
|
echo "$response"
|
|
|
|
}
|
|
|
|
|
|
|
|
save_configuration() {
|
|
|
|
local name="$1"
|
|
|
|
local filename="$2"
|
|
|
|
|
2024-07-02 18:51:10 -05:00
|
|
|
content=$(cat "$CONFIG_DIR/$filename")
|
2024-07-02 14:15:02 -05:00
|
|
|
response=$(curl -s -X POST -d "name=${name}&content=${content}" "${SERVER_URL}/save-configuration")
|
|
|
|
echo "$response"
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:51:10 -05:00
|
|
|
connect_ssh() {
|
|
|
|
local conf_name="$1"
|
|
|
|
local conf_file="$CONFIG_DIR/${conf_name}.yml"
|
|
|
|
|
|
|
|
if [ ! -f "$conf_file" ]; then
|
|
|
|
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
|
|
|
|
password=$(grep 'password:' "$conf_file" | sed 's/^[ \t]*password:[ \t]*//' | awk '{$1=$1};1')
|
|
|
|
echo "Connecting to $host as $username using password authentication..."
|
|
|
|
sshpass -p "$password" ssh "$username@$host" -p "$port"
|
|
|
|
elif [ "$auth" == "key" ]; then
|
|
|
|
key=$(sed -n '/key:/,/^ *$/p' "$conf_file" | sed '1d;$d' | awk '{$1=$1};1')
|
|
|
|
echo "Connecting to $host as $username using key authentication..."
|
|
|
|
|
|
|
|
key_file=$(mktemp)
|
|
|
|
echo "$key" > "$key_file"
|
|
|
|
chmod 600 "$key_file"
|
|
|
|
ssh -i "$key_file" "$username@$host" -p "$port"
|
|
|
|
rm "$key_file"
|
|
|
|
else
|
|
|
|
echo "Unknown authentication method: $auth"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
|
|
|
|
mkdir -p "$CONFIG_DIR"
|
|
|
|
echo "Created directory $CONFIG_DIR for storing local configurations."
|
|
|
|
fi
|
|
|
|
|
2024-07-02 14:15:02 -05:00
|
|
|
if [ $# -eq 0 ]; then
|
|
|
|
show_help
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
case $1 in
|
|
|
|
-h|--help)
|
|
|
|
show_help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
-l|--list)
|
2024-07-02 18:51:10 -05:00
|
|
|
list_cloud_configs
|
|
|
|
list_local_configs
|
2024-07-02 14:15:02 -05:00
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
-u|--user)
|
|
|
|
if [ $# -ne 3 ]; then
|
|
|
|
echo "Error: Username and password required."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
login_user "$2" "$3"
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
-p|--password)
|
|
|
|
echo "Error: Password option must be used with -u/--user."
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
-s|--save)
|
|
|
|
if [ $# -ne 3 ]; then
|
|
|
|
echo "Error: Configuration name and filename required."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
save_configuration "$2" "$3"
|
|
|
|
exit 0
|
|
|
|
;;
|
2024-07-02 18:51:10 -05:00
|
|
|
-o|--offline)
|
|
|
|
if [ $# -ne 2 ]; then
|
|
|
|
echo "Error: Configuration name required."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
connect_ssh "$2"
|
|
|
|
exit 0
|
|
|
|
;;
|
2024-07-02 14:15:02 -05:00
|
|
|
*)
|
|
|
|
CONF_NAME=$1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
echo "Unknown option: $1"
|
|
|
|
show_help
|
|
|
|
exit 1
|