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"
|
2024-07-02 22:32:11 -05:00
|
|
|
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
|
|
|
|
}
|
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"
|
2024-07-02 22:32:11 -05:00
|
|
|
echo " -l, --list List all available configurations (local and cloud)"
|
2024-07-02 14:15:02 -05:00
|
|
|
echo " -u, --user Login with username"
|
|
|
|
echo " -p, --password Password for login"
|
|
|
|
echo " -s, --save Save configuration to Cloud"
|
2024-07-02 22:32:11 -05:00
|
|
|
echo " -o, --offline Use local configuration"
|
|
|
|
echo " -m, --make Create a new local configuration"
|
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 22:32:11 -05:00
|
|
|
echo " bssh -o CONF_NAME Use local configuration"
|
|
|
|
echo " bssh --make Create a new local configuration"
|
2024-07-02 14:15:02 -05:00
|
|
|
}
|
|
|
|
|
2024-07-02 22:32:11 -05:00
|
|
|
list_configs() {
|
2024-07-02 14:15:02 -05:00
|
|
|
echo "Cloud Configurations Available:"
|
|
|
|
curl -s "${SERVER_URL}/configurations"
|
2024-07-02 22:32:11 -05:00
|
|
|
echo ""
|
2024-07-02 18:51:10 -05:00
|
|
|
echo "Local Configurations Available:"
|
2024-07-02 22:32:11 -05:00
|
|
|
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
|
2024-07-02 18:51:10 -05:00
|
|
|
}
|
|
|
|
|
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 22:32:11 -05:00
|
|
|
content=$(cat "$CONF_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 22:32:11 -05:00
|
|
|
create_local_configuration() {
|
|
|
|
read -p "Enter host: " host
|
|
|
|
read -p "Enter port: " port
|
|
|
|
read -p "Enter username: " username
|
|
|
|
read -p "Enter authentication method (password/key): " auth
|
2024-07-02 18:51:10 -05:00
|
|
|
|
2024-07-02 22:32:11 -05:00
|
|
|
if [ "$auth" == "password" ]; then
|
|
|
|
read -sp "Enter password: " password
|
|
|
|
echo ""
|
|
|
|
echo -e "host: $host\nport: $port\nusername: $username\nauth: $auth\npassword: $password" > "$CONF_DIR/$username.yml"
|
|
|
|
elif [ "$auth" == "key" ]; then
|
|
|
|
read -p "Enter path to private key: " key_path
|
|
|
|
echo -e "host: $host\nport: $port\nusername: $username\nauth: $auth\nkey: |" > "$CONF_DIR/$username.yml"
|
|
|
|
sed 's/^/ /' "$key_path" >> "$CONF_DIR/$username.yml"
|
|
|
|
else
|
|
|
|
echo "Unknown authentication method: $auth"
|
|
|
|
exit 1
|
|
|
|
fi
|
2024-07-02 18:51:10 -05:00
|
|
|
|
2024-07-02 22:32:11 -05:00
|
|
|
echo "Configuration saved to $CONF_DIR/$username.yml"
|
|
|
|
}
|
2024-07-02 18:51:10 -05:00
|
|
|
|
2024-07-02 22:32:11 -05:00
|
|
|
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
|
2024-07-02 18:51:10 -05:00
|
|
|
fi
|
|
|
|
|
2024-07-02 22:32:11 -05:00
|
|
|
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}')
|
2024-07-02 18:51:10 -05:00
|
|
|
|
|
|
|
if [ "$auth" == "password" ]; then
|
2024-07-02 22:32:11 -05:00
|
|
|
password=$(grep '^password: ' "$conf_file" | awk '{print $2}')
|
|
|
|
sshpass -p "$password" ssh -o StrictHostKeyChecking=no "$username@$host" -p "$port"
|
2024-07-02 18:51:10 -05:00
|
|
|
elif [ "$auth" == "key" ]; then
|
2024-07-02 22:32:11 -05:00
|
|
|
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"
|
2024-07-02 18:51:10 -05:00
|
|
|
else
|
|
|
|
echo "Unknown authentication method: $auth"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-07-02 22:32:11 -05:00
|
|
|
create_conf_directory
|
2024-07-02 18:51:10 -05:00
|
|
|
|
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 22:32:11 -05:00
|
|
|
list_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
|
2024-07-02 22:32:11 -05:00
|
|
|
use_local_conf "$2"
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
-m|--make)
|
|
|
|
create_local_configuration
|
2024-07-02 18:51:10 -05:00
|
|
|
exit 0
|
|
|
|
;;
|
2024-07-02 14:15:02 -05:00
|
|
|
*)
|
|
|
|
CONF_NAME=$1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
echo "Unknown option: $1"
|
|
|
|
show_help
|
2024-07-02 22:32:11 -05:00
|
|
|
exit 1
|