2024-09-19 18:16:03 +01:00
|
|
|
#!/bin/sh -e
|
|
|
|
|
|
|
|
. ../common-script.sh
|
|
|
|
|
|
|
|
# Check if ~/.ssh/config exists, if not, create it
|
|
|
|
if [ ! -f ~/.ssh/config ]; then
|
2024-10-31 18:37:41 +00:00
|
|
|
mkdir -p "$HOME/.ssh"
|
|
|
|
touch "$HOME/.ssh/config"
|
|
|
|
chmod 600 "$HOME/.ssh/config"
|
2024-09-19 18:16:03 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Function to show available hosts from ~/.ssh/config
|
|
|
|
show_available_hosts() {
|
|
|
|
printf "%b\n" "Available Systems:"
|
2024-10-31 18:37:41 +00:00
|
|
|
grep -E "^Host " "$HOME/.ssh/config" | awk '{print $2}'
|
2024-09-19 18:16:03 +01:00
|
|
|
printf "%b\n" "-------------------"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function to ask for host details
|
|
|
|
ask_for_host_details() {
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter Host Alias: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r host_alias
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter Remote Host (hostname or IP): "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r host
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter Remote User: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r user
|
2024-09-20 02:13:04 +01:00
|
|
|
{
|
|
|
|
printf "%b\n" "Host $host_alias"
|
|
|
|
printf "%b\n" " HostName $host"
|
|
|
|
printf "%b\n" " User $user"
|
|
|
|
printf "%b\n" " IdentityFile ~/.ssh/id_rsa"
|
|
|
|
printf "%b\n" " StrictHostKeyChecking no"
|
|
|
|
printf "%b\n" " UserKnownHostsFile=/dev/null"
|
|
|
|
} >> ~/.ssh/config
|
2024-09-19 18:16:03 +01:00
|
|
|
printf "%b\n" "Host $host_alias added successfully."
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function to generate SSH key if not exists
|
|
|
|
generate_ssh_key() {
|
|
|
|
if [ ! -f ~/.ssh/id_rsa ]; then
|
|
|
|
printf "%b\n" "SSH key not found, generating one..."
|
|
|
|
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -N "" -C "$(whoami)@$(hostname)"
|
|
|
|
else
|
|
|
|
printf "%b\n" "SSH key already exists."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function to share the SSH public key with the remote host
|
|
|
|
share_ssh_key() {
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the alias of the host to copy the key to: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r host_alias
|
|
|
|
printf "%b\n" "Copying SSH key to $host_alias..."
|
|
|
|
ssh-copy-id "$host_alias"
|
|
|
|
printf "%b\n" "SSH key copied to $host_alias successfully."
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function to disable password authentication and allow only SSH keys
|
|
|
|
#repeated twice as changes should take place when in commented state or modified state.
|
|
|
|
disable_password_auth() {
|
|
|
|
printf "%b\n" "Disabling SSH password authentication and enabling key-only login..."
|
|
|
|
printf "%b\n" "Enter the alias of the host: "
|
|
|
|
read -r host_alias
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "\n"
|
|
|
|
ssh "$host_alias" "
|
2024-09-19 19:05:36 +01:00
|
|
|
"$ESCALATION_TOOL" -S sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config &&
|
|
|
|
"$ESCALATION_TOOL" -S sed -i 's/^PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config &&
|
|
|
|
"$ESCALATION_TOOL" -S sed -i 's/^#PubkeyAuthentication no/PubkeyAuthentication yes/' /etc/ssh/sshd_config &&
|
|
|
|
"$ESCALATION_TOOL" -S sed -i 's/^PubkeyAuthentication no/PubkeyAuthentication yes/' /etc/ssh/sshd_config &&
|
|
|
|
"$ESCALATION_TOOL" -S systemctl restart sshd
|
2024-09-19 18:16:03 +01:00
|
|
|
"
|
|
|
|
printf "%b\n" "PasswordAuthentication set to no and PubkeyAuthentication set to yes."
|
|
|
|
}
|
|
|
|
|
|
|
|
enable_password_auth() {
|
|
|
|
printf "%b\n" "Disabling SSH password authentication and enabling key-only login..."
|
|
|
|
printf "%b\n" "Enter the alias of the host: "
|
|
|
|
read -r host_alias
|
|
|
|
printf "\n"
|
2024-09-22 17:01:10 +01:00
|
|
|
ssh "$host_alias" "
|
2024-09-19 19:05:36 +01:00
|
|
|
"$ESCALATION_TOOL" -S sed -i 's/^#PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config &&
|
|
|
|
"$ESCALATION_TOOL" -S sed -i 's/^PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config &&
|
|
|
|
"$ESCALATION_TOOL" -S sed -i 's/^#PubkeyAuthentication yes/PubkeyAuthentication no/' /etc/ssh/sshd_config &&
|
|
|
|
"$ESCALATION_TOOL" -S sed -i 's/^PubkeyAuthentication yes/PubkeyAuthentication no/' /etc/ssh/sshd_config &&
|
|
|
|
"$ESCALATION_TOOL" -S systemctl restart sshd
|
2024-09-19 18:16:03 +01:00
|
|
|
"
|
|
|
|
printf "%b\n" "PasswordAuthentication set to yes and PubkeyAuthentication set to no."
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function to check if password authentication is disabled
|
|
|
|
check_password_auth() {
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the alias of the host: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r host_alias
|
2024-09-22 17:01:10 +01:00
|
|
|
ssh "$host_alias" "grep '^PasswordAuthentication' /etc/ssh/sshd_config"
|
2024-09-19 18:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Function to run a command on a remote server
|
|
|
|
run_remote_command() {
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the alias of the host: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r host_alias
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the command to run: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r remote_command
|
2024-09-22 17:01:10 +01:00
|
|
|
ssh "$host_alias" "$remote_command"
|
2024-09-19 18:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Function to copy a file to a remote server
|
|
|
|
copy_file_to_remote() {
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the local file path: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r local_file
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the alias of the host: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r host_alias
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the remote destination path: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r remote_path
|
2024-09-22 17:01:10 +01:00
|
|
|
scp "$local_file" "$host_alias:$remote_path"
|
2024-09-19 18:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Function to copy a directory to a remote server
|
|
|
|
copy_directory_to_remote() {
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the local directory path: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r local_dir
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the alias of the host: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r host_alias
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the remote destination path: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r remote_path
|
2024-09-22 17:01:10 +01:00
|
|
|
scp -r "$local_dir" "$host_alias:$remote_path"
|
2024-09-19 18:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Function to move a file to a remote server (copy and delete local)
|
|
|
|
move_file_to_remote() {
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the local file path: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r local_file
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the alias of the host: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r host_alias
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the remote destination path: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r remote_path
|
2024-09-22 17:01:10 +01:00
|
|
|
scp "$local_file" "$host_alias:$remote_path" && rm "$local_file"
|
2024-09-19 18:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Function to move a directory to a remote server (copy and delete local)
|
|
|
|
move_directory_to_remote() {
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the local directory path: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r local_dir
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the alias of the host: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r host_alias
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the remote destination path: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r remote_path
|
2024-09-22 17:01:10 +01:00
|
|
|
scp -r "$local_dir" "$host_alias:$remote_path" && rm -r "$local_dir"
|
2024-09-19 18:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Function to remove a system from SSH configuration
|
|
|
|
remove_system() {
|
|
|
|
printf "%b\n" "Enter the alias of the host to remove: "
|
|
|
|
read -r host_alias
|
|
|
|
sed -i "/^Host $host_alias/,+3d" ~/.ssh/config
|
|
|
|
printf "%b\n" "Removed $host_alias from SSH configuration."
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function to view SSH configuration
|
|
|
|
view_ssh_config() {
|
|
|
|
printf "%b\n" "Enter the alias of the host to view (or press Enter to view all): "
|
|
|
|
read -r host_alias
|
|
|
|
if [ -z "$host_alias" ]; then
|
|
|
|
cat ~/.ssh/config
|
|
|
|
else
|
|
|
|
grep -A 3 "^Host $host_alias" ~/.ssh/config
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function to backup files from remote host
|
|
|
|
backup_files() {
|
|
|
|
printf "%b\n" "Enter the alias of the host: "
|
|
|
|
read -r host_alias
|
|
|
|
printf "%b\n" "Enter the files or directories to backup on remote host: "
|
|
|
|
read -r remote_files
|
|
|
|
printf "%b\n" "Enter the local backup directory path: "
|
|
|
|
read -r local_backup_dir
|
2024-09-22 17:01:10 +01:00
|
|
|
scp -r "$host_alias:$remote_files" "$local_backup_dir"
|
2024-09-19 18:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Function to sync directories with remote host
|
|
|
|
sync_directories() {
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the local directory path: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r local_dir
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the alias of the host: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r host_alias
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter the remote directory path: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r remote_dir
|
2024-09-22 17:01:10 +01:00
|
|
|
rsync -avz "$local_dir" "$host_alias:$remote_dir"
|
2024-09-19 18:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Function to check SSH key authentication status
|
|
|
|
check_ssh_key_authentication() {
|
2024-09-20 02:13:04 +01:00
|
|
|
printf "%b\n" "Enter the alias of the host: "
|
2024-09-19 18:16:03 +01:00
|
|
|
read -r host_alias
|
2024-09-22 17:01:10 +01:00
|
|
|
ssh "$host_alias" "grep '^PubkeyAuthentication' /etc/ssh/sshd_config"
|
2024-09-19 18:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Function to show options for the user
|
|
|
|
show_menu() {
|
|
|
|
printf "%b\n" "Select an SSH operation:"
|
|
|
|
printf "%b\n" "1. Add a new system"
|
|
|
|
printf "%b\n" "2. Connect to a system"
|
|
|
|
printf "%b\n" "3. Generate SSH key"
|
|
|
|
printf "%b\n" "4. Share SSH key with remote host"
|
|
|
|
printf "%b\n" "5. Disable password authentication on remote host"
|
|
|
|
printf "%b\n" "6. Enable password authentication on remote host"
|
|
|
|
printf "%b\n" "7. Check password authentication on remote host"
|
|
|
|
printf "%b\n" "8. Check SSH key authentication status"
|
|
|
|
printf "%b\n" "9. Run a command on remote host"
|
|
|
|
printf "%b\n" "10. Copy a file to remote host"
|
|
|
|
printf "%b\n" "11. Copy a directory to remote host"
|
|
|
|
printf "%b\n" "12. Move a file to remote host (copy and delete local)"
|
|
|
|
printf "%b\n" "13. Move a directory to remote host (copy and delete local)"
|
|
|
|
printf "%b\n" "14. Remove a system from SSH configuration"
|
|
|
|
printf "%b\n" "15. View SSH configuration"
|
|
|
|
printf "%b\n" "16. Backup files from remote host"
|
|
|
|
printf "%b\n" "17. Sync directories with remote host"
|
|
|
|
printf "%b\n" "18. Exit"
|
2024-09-22 17:01:10 +01:00
|
|
|
printf "%b" "Enter your choice: "
|
2024-09-19 18:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Function to execute the selected SSH operation
|
|
|
|
main() {
|
|
|
|
while true; do
|
|
|
|
show_menu
|
2024-09-19 22:47:08 +01:00
|
|
|
read -r choice
|
2024-09-19 18:16:03 +01:00
|
|
|
case $choice in
|
|
|
|
1) ask_for_host_details ;;
|
2024-09-22 17:01:10 +01:00
|
|
|
2) show_available_hosts && printf "%b" "Enter the alias of the host to connect to: " && read -r host_alias; ssh "$host_alias" ;;
|
2024-09-19 18:16:03 +01:00
|
|
|
3) generate_ssh_key ;;
|
|
|
|
4) share_ssh_key ;;
|
|
|
|
5) disable_password_auth ;;
|
|
|
|
6) enable_password_auth ;;
|
|
|
|
7) check_password_auth ;;
|
|
|
|
8) check_ssh_key_authentication ;;
|
|
|
|
9) run_remote_command ;;
|
|
|
|
10) copy_file_to_remote ;;
|
|
|
|
11) copy_directory_to_remote ;;
|
|
|
|
12) move_file_to_remote ;;
|
|
|
|
13) move_directory_to_remote ;;
|
|
|
|
14) remove_system ;;
|
|
|
|
15) view_ssh_config ;;
|
|
|
|
16) backup_files ;;
|
|
|
|
17) sync_directories ;;
|
|
|
|
18) exit ;;
|
|
|
|
*) printf "%b\n" "Invalid choice. Please try again." ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
checkEnv
|
|
|
|
checkEscalationTool
|
2024-09-22 17:01:10 +01:00
|
|
|
main
|