Delete 'k3os/toolbox.sh'
							parent
							
								
									928bdce6cc
								
							
						
					
					
						commit
						e1d8079b24
					
				@ -1,271 +0,0 @@
 | 
			
		||||
#!/bin/sh
 | 
			
		||||
# k3os-config.sh
 | 
			
		||||
# A configuration utility for k3os environments (BusyBox/Alpine compatible)
 | 
			
		||||
 | 
			
		||||
# --- Global Variables and Constants ---
 | 
			
		||||
 | 
			
		||||
CONNMAN_CONFIG="/var/lib/connman/default.config"
 | 
			
		||||
HOSTNAME_FILE="/var/lib/rancher/k3os/hostname"
 | 
			
		||||
SSHD_CONFIG="/etc/ssh/sshd_config"
 | 
			
		||||
 | 
			
		||||
# Get current user's home directory for Kubeconfig
 | 
			
		||||
USER_HOME=$(getent passwd "$(whoami)" | cut -d: -f6)
 | 
			
		||||
KUBECONFIG_PATH="$USER_HOME/.kube/config"
 | 
			
		||||
 | 
			
		||||
# --- ANSI Color Definitions (Portable across BusyBox) ---
 | 
			
		||||
# Use %b in printf to interpret these backslash escapes
 | 
			
		||||
RED='\033[31m'
 | 
			
		||||
GREEN='\033[32m'
 | 
			
		||||
YELLOW='\033[33m'
 | 
			
		||||
BLUE='\033[34m'
 | 
			
		||||
MAGENTA='\033[35m'
 | 
			
		||||
CYAN='\033[36m'
 | 
			
		||||
BOLD='\033[1m'
 | 
			
		||||
BG_BLUE='\033[44;1m'
 | 
			
		||||
NC='\033[0m' # No Color
 | 
			
		||||
 | 
			
		||||
# --- Utility Functions ---
 | 
			
		||||
 | 
			
		||||
# Function to display system status indicators
 | 
			
		||||
show_status() {
 | 
			
		||||
    printf '\n'
 | 
			
		||||
    printf '%b\n' "${BG_BLUE}--- K3OS Node Status ---${NC}"
 | 
			
		||||
 | 
			
		||||
    # 1. Hostname
 | 
			
		||||
    CURRENT_HOSTNAME=$(hostname)
 | 
			
		||||
 | 
			
		||||
    # 2. SSHD Password Auth Status
 | 
			
		||||
    SSHD_STATUS="${RED}Error (Need Sudo)${NC}"
 | 
			
		||||
    if sudo test -f "$SSHD_CONFIG"; then
 | 
			
		||||
        if sudo grep -q '^PasswordAuthentication yes' "$SSHD_CONFIG"; then
 | 
			
		||||
            SSHD_STATUS="${GREEN}Enabled${NC}"
 | 
			
		||||
        else
 | 
			
		||||
            SSHD_STATUS="${YELLOW}Disabled${NC}"
 | 
			
		||||
        fi
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    # 3. Static IP Configuration Check (from Connman config)
 | 
			
		||||
    STATIC_IP=""
 | 
			
		||||
    CONFIG_IFACE=""
 | 
			
		||||
    if sudo test -f "$CONNMAN_CONFIG"; then
 | 
			
		||||
        CONNMAN_CONTENT=$(sudo cat "$CONNMAN_CONFIG" 2>/dev/null)
 | 
			
		||||
        CONFIG_IFACE=$(echo "$CONNMAN_CONTENT" | grep '^\[service_' | head -n 1 | sed -e 's/^\[service_\(.*\)\].*$/\1/' | tr -d '[]')
 | 
			
		||||
        STATIC_IP=$(echo "$CONNMAN_CONTENT" | grep -oE 'IPv4 = ([0-9]{1,3}\.){3}[0-9]{1,3}' 2>/dev/null | head -n 1 | awk '{print $3}' | cut -d'/' -f1)
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    # 4. Current/Active IP and DHCP status - Use the default route interface
 | 
			
		||||
    ACTIVE_IFACE=$(ip route get 1.1.1.1 2>/dev/null | grep -o 'dev [^ ]*' | awk '{print $2}' | head -n 1)
 | 
			
		||||
    if [ -z "$ACTIVE_IFACE" ]; then
 | 
			
		||||
        ACTIVE_IFACE="eth0"
 | 
			
		||||
    fi
 | 
			
		||||
    
 | 
			
		||||
    CURRENT_IP=$(ip addr show "$ACTIVE_IFACE" 2>/dev/null | grep 'inet ' | awk '{print $2}' | cut -d/ -f1 | head -n 1)
 | 
			
		||||
 | 
			
		||||
    DHCP_STATUS="${RED}N/A${NC}"
 | 
			
		||||
    IP_ADDRESS_STATUS="${RED}No Active Link${NC}"
 | 
			
		||||
 | 
			
		||||
    if [ -n "$CURRENT_IP" ]; then
 | 
			
		||||
        IP_ADDRESS_STATUS="$CURRENT_IP (${CYAN}$ACTIVE_IFACE${NC})"
 | 
			
		||||
        if [ -n "$STATIC_IP" ] && [ "$STATIC_IP" = "$CURRENT_IP" ]; then
 | 
			
		||||
            DHCP_STATUS="${YELLOW}No${NC} (${CYAN}Static Configured${NC}: $CONFIG_IFACE)"
 | 
			
		||||
        else
 | 
			
		||||
            DHCP_STATUS="${GREEN}Yes${NC} (or Link-Local)"
 | 
			
		||||
        fi
 | 
			
		||||
    elif [ -n "$STATIC_IP" ]; then
 | 
			
		||||
        IP_ADDRESS_STATUS="${YELLOW}Static Configured${NC}: $STATIC_IP (Inactive)"
 | 
			
		||||
        DHCP_STATUS="${YELLOW}No${NC} (Check Link)"
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    # Display results using printf for aligned columns
 | 
			
		||||
    printf '%b%-30s%b: %s\n' "$BOLD" "Hostname" "$NC" "$CURRENT_HOSTNAME"
 | 
			
		||||
    printf '%b%-30s%b: %s\n' "$BOLD" "Active IP Address (Interface)" "$NC" "$IP_ADDRESS_STATUS"
 | 
			
		||||
    printf '%b%-30s%b: %s\n' "$BOLD" "DHCP Assigned" "$NC" "$DHCP_STATUS"
 | 
			
		||||
    printf '%b%-30s%b: %s\n' "$BOLD" "SSHD Password Authentication" "$NC" "$SSHD_STATUS"
 | 
			
		||||
    printf '\n'
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Function to notify user about reboot
 | 
			
		||||
notify_reboot() {
 | 
			
		||||
    printf '\n%b*** Configuration Applied ***%b\n' "${YELLOW}${BOLD}" "${NC}"
 | 
			
		||||
    printf 'For the new settings (Hostname, IP) to take full effect, you must reboot.\n'
 | 
			
		||||
    printf '%b(Note: SSH changes are active, but a reboot is safer).%b\n' "$CYAN" "$NC"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Helper to run a command that modifies a root-owned file
 | 
			
		||||
run_privileged() {
 | 
			
		||||
    printf '\n%b[ACTION REQUIRING SUDO]%b\n' "${BLUE}${BOLD}" "${NC}"
 | 
			
		||||
    # Use sudo sh -c to execute the command as root
 | 
			
		||||
    if sudo sh -c "$1"; then
 | 
			
		||||
        return 0
 | 
			
		||||
    else
 | 
			
		||||
        printf '\n%bError: Sudo command failed. Check password or permissions.%b\n' "${RED}" "${NC}"
 | 
			
		||||
        return 1
 | 
			
		||||
    fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# --- Menu Option Functions (1, 2, 3 require sudo via run_privileged) ---
 | 
			
		||||
 | 
			
		||||
# a. Update hostname
 | 
			
		||||
update_hostname() {
 | 
			
		||||
    printf "\n--- Update Hostname ---\n"
 | 
			
		||||
    printf "The current hostname is: %b%s%b\n" "$CYAN" "$(hostname)" "$NC"
 | 
			
		||||
    printf "Enter new hostname: "
 | 
			
		||||
    read NEW_HOSTNAME
 | 
			
		||||
 | 
			
		||||
    if [ -z "$NEW_HOSTNAME" ]; then
 | 
			
		||||
        printf '%b\n' "${RED}Hostname cannot be empty. Aborting.${NC}"
 | 
			
		||||
        return
 | 
			
		||||
    fi
 | 
			
		||||
    
 | 
			
		||||
    # Use run_privileged for writing to the root-owned file
 | 
			
		||||
    if run_privileged "echo '$NEW_HOSTNAME' > $HOSTNAME_FILE"; then
 | 
			
		||||
        # This updates the *runtime* hostname, not just the file
 | 
			
		||||
        hostname "$NEW_HOSTNAME"
 | 
			
		||||
        printf '\n%bSuccessfully updated hostname to: %s%b\n' "$GREEN" "$NEW_HOSTNAME" "$NC"
 | 
			
		||||
        notify_reboot
 | 
			
		||||
    fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# b. Configure IP address (Static)
 | 
			
		||||
configure_ip() {
 | 
			
		||||
    printf "\n--- Configure Static IP Address (Connman) ---\n"
 | 
			
		||||
    
 | 
			
		||||
    # 1. Detect Interface
 | 
			
		||||
    IFACE=$(ip route get 1.1.1.1 2>/dev/null | grep -o 'dev [^ ]*' | awk '{print $2}' | head -n 1)
 | 
			
		||||
    
 | 
			
		||||
    if [ -z "$IFACE" ]; then
 | 
			
		||||
        printf '%b\n' "${RED}Could not detect a default network interface. Aborting.${NC}"
 | 
			
		||||
        return
 | 
			
		||||
    fi
 | 
			
		||||
    printf "Detected primary network interface: %b%s%b\n" "$CYAN" "$IFACE" "$NC"
 | 
			
		||||
 | 
			
		||||
    printf "Enter static IP address (e.g., 192.168.1.10): "
 | 
			
		||||
    read IP_ADDR
 | 
			
		||||
 | 
			
		||||
    printf "Enter netmask prefix (e.g., 24 for 255.255.255.0): "
 | 
			
		||||
    read MASK_PREFIX
 | 
			
		||||
 | 
			
		||||
    printf "Enter gateway address (e.g., 192.168.1.1): "
 | 
			
		||||
    read GATEWAY
 | 
			
		||||
 | 
			
		||||
    printf "Enter nameserver IP (e.g., 8.8.8.8): "
 | 
			
		||||
    read NAMESERVER
 | 
			
		||||
 | 
			
		||||
    if [ -z "$IP_ADDR" ] || [ -z "$MASK_PREFIX" ] || [ -z "$GATEWAY" ] || [ -z "$NAMESERVER" ]; then
 | 
			
		||||
        printf '%b\n' "${RED}All fields are required. Aborting.${NC}"
 | 
			
		||||
        return
 | 
			
		||||
    fi
 | 
			
		||||
    
 | 
			
		||||
    # Generate Connman config content in a shell variable
 | 
			
		||||
    CONFIG_CONTENT="# Automatically generated by k3os-config.sh\n"
 | 
			
		||||
    CONFIG_CONTENT="${CONFIG_CONTENT}[service_$IFACE]\n"
 | 
			
		||||
    CONFIG_CONTENT="${CONFIG_CONTENT}Type = ethernet\n"
 | 
			
		||||
    CONFIG_CONTENT="${CONFIG_CONTENT}Nameservers = $NAMESERVER\n"
 | 
			
		||||
    CONFIG_CONTENT="${CONFIG_CONTENT}IPv4 = $IP_ADDR/$MASK_PREFIX/$GATEWAY\n"
 | 
			
		||||
    
 | 
			
		||||
    # Use run_privileged to write the configuration content
 | 
			
		||||
    printf "Writing static configuration to %s...\n" "$CONNMAN_CONFIG"
 | 
			
		||||
    
 | 
			
		||||
    # Use tee to safely overwrite the file as root.
 | 
			
		||||
    # Note: We use single quotes around $CONNMAN_CONFIG for safety, but double quotes around $CONFIG_CONTENT to allow variable expansion.
 | 
			
		||||
    PRIV_CMD="printf \"\%b\" \"$CONFIG_CONTENT\" | tee \"$CONNMAN_CONFIG\" >/dev/null"
 | 
			
		||||
 | 
			
		||||
    if run_privileged "$PRIV_CMD"; then
 | 
			
		||||
        printf '\n%bStatic IP configuration saved for interface %s.%b\n' "$GREEN" "$IFACE" "$NC"
 | 
			
		||||
        printf "IP: %s/%s, Gateway: %s, Nameserver: %s\n" "$IP_ADDR" "$MASK_PREFIX" "$GATEWAY" "$NAMESERVER"
 | 
			
		||||
        notify_reboot
 | 
			
		||||
    fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# c. Toggle sshd password authentication
 | 
			
		||||
toggle_ssh_auth() {
 | 
			
		||||
    printf "\n--- Toggle SSHD Password Authentication ---\n"
 | 
			
		||||
    
 | 
			
		||||
    # Check current status by reading the root-owned file
 | 
			
		||||
    if sudo grep -q '^PasswordAuthentication yes' "$SSHD_CONFIG" 2>/dev/null; then
 | 
			
		||||
        CURRENT_SETTING="yes"
 | 
			
		||||
        NEW_SETTING="no"
 | 
			
		||||
    else
 | 
			
		||||
        CURRENT_SETTING="no"
 | 
			
		||||
        NEW_SETTING="yes"
 | 
			
		||||
    fi
 | 
			
		||||
    
 | 
			
		||||
    printf "Current setting: %b%s%b\n" "$CYAN" "$CURRENT_SETTING" "$NC"
 | 
			
		||||
    
 | 
			
		||||
    # 1. Check if line exists (commented or not) and replace it
 | 
			
		||||
    # 2. If it doesn't exist, append it
 | 
			
		||||
    
 | 
			
		||||
    # We use a combined, double-quoted command for run_privileged
 | 
			
		||||
    PRIV_CMD="if grep -q '^#*PasswordAuthentication' $SSHD_CONFIG; then
 | 
			
		||||
        sed -i 's/^#*PasswordAuthentication .*/PasswordAuthentication $NEW_SETTING/g' $SSHD_CONFIG
 | 
			
		||||
    else
 | 
			
		||||
        echo 'PasswordAuthentication $NEW_SETTING' >> $SSHD_CONFIG
 | 
			
		||||
    fi"
 | 
			
		||||
 | 
			
		||||
    if run_privileged "$PRIV_CMD"; then
 | 
			
		||||
        printf "Setting changed to: %b%s%b\n" "$GREEN" "$NEW_SETTING" "$NC"
 | 
			
		||||
        
 | 
			
		||||
        printf "Restarting SSH service...\n"
 | 
			
		||||
        # Restarting the service also requires sudo
 | 
			
		||||
        if run_privileged "/etc/init.d/sshd restart"; then
 | 
			
		||||
            printf '\n%bSSHD service restarted. Password authentication is now set to %s.%b\n' "$GREEN" "$NEW_SETTING" "$NC"
 | 
			
		||||
        fi
 | 
			
		||||
    fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# d. Download kubeconfig from master (NO SUDO REQUIRED)
 | 
			
		||||
download_kubeconfig() {
 | 
			
		||||
    printf "\n--- Download Kubeconfig ---\n"
 | 
			
		||||
    printf "Enter k3s master IP address: "
 | 
			
		||||
    read MASTER_IP
 | 
			
		||||
    
 | 
			
		||||
    if [ -z "$MASTER_IP" ]; then
 | 
			
		||||
        printf '%b\n' "${RED}Master IP cannot be empty. Aborting.${NC}"
 | 
			
		||||
        return
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    printf "Attempting to download /etc/rancher/k3s/k3s.yaml from rancher@%s\n" "$MASTER_IP"
 | 
			
		||||
    printf "(You will be prompted for the 'rancher' user's password on the remote master.)\n"
 | 
			
		||||
 | 
			
		||||
    # Create .kube directory in the current user's home directory
 | 
			
		||||
    mkdir -p "$USER_HOME/.kube"
 | 
			
		||||
    
 | 
			
		||||
    # Use scp to copy the file to the user's local path
 | 
			
		||||
    if scp "rancher@$MASTER_IP:/etc/rancher/k3s/k3s.yaml" "$KUBECONFIG_PATH"; then
 | 
			
		||||
        printf '\n%bSuccessfully downloaded kubeconfig to %s%b\n' "$GREEN" "$KUBECONFIG_PATH" "$NC"
 | 
			
		||||
        printf "The config file is ready for use by 'kubectl' or 'k3s kubectl'.\n"
 | 
			
		||||
    else
 | 
			
		||||
        printf '\n%bError: Kubeconfig download failed.%b\n' "${RED}" "${NC}"
 | 
			
		||||
        printf "Check the master IP, password for 'rancher', and SSH connectivity.\n"
 | 
			
		||||
    fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# --- Main Menu Logic ---
 | 
			
		||||
 | 
			
		||||
main_menu() {
 | 
			
		||||
    while true; do
 | 
			
		||||
        show_status
 | 
			
		||||
        printf '%bSelect an option (1, 2, 3 require sudo):%b\n' "$BOLD" "$NC"
 | 
			
		||||
        printf '  %b1%b) Update hostname (k3os reboot required)\n' "$CYAN" "$NC"
 | 
			
		||||
        printf '  %b2%b) Configure static IP (k3os reboot required)\n' "$CYAN" "$NC"
 | 
			
		||||
        printf '  %b3%b) Toggle sshd PasswordAuthentication (SSHD service restart)\n' "$CYAN" "$NC"
 | 
			
		||||
        printf '  %b4%b) Download kubeconfig from master (Runs as user, NO local sudo)\n' "$CYAN" "$NC"
 | 
			
		||||
        printf '  %bQ%b) Quit\n' "$RED" "$NC"
 | 
			
		||||
        printf "\nOption: "
 | 
			
		||||
        read CHOICE
 | 
			
		||||
 | 
			
		||||
        case "$CHOICE" in
 | 
			
		||||
            1) update_hostname ;;
 | 
			
		||||
            2) configure_ip ;;
 | 
			
		||||
            3) toggle_ssh_auth ;;
 | 
			
		||||
            4) download_kubeconfig ;;
 | 
			
		||||
            [Qq]*) printf "\nExiting K3OS configuration utility. Goodbye.\n"; exit 0 ;;
 | 
			
		||||
            *) printf '\n%bInvalid option. Please choose 1, 2, 3, 4, or Q.%b\n' "${RED}" "${NC}" ;;
 | 
			
		||||
        esac
 | 
			
		||||
        printf "\nPress ENTER to return to the menu...\n"
 | 
			
		||||
        read DUMMY
 | 
			
		||||
    done
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# --- Script Execution ---
 | 
			
		||||
main_menu
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue