.
This commit is contained in:
Vendored
Vendored
+169
@@ -0,0 +1,169 @@
|
||||
# Raspberry Pi/Debian Automated Setup Scripts
|
||||
|
||||
## Overview
|
||||
This project provides a collection of scripts to automate the configuration of Raspberry Pi or Debian-based systems. The main script (`setup_raspberry.sh`) allows you to easily configure network settings, disable the graphical interface to save RAM, and customize system behavior.
|
||||
|
||||
## Features
|
||||
- Set static MAC address for Ethernet interface (eth0)
|
||||
- Automatically disable Wi-Fi (wlan0) if Ethernet is active and has an IP
|
||||
- Configure static IP address with custom gateway
|
||||
- Disable graphical environment to save system resources
|
||||
- Custom welcome message with system information
|
||||
- Automated setup with a single command
|
||||
|
||||
## Prerequisites
|
||||
- Raspberry Pi or Debian-based Linux system
|
||||
- Internet connection
|
||||
- sudo privileges
|
||||
|
||||
## Installation & Usage
|
||||
|
||||
### Quick Setup via curl
|
||||
You can execute the setup script directly from GitLab using curl:
|
||||
|
||||
#### Basic interactive setup (prompts for each option):
|
||||
```bash
|
||||
curl -s https://gitlab.spiralragetech.com/tiago.aica/scripts-linux/-/raw/main/debian/setup_raspberry.sh | sudo bash
|
||||
```
|
||||
|
||||
#### Configure MAC address only:
|
||||
```bash
|
||||
curl -s https://gitlab.spiralragetech.com/tiago.aica/scripts-linux/-/raw/main/debian/setup_raspberry.sh | sudo bash -s "94:29:48:23:6A:B8"
|
||||
```
|
||||
|
||||
#### Configure MAC address and disable GUI:
|
||||
```bash
|
||||
curl -s https://gitlab.spiralragetech.com/tiago.aica/scripts-linux/-/raw/main/debian/setup_raspberry.sh | sudo bash -s "94:29:48:23:6A:B8" disable_gui
|
||||
```
|
||||
|
||||
#### Configure MAC, disable GUI, and set static IP with gateway:
|
||||
```bash
|
||||
curl -s https://gitlab.spiralragetech.com/tiago.aica/scripts-linux/-/raw/main/debian/setup_raspberry.sh | sudo bash -s "94:29:48:23:6A:B8" disable_gui "192.168.1.100/24" "192.168.1.1"
|
||||
```
|
||||
|
||||
### Manual Installation
|
||||
Alternatively, download and run the script locally:
|
||||
|
||||
1. Download the setup script:
|
||||
```bash
|
||||
wget https://gitlab.spiralragetech.com/tiago.aica/scripts-linux/-/raw/main/debian/setup_raspberry.sh
|
||||
```
|
||||
|
||||
2. Make it executable:
|
||||
```bash
|
||||
chmod +x setup_raspberry.sh
|
||||
```
|
||||
|
||||
3. Run the script:
|
||||
```bash
|
||||
sudo ./setup_raspberry.sh
|
||||
```
|
||||
|
||||
## Script Parameters
|
||||
The setup_raspberry.sh script accepts these parameters in order:
|
||||
1. MAC_ADDRESS (e.g., "94:29:48:23:6A:B8")
|
||||
2. GUI_ACTION (use "disable_gui" to disable graphical environment)
|
||||
3. IP_ADDRESS (e.g., "192.168.1.100/24")
|
||||
4. GATEWAY (e.g., "192.168.1.1")
|
||||
|
||||
## Components
|
||||
|
||||
### 1. set_mac_wifi.sh
|
||||
- Sets static MAC address for eth0
|
||||
- Disables wlan0 if eth0 gets an IP address after 60 seconds
|
||||
- Logs all actions to /var/log/set_mac.log
|
||||
|
||||
### 2. gui_manager.sh
|
||||
- Manages graphical environment (enable/disable)
|
||||
- Helps save RAM by disabling GUI when not needed
|
||||
- Usage:
|
||||
- Disable GUI: `sudo GUI_ACTION=disable /usr/local/bin/gui_manager.sh`
|
||||
- Enable GUI: `sudo GUI_ACTION=enable /usr/local/bin/gui_manager.sh`
|
||||
|
||||
### 3. set_static_ip.sh
|
||||
- Configures static IP for active interface (eth0 or wlan0)
|
||||
- Supports custom gateway configuration
|
||||
- Automatically detects network management system (dhcpcd, netplan, NetworkManager)
|
||||
- Usage:
|
||||
- With parameters: `sudo IP_ADDRESS=192.168.1.100/24 GATEWAY=192.168.1.1 /usr/local/bin/set_static_ip.sh`
|
||||
- Interactive: `sudo /usr/local/bin/set_static_ip.sh`
|
||||
|
||||
### 4. welcome.sh
|
||||
- Displays custom welcome message on login
|
||||
- Shows system information (uptime, IP, memory usage, etc.)
|
||||
- Automatically added to /etc/profile
|
||||
|
||||
## Configuration Examples
|
||||
|
||||
### Example 1: Minimal Configuration (MAC only)
|
||||
```bash
|
||||
curl -s https://gitlab.spiralragetech.com/tiago.aica/scripts-linux/-/raw/main/debian/setup_raspberry.sh | sudo bash -s "00:1A:2B:3C:4D:5E"
|
||||
```
|
||||
|
||||
### Example 2: Network Configuration
|
||||
```bash
|
||||
curl -s https://gitlab.spiralragetech.com/tiago.aica/scripts-linux/-/raw/main/debian/setup_raspberry.sh | sudo bash -s "" "" "192.168.1.100/24" "192.168.1.1"
|
||||
```
|
||||
|
||||
### Example 3: Full Configuration
|
||||
```bash
|
||||
curl -s https://gitlab.spiralragetech.com/tiago.aica/scripts-linux/-/raw/main/debian/setup_raspberry.sh | sudo bash -s "00:1A:2B:3C:4D:5E" disable_gui "192.168.1.100/24" "192.168.1.1"
|
||||
```
|
||||
|
||||
## Post-Installation
|
||||
|
||||
### Checking Configurations
|
||||
- View MAC configuration: `cat /usr/local/bin/set_mac_wifi.sh | grep ETH0_MAC | head -n 1`
|
||||
- View static IP configuration: `ip addr show`
|
||||
- View crontab entries: `crontab -l`
|
||||
|
||||
### Manual Configuration
|
||||
After installation, you can manually run any component:
|
||||
```bash
|
||||
# Change MAC address
|
||||
sudo /usr/local/bin/set_mac_wifi.sh
|
||||
|
||||
# Change static IP
|
||||
sudo IP_ADDRESS=192.168.1.200/24 GATEWAY=192.168.1.1 /usr/local/bin/set_static_ip.sh
|
||||
|
||||
# Toggle GUI
|
||||
sudo GUI_ACTION=enable /usr/local/bin/gui_manager.sh
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
1. **Check log files** for errors:
|
||||
```bash
|
||||
cat /var/log/raspberry_setup.log
|
||||
cat /var/log/set_mac.log
|
||||
cat /var/log/set_static_ip.log
|
||||
cat /var/log/gui_manager.log
|
||||
```
|
||||
|
||||
2. **Verify network configuration**:
|
||||
```bash
|
||||
ip addr show
|
||||
ip route show
|
||||
cat /etc/resolv.conf
|
||||
```
|
||||
|
||||
3. **Check service status**:
|
||||
```bash
|
||||
systemctl status dhcpcd
|
||||
systemctl status networking
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
1. The scripts will create backups of configuration files before making changes.
|
||||
2. A reboot is recommended after configuration to ensure all changes take effect.
|
||||
3. For security, the scripts set proper permissions on configuration files.
|
||||
4. The setup script will prompt before making any changes to your system.
|
||||
|
||||
## Support
|
||||
|
||||
For issues or questions:
|
||||
1. Check the log files in /var/log/
|
||||
2. Verify your network configuration
|
||||
3. Ensure you have sudo privileges
|
||||
4. Check the project repository for updates
|
||||
Vendored
+189
@@ -0,0 +1,189 @@
|
||||
#!/bin/bash
|
||||
|
||||
# =============================================
|
||||
# Graphical Environment Manager Script
|
||||
# Disables/enables graphical environment to save RAM
|
||||
# Must be run with sudo!
|
||||
#
|
||||
# Usage:
|
||||
# ./gui_manager.sh [enable|disable|status]
|
||||
# GUI_ACTION=enable|disable ./gui_manager.sh
|
||||
# =============================================
|
||||
|
||||
# --- Configuration ---
|
||||
LOG_FILE="/var/log/gui_manager.log"
|
||||
|
||||
# --- Functions ---
|
||||
|
||||
# Function to check if system uses systemd
|
||||
is_systemd() {
|
||||
[ -d /run/systemd/system ] && return 0 || return 1
|
||||
}
|
||||
|
||||
# Function to get current display manager
|
||||
get_display_manager() {
|
||||
if [ -f /etc/X11/default-display-manager ]; then
|
||||
cat /etc/X11/default-display-manager
|
||||
elif systemctl list-units --type=service | grep -q "gdm"; then
|
||||
echo "/usr/sbin/gdm3"
|
||||
elif systemctl list-units --type=service | grep -q "lightdm"; then
|
||||
echo "/usr/sbin/lightdm"
|
||||
elif systemctl list-units --type=service | grep -q "sddm"; then
|
||||
echo "/usr/sbin/sddm"
|
||||
elif systemctl list-units --type=service | grep -q "lxdm"; then
|
||||
echo "/usr/sbin/lxdm"
|
||||
else
|
||||
echo "unknown"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to get current target
|
||||
get_current_target() {
|
||||
if is_systemd; then
|
||||
systemctl get-default
|
||||
else
|
||||
echo "System doesn't use systemd"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to show GUI status
|
||||
show_gui_status() {
|
||||
if ! is_systemd; then
|
||||
echo "$(date): System doesn't use systemd" | tee -a "$LOG_FILE"
|
||||
return 1
|
||||
fi
|
||||
|
||||
current_target=$(get_current_target)
|
||||
dm=$(get_display_manager)
|
||||
|
||||
echo "$(date): Current system target: $current_target" | tee -a "$LOG_FILE"
|
||||
echo "$(date): Display Manager: $(basename "$dm")" | tee -a "$LOG_FILE"
|
||||
|
||||
if [ "$current_target" = "graphical.target" ]; then
|
||||
echo "$(date): Graphical environment is ENABLED" | tee -a "$LOG_FILE"
|
||||
else
|
||||
echo "$(date): Graphical environment is DISABLED" | tee -a "$LOG_FILE"
|
||||
fi
|
||||
|
||||
# Show memory usage
|
||||
echo "$(date): Memory usage:" | tee -a "$LOG_FILE"
|
||||
free -h | awk '/^Mem:/ {print " Total: " $2 ", Used: " $3 ", Free: " $4}' | tee -a "$LOG_FILE"
|
||||
}
|
||||
|
||||
# Function to disable graphical environment
|
||||
disable_gui() {
|
||||
if ! is_systemd; then
|
||||
echo "$(date): Error: This system doesn't use systemd" | tee -a "$LOG_FILE"
|
||||
return 1
|
||||
fi
|
||||
|
||||
current_target=$(get_current_target)
|
||||
if [ "$current_target" != "graphical.target" ]; then
|
||||
echo "$(date): Graphical environment is already disabled" | tee -a "$LOG_FILE"
|
||||
return 0
|
||||
fi
|
||||
|
||||
echo "$(date): Disabling graphical environment..." | tee -a "$LOG_FILE"
|
||||
|
||||
# Get current display manager
|
||||
dm=$(get_display_manager)
|
||||
if [ "$dm" = "unknown" ]; then
|
||||
echo "$(date): Error: Could not determine display manager" | tee -a "$LOG_FILE"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Show memory usage before
|
||||
echo "$(date): Memory usage before disabling GUI:" | tee -a "$LOG_FILE"
|
||||
free -h | awk '/^Mem:/ {print " Total: " $2 ", Used: " $3 ", Free: " $4}' | tee -a "$LOG_FILE"
|
||||
|
||||
# Stop display manager
|
||||
echo "$(date): Stopping display manager $(basename "$dm")..." | tee -a "$LOG_FILE"
|
||||
systemctl stop "$(basename "$dm")"
|
||||
|
||||
# Disable graphical target
|
||||
echo "$(date): Setting default target to multi-user.target..." | tee -a "$LOG_FILE"
|
||||
systemctl set-default multi-user.target
|
||||
|
||||
# Show memory usage after
|
||||
echo "$(date): Memory usage after disabling GUI:" | tee -a "$LOG_FILE"
|
||||
free -h | awk '/^Mem:/ {print " Total: " $2 ", Used: " $3 ", Free: " $4}' | tee -a "$LOG_FILE"
|
||||
|
||||
echo "$(date): Graphical environment disabled successfully" | tee -a "$LOG_FILE"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to enable graphical environment
|
||||
enable_gui() {
|
||||
if ! is_systemd; then
|
||||
echo "$(date): Error: This system doesn't use systemd" | tee -a "$LOG_FILE"
|
||||
return 1
|
||||
fi
|
||||
|
||||
current_target=$(get_current_target)
|
||||
if [ "$current_target" = "graphical.target" ]; then
|
||||
echo "$(date): Graphical environment is already enabled" | tee -a "$LOG_FILE"
|
||||
return 0
|
||||
fi
|
||||
|
||||
echo "$(date): Enabling graphical environment..." | tee -a "$LOG_FILE"
|
||||
|
||||
# Get current display manager
|
||||
dm=$(get_display_manager)
|
||||
if [ "$dm" = "unknown" ]; then
|
||||
echo "$(date): Error: Could not determine display manager" | tee -a "$LOG_FILE"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Enable graphical target
|
||||
echo "$(date): Setting default target to graphical.target..." | tee -a "$LOG_FILE"
|
||||
systemctl set-default graphical.target
|
||||
|
||||
# Start display manager
|
||||
echo "$(date): Starting display manager $(basename "$dm")..." | tee -a "$LOG_FILE"
|
||||
systemctl start "$(basename "$dm")"
|
||||
|
||||
echo "$(date): Graphical environment enabled successfully" | tee -a "$LOG_FILE"
|
||||
return 0
|
||||
}
|
||||
|
||||
# --- Main Execution ---
|
||||
|
||||
# Create log directory if it doesn't exist
|
||||
mkdir -p /var/log
|
||||
|
||||
# Determine action based on parameters or environment variable
|
||||
ACTION=""
|
||||
|
||||
# Check for command line parameter
|
||||
if [ $# -gt 0 ]; then
|
||||
ACTION="$1"
|
||||
fi
|
||||
|
||||
# Check for environment variable if no parameter provided
|
||||
if [ -z "$ACTION" ] && [ -n "$GUI_ACTION" ]; then
|
||||
ACTION="$GUI_ACTION"
|
||||
fi
|
||||
|
||||
# Default to status if no action specified
|
||||
if [ -z "$ACTION" ]; then
|
||||
ACTION="status"
|
||||
fi
|
||||
|
||||
# Execute the appropriate action
|
||||
case "$ACTION" in
|
||||
disable)
|
||||
disable_gui
|
||||
;;
|
||||
enable)
|
||||
enable_gui
|
||||
;;
|
||||
status)
|
||||
show_gui_status
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 [enable|disable|status]"
|
||||
echo "Or: GUI_ACTION=enable|disable $0"
|
||||
echo "Invalid action: $ACTION"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
Vendored
+112
@@ -0,0 +1,112 @@
|
||||
#!/bin/bash
|
||||
# Script to set fixed MAC address and disable Wi-Fi if eth0 configuration succeeds
|
||||
# Must be run with sudo!
|
||||
|
||||
# --- Configuration ---
|
||||
ETH0_MAC="00:00:00:00:00:00" # Default MAC - will be changed by setup script
|
||||
# WLAN0_MAC="XX:XX:XX:XX:XX:XX" # Uncomment and set if needed
|
||||
|
||||
# Verify root privileges
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
echo "This script must be run as root" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# --- Functions ---
|
||||
# Function to set MAC and verify success
|
||||
set_mac() {
|
||||
local interface="$1"
|
||||
local mac="$2"
|
||||
local success=false
|
||||
|
||||
if ip link show "$interface" &> /dev/null; then
|
||||
# Try to set MAC
|
||||
if ip link set "$interface" down && \
|
||||
ip link set dev "$interface" address "$mac" && \
|
||||
ip link set "$interface" up; then
|
||||
echo "$(date): MAC address for $interface set to $mac" | tee -a /var/log/set_mac.log
|
||||
success=true
|
||||
else
|
||||
echo "$(date): Failed to set MAC address for $interface" | tee -a /var/log/set_mac.log
|
||||
fi
|
||||
else
|
||||
echo "$(date): Interface $interface not found" | tee -a /var/log/set_mac.log
|
||||
fi
|
||||
|
||||
# Return success (0) or failure (1)
|
||||
$success
|
||||
}
|
||||
|
||||
# Function to configure static IP
|
||||
configure_static_ip() {
|
||||
local ip_script="/usr/local/bin/set_static_ip.sh"
|
||||
|
||||
if [ -f "$ip_script" ]; then
|
||||
# Get current IP configuration
|
||||
local interface
|
||||
if interface=$(get_active_interface); then
|
||||
local current_ip=$(ip -4 addr show "$interface" | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
|
||||
if [[ "$current_ip" =~ ^169\.254\. ]]; then
|
||||
# Link-local address, likely no DHCP
|
||||
log "No valid IP address found for $interface, configuring static IP..."
|
||||
IP_ADDRESS_ENV="10.10.0.100/24" bash "$ip_script"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if interface has IP address
|
||||
has_ip_address() {
|
||||
local interface="$1"
|
||||
# Check if interface is UP and has an IPv4 address
|
||||
if ip link show "$interface" | grep -q "state UP" && \
|
||||
ip -4 addr show "$interface" | grep -q "inet "; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to disable Wi-Fi
|
||||
disable_wifi() {
|
||||
# Create log directory if it doesn't exist
|
||||
mkdir -p /var/log
|
||||
|
||||
# Wait for 1 minute (60 seconds) before disabling Wi-Fi
|
||||
echo "$(date): Waiting 60 seconds before checking eth0 IP and disabling Wi-Fi..." | tee -a /var/log/set_mac.log
|
||||
sleep 60
|
||||
|
||||
# Check if eth0 has an IP address before disabling Wi-Fi
|
||||
if has_ip_address "eth0"; then
|
||||
if ip link show "wlan0" &> /dev/null; then
|
||||
if ip link set "wlan0" down; then
|
||||
echo "$(date): Wi-Fi (wlan0) disabled successfully (eth0 has IP)" | tee -a /var/log/set_mac.log
|
||||
else
|
||||
echo "$(date): Failed to disable Wi-Fi (wlan0)" | tee -a /var/log/set_mac.log
|
||||
fi
|
||||
else
|
||||
echo "$(date): wlan0 interface not found (Wi-Fi already disabled?)" | tee -a /var/log/set_mac.log
|
||||
fi
|
||||
else
|
||||
echo "$(date): eth0 does not have an IP address. Wi-Fi remains active." | tee -a /var/log/set_mac.log
|
||||
fi
|
||||
}
|
||||
|
||||
# --- Execution ---
|
||||
# Set MAC for eth0 and disable Wi-Fi if successful
|
||||
if set_mac "eth0" "$ETH0_MAC"; then
|
||||
# Check if disable_wifi is already running
|
||||
if ! pgrep -f "disable_wifi" > /dev/null; then
|
||||
#configure_static_ip
|
||||
disable_wifi &
|
||||
else
|
||||
echo "$(date): disable_wifi is already running" | tee -a /var/log/set_mac.log
|
||||
fi
|
||||
else
|
||||
echo "$(date): Could not set MAC for eth0. Wi-Fi remains active" | tee -a /var/log/set_mac.log
|
||||
fi
|
||||
|
||||
# Uncomment if you want to set MAC for wlan0 (optional)
|
||||
# if [[ -n "$WLAN0_MAC" ]]; then
|
||||
# set_mac "wlan0" "$WLAN0_MAC"
|
||||
# fi
|
||||
Vendored
+269
@@ -0,0 +1,269 @@
|
||||
#!/bin/bash
|
||||
|
||||
# =============================================
|
||||
# Static IP Configuration Script
|
||||
# Configures static IP for active interface (eth0 or wlan0)
|
||||
# Must be run with sudo!
|
||||
#
|
||||
# Usage:
|
||||
# IP_ADDRESS=192.168.1.100 GATEWAY=192.168.1.1 ./set_static_ip.sh
|
||||
# =============================================
|
||||
|
||||
# --- Configuration ---
|
||||
DEFAULT_GATEWAY="${GATEWAY_ENV:-10.10.0.1}" # Use GATEWAY_ENV if provided, otherwise default
|
||||
DNS_SERVERS="8.8.8.8 8.8.4.4"
|
||||
LOG_FILE="/var/log/set_static_ip.log"
|
||||
NETPLAN_DIR="/etc/netplan"
|
||||
|
||||
# --- Functions ---
|
||||
|
||||
# Function to log messages
|
||||
log() {
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
|
||||
}
|
||||
|
||||
# Function to fix netplan permissions
|
||||
fix_netplan_permissions() {
|
||||
log "Fixing netplan permissions..."
|
||||
|
||||
# Fix permissions for all netplan files
|
||||
find "$NETPLAN_DIR" -type f -name "*.yaml" -exec chmod 600 {} \;
|
||||
find /lib/netplan -type f -name "*.yaml" -exec chmod 600 {} \;
|
||||
find /run/netplan -type f -name "*.yaml" -exec chmod 600 {} \;
|
||||
|
||||
# Fix ownership
|
||||
chown root:root "$NETPLAN_DIR"/*.yaml 2>/dev/null
|
||||
chown root:root /lib/netplan/*.yaml 2>/dev/null
|
||||
chown root:root /run/netplan/*.yaml 2>/dev/null
|
||||
|
||||
log "Netplan permissions fixed"
|
||||
}
|
||||
|
||||
# Function to check if interface is up
|
||||
is_interface_up() {
|
||||
local interface="$1"
|
||||
if ip link show "$interface" | grep -q "state UP"; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to get active interface
|
||||
get_active_interface() {
|
||||
# Check eth0 first
|
||||
if is_interface_up "eth0"; then
|
||||
echo "eth0"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Then check wlan0
|
||||
if is_interface_up "wlan0"; then
|
||||
echo "wlan0"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# No active interface found
|
||||
return 1
|
||||
}
|
||||
|
||||
# Function to check if system uses dhcpcd
|
||||
uses_dhcpcd() {
|
||||
if [ -f /etc/dhcpcd.conf ] && systemctl list-unit-files | grep -q dhcpcd; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if system uses netplan
|
||||
uses_netplan() {
|
||||
if [ -d "$NETPLAN_DIR" ] && command -v netplan >/dev/null; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if system uses NetworkManager
|
||||
uses_network_manager() {
|
||||
if systemctl list-unit-files | grep -q NetworkManager && command -v nmcli >/dev/null; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to configure static IP using dhcpcd
|
||||
configure_static_ip_dhcpcd() {
|
||||
local interface="$1"
|
||||
local ip_address="$2"
|
||||
local gateway="$3"
|
||||
local dns="$4"
|
||||
|
||||
log "Configuring static IP for $interface using dhcpcd..."
|
||||
|
||||
# Backup original dhcpcd.conf
|
||||
if [ ! -f /etc/dhcpcd.conf.bak ]; then
|
||||
cp /etc/dhcpcd.conf /etc/dhcpcd.conf.bak
|
||||
log "Created backup of /etc/dhcpcd.conf"
|
||||
fi
|
||||
|
||||
# Remove any existing configuration for this interface
|
||||
sed -i "/^interface $interface$/,/^$/d" /etc/dhcpcd.conf
|
||||
|
||||
# Add new configuration
|
||||
{
|
||||
echo ""
|
||||
echo "# Static IP configuration for $interface"
|
||||
echo "interface $interface"
|
||||
echo "static ip_address=$ip_address"
|
||||
echo "static routers=$gateway"
|
||||
echo "static domain_name_servers=$dns"
|
||||
} >> /etc/dhcpcd.conf
|
||||
|
||||
log "Static IP configuration added to /etc/dhcpcd.conf"
|
||||
log "Restarting networking..."
|
||||
|
||||
# Try different methods to restart networking
|
||||
if systemctl restart dhcpcd 2>/dev/null; then
|
||||
log "dhcpcd service restarted successfully"
|
||||
return 0
|
||||
elif systemctl restart networking 2>/dev/null; then
|
||||
log "networking service restarted successfully"
|
||||
return 0
|
||||
elif systemctl restart systemd-networkd 2>/dev/null; then
|
||||
log "systemd-networkd service restarted successfully"
|
||||
return 0
|
||||
else
|
||||
log "Could not restart networking service automatically"
|
||||
log "Please reboot the system for changes to take effect"
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to configure static IP using netplan
|
||||
configure_static_ip_netplan() {
|
||||
local interface="$1"
|
||||
local ip_address="$2"
|
||||
local gateway="$3"
|
||||
local dns="$4"
|
||||
|
||||
log "Configuring static IP for $interface using netplan..."
|
||||
|
||||
# Fix netplan permissions first
|
||||
fix_netplan_permissions
|
||||
|
||||
# Find netplan config file
|
||||
local netplan_file=$(ls "$NETPLAN_DIR"/*.yaml 2>/dev/null | head -n 1)
|
||||
|
||||
if [ -z "$netplan_file" ]; then
|
||||
# Create default netplan file if none exists
|
||||
netplan_file="$NETPLAN_DIR/01-static-ip.yaml"
|
||||
log "Creating new netplan configuration file: $netplan_file"
|
||||
else
|
||||
# Backup original netplan config
|
||||
if [ ! -f "$netplan_file.bak" ]; then
|
||||
cp "$netplan_file" "$netplan_file.bak"
|
||||
log "Created backup of $netplan_file"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Create new netplan configuration
|
||||
cat > "$netplan_file" <<EOF
|
||||
network:
|
||||
version: 2
|
||||
renderer: networkd
|
||||
ethernets:
|
||||
$interface:
|
||||
addresses: [$ip_address]
|
||||
routes:
|
||||
- to: default
|
||||
via: $gateway
|
||||
nameservers:
|
||||
addresses: [$(echo $dns | sed 's/ /, /g')]
|
||||
EOF
|
||||
|
||||
# Set correct permissions
|
||||
chmod 600 "$netplan_file"
|
||||
chown root:root "$netplan_file"
|
||||
|
||||
log "Static IP configuration added to $netplan_file"
|
||||
log "Applying netplan configuration..."
|
||||
|
||||
# Apply netplan configuration
|
||||
if netplan apply; then
|
||||
log "Netplan configuration applied successfully"
|
||||
return 0
|
||||
else
|
||||
log "Failed to apply netplan configuration"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to configure static IP using NetworkManager
|
||||
configure_static_ip_network_manager() {
|
||||
local interface="$1"
|
||||
local ip_address="$2"
|
||||
local gateway="$3"
|
||||
local dns="$4"
|
||||
|
||||
log "Configuring static IP for $interface using NetworkManager..."
|
||||
|
||||
# Set static IP
|
||||
if nmcli con show "$interface" >/dev/null 2>&1; then
|
||||
nmcli con mod "$interface" ipv4.method manual ipv4.addresses "$ip_address" ipv4.gateway "$gateway" ipv4.dns "$dns"
|
||||
else
|
||||
nmcli con add type ethernet ifname "$interface" con-name "$interface" ipv4.method manual ipv4.addresses "$ip_address" ipv4.gateway "$gateway" ipv4.dns "$dns"
|
||||
fi
|
||||
|
||||
# Restart connection
|
||||
nmcli con down "$interface" && nmcli con up "$interface"
|
||||
|
||||
log "NetworkManager configuration applied successfully"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to configure static IP
|
||||
configure_static_ip() {
|
||||
local ip_address="${IP_ADDRESS_ENV:-$1}"
|
||||
local gateway="${GATEWAY_ENV:-$DEFAULT_GATEWAY}"
|
||||
local interface
|
||||
local dns="$DNS_SERVERS"
|
||||
|
||||
# Create log directory if it doesn't exist
|
||||
mkdir -p /var/log
|
||||
|
||||
# Get active interface
|
||||
if ! interface=$(get_active_interface); then
|
||||
log "No active network interface found (eth0 or wlan0)"
|
||||
return 1
|
||||
fi
|
||||
|
||||
log "Active interface: $interface"
|
||||
log "Configuring static IP: $ip_address"
|
||||
log "Gateway: $gateway"
|
||||
log "DNS Servers: $dns"
|
||||
|
||||
# Check which network management system is in use
|
||||
if uses_dhcpcd; then
|
||||
configure_static_ip_dhcpcd "$interface" "$ip_address" "$gateway" "$dns"
|
||||
elif uses_netplan; then
|
||||
configure_static_ip_netplan "$interface" "$ip_address" "$gateway" "$dns"
|
||||
elif uses_network_manager; then
|
||||
configure_static_ip_network_manager "$interface" "$ip_address" "$gateway" "$dns"
|
||||
else
|
||||
log "Could not determine network configuration method"
|
||||
log "Please configure your network manually:"
|
||||
log "Interface: $interface"
|
||||
log "IP Address: $ip_address"
|
||||
log "Gateway: $gateway"
|
||||
log "DNS Servers: $dns"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# --- Main Execution ---
|
||||
|
||||
# Configure static IP
|
||||
configure_static_ip
|
||||
Vendored
+517
@@ -0,0 +1,517 @@
|
||||
#!/bin/bash
|
||||
|
||||
# =============================================
|
||||
# Raspberry Pi Setup Script
|
||||
# Downloads and configures system scripts from Git
|
||||
# =============================================
|
||||
|
||||
# Configuration
|
||||
GITHUB_REPO="https://gitlab.spiralragetech.com/tiago.aica/scripts-linux/-/raw/main/debian"
|
||||
SCRIPT_DIR="/usr/local/bin"
|
||||
LOG_FILE="/var/log/raspberry_setup.log"
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[1;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Function to log messages
|
||||
log() {
|
||||
echo -e "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
|
||||
}
|
||||
|
||||
# Function to ask for confirmation
|
||||
ask_confirmation() {
|
||||
local message="$1"
|
||||
local default="$2" # "y" or "n"
|
||||
|
||||
while true; do
|
||||
if [ "$default" = "y" ]; then
|
||||
read -p "$message [Y/n]: " choice < /dev/tty
|
||||
else
|
||||
read -p "$message [y/N]: " choice < /dev/tty
|
||||
fi
|
||||
|
||||
# Default choice if user just presses Enter
|
||||
if [ -z "$choice" ]; then
|
||||
choice="$default"
|
||||
fi
|
||||
|
||||
case "$choice" in
|
||||
y|Y|yes|Yes|YES)
|
||||
return 0
|
||||
;;
|
||||
n|N|no|No|NO)
|
||||
return 1
|
||||
;;
|
||||
*)
|
||||
echo "Please answer yes or no."
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
# Function to validate MAC address format
|
||||
validate_mac() {
|
||||
local mac=$1
|
||||
# MAC address validation regex
|
||||
local mac_regex='^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$'
|
||||
if [[ $mac =~ $mac_regex ]]; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to validate IP address format
|
||||
validate_ip() {
|
||||
local ip=$1
|
||||
# IP address validation regex
|
||||
local ip_regex='^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(/[0-9]+)?$'
|
||||
if [[ $ip =~ $ip_regex ]]; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to validate gateway format
|
||||
validate_gateway() {
|
||||
local gateway=$1
|
||||
# Gateway validation regex
|
||||
local gateway_regex='^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'
|
||||
if [[ $gateway =~ $gateway_regex ]]; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to download file from GitLab
|
||||
download_file() {
|
||||
local filename=$1
|
||||
local destination=$2
|
||||
|
||||
log "Downloading $filename..."
|
||||
if curl -s -o "$destination/$filename" "$GITHUB_REPO/$filename"; then
|
||||
log "${GREEN}Successfully downloaded $filename${NC}"
|
||||
chmod +x "$destination/$filename"
|
||||
return 0
|
||||
else
|
||||
log "${RED}Failed to download $filename${NC}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to configure MAC address
|
||||
configure_mac() {
|
||||
local mac_script="$SCRIPT_DIR/set_mac_wifi.sh"
|
||||
local new_mac=$1
|
||||
|
||||
if [ ! -f "$mac_script" ]; then
|
||||
log "${RED}MAC address script not found${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Validate MAC address format
|
||||
if validate_mac "$new_mac"; then
|
||||
# Normalize to use colons
|
||||
new_mac=$(echo "$new_mac" | tr '-' ':')
|
||||
# Update MAC address in script
|
||||
sed -i "s/ETH0_MAC=.*/ETH0_MAC=\"$new_mac\"/" "$mac_script"
|
||||
log "${GREEN}MAC address updated to $new_mac${NC}"
|
||||
return 0
|
||||
else
|
||||
log "${RED}Invalid MAC address format: $new_mac. Please use format XX:XX:XX:XX:XX:XX or XX-XX-XX-XX-XX-XX${NC}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to setup MAC address
|
||||
setup_mac_address() {
|
||||
if ask_confirmation "Do you want to configure the MAC address for eth0?" "n"; then
|
||||
download_file "set_mac_wifi.sh" "$SCRIPT_DIR" || return 1
|
||||
|
||||
local mac_script="$SCRIPT_DIR/set_mac_wifi.sh"
|
||||
|
||||
# If MAC address was provided as parameter, use it
|
||||
if [ -n "$1" ]; then
|
||||
if configure_mac "$1"; then
|
||||
return 0
|
||||
else
|
||||
log "${RED}Failed to configure MAC address${NC}"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Prompt for MAC address
|
||||
while true; do
|
||||
read -p "Enter MAC address for eth0 (format: XX:XX:XX:XX:XX:XX or XX-XX-XX-XX-XX-XX): " new_mac < /dev/tty
|
||||
if validate_mac "$new_mac"; then
|
||||
if configure_mac "$new_mac"; then
|
||||
return 0
|
||||
else
|
||||
log "${RED}Failed to configure MAC address${NC}"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
log "${RED}Invalid MAC address format. Please use format XX:XX:XX:XX:XX:XX or XX-XX-XX-XX-XX-XX${NC}"
|
||||
fi
|
||||
done
|
||||
else
|
||||
log "${YELLOW}Skipping MAC address configuration${NC}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if crontab entry exists
|
||||
crontab_entry_exists() {
|
||||
local script_path="$1"
|
||||
crontab -l 2>/dev/null | grep -q "@reboot $script_path"
|
||||
return $?
|
||||
}
|
||||
|
||||
# Function to setup crontab
|
||||
setup_crontab() {
|
||||
if ask_confirmation "Do you want to setup crontab to run MAC script at boot?" "y"; then
|
||||
local mac_script="$SCRIPT_DIR/set_mac_wifi.sh"
|
||||
|
||||
# Download script if not present
|
||||
if [ ! -f "$mac_script" ]; then
|
||||
download_file "set_mac_wifi.sh" "$SCRIPT_DIR" || return 1
|
||||
fi
|
||||
|
||||
if crontab_entry_exists "$mac_script"; then
|
||||
log "${YELLOW}Crontab entry for MAC address script already exists${NC}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
log "Setting up crontab for MAC address script..."
|
||||
(crontab -l 2>/dev/null; echo "@reboot $mac_script") | crontab -
|
||||
log "${GREEN}Crontab configured successfully${NC}"
|
||||
return 0
|
||||
else
|
||||
log "${YELLOW}Skipping crontab configuration${NC}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to install dependencies and system updates
|
||||
install_dependencies() {
|
||||
if ask_confirmation "Do you want to update the system and install required dependencies?" "y"; then
|
||||
log "Updating package lists and upgrading installed packages..."
|
||||
|
||||
# Update package lists
|
||||
if ! apt update; then
|
||||
log "${RED}Failed to update package lists${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Upgrade installed packages (excluding kernel)
|
||||
if ask_confirmation "Do you want to perform a packages upgrade?" "n"; then
|
||||
log "Performing package upgrade..."
|
||||
if ! apt upgrade -y --without-new-pkgs; then
|
||||
log "${RED}Failed to upgrade installed packages${NC}"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Upgrade distribution (including kernel)
|
||||
if ask_confirmation "Do you want to perform a distribution upgrade (may include kernel updates)?" "n"; then
|
||||
|
||||
if [ -f /proc/device-tree/model ] && grep -q "Raspberry Pi" /proc/device-tree/model; then
|
||||
log "Raspberry Pi detected - using rpi-update for firmware/kernel updates"
|
||||
|
||||
# Install rpi-update if not already installed
|
||||
if ! command -v rpi-update >/dev/null 2>&1; then
|
||||
log "Installing rpi-update..."
|
||||
if ! apt install -y rpi-update; then
|
||||
log "${RED}Failed to install rpi-update${NC}"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# # Update firmware and kernel
|
||||
# log "Updating Raspberry Pi firmware and kernel..."
|
||||
# if ! rpi-update; then
|
||||
# log "${RED}Failed to update Raspberry Pi firmware/kernel${NC}"
|
||||
# return 1
|
||||
# fi
|
||||
# log "${GREEN}Raspberry Pi firmware and kernel updated successfully${NC}"
|
||||
# log "${YELLOW}Note: A reboot is required for kernel updates to take effect${NC}"
|
||||
|
||||
|
||||
log "Updating Raspberry Pi firmware and kernel..."
|
||||
|
||||
# Install 'yes' command if not available (usually pre-installed)
|
||||
if ! command -v yes >/dev/null 2>&1; then
|
||||
if ! apt install -y coreutils; then
|
||||
log "${RED}Failed to install coreutils package (needed for 'yes' command)${NC}"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Run rpi-update with all prompts automatically answered with 'y'
|
||||
if ! yes | rpi-update; then
|
||||
log "${RED}Failed to update Raspberry Pi firmware/kernel${NC}"
|
||||
|
||||
log "Trying common distribution upgrade..."
|
||||
if ! apt full-upgrade -y; then
|
||||
log "${RED}Failed to perform distribution upgrade${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 1
|
||||
fi
|
||||
|
||||
log "${GREEN}Raspberry Pi firmware and kernel updated successfully${NC}"
|
||||
log "${YELLOW}Note: A reboot is required for kernel updates to take effect${NC}"
|
||||
|
||||
|
||||
else
|
||||
log "Performing distribution upgrade..."
|
||||
if ! apt full-upgrade -y; then
|
||||
log "${RED}Failed to perform distribution upgrade${NC}"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# Install required dependencies
|
||||
if ask_confirmation "Do you want to install the required dependencies (figlet, bc, lsof, net-tools)?" "n"; then
|
||||
log "Installing required dependencies (figlet, bc, lsof, net-tools)..."
|
||||
if apt install -y figlet bc lsof net-tools; then
|
||||
log "${GREEN}Dependencies installed successfully${NC}"
|
||||
return 0
|
||||
else
|
||||
log "${RED}Failed to install dependencies${NC}"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Clean up
|
||||
if ask_confirmation "Do you want to clean the package cache?" "n"; then
|
||||
log "Cleaning up package cache..."
|
||||
if apt autoremove -y && apt clean; then
|
||||
log "${GREEN}Package cache cleaned successfully${NC}"
|
||||
else
|
||||
log "${YELLOW}Warning: Failed to clean package cache${NC}"
|
||||
fi
|
||||
fi
|
||||
|
||||
else
|
||||
log "${YELLOW}Skipping system update and dependency installation${NC}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to setup GUI manager
|
||||
setup_gui_manager() {
|
||||
if ask_confirmation "Do you want to configure the graphical environment?" "y"; then
|
||||
download_file "gui_manager.sh" "$SCRIPT_DIR" || return 1
|
||||
|
||||
local gui_script="$SCRIPT_DIR/gui_manager.sh"
|
||||
|
||||
# If GUI action was provided as parameter, use it
|
||||
if [ "$1" = "disable_gui" ]; then
|
||||
log "Disabling graphical environment as requested..."
|
||||
GUI_ACTION=disable bash "$gui_script"
|
||||
# Provide instructions for re-enabling
|
||||
log "${YELLOW}Note:${NC} The graphical environment has been disabled."
|
||||
log "${YELLOW}To re-enable it later, run:${NC}"
|
||||
log "${YELLOW} sudo GUI_ACTION=enable $gui_script${NC}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Ask if user wants to disable GUI
|
||||
if ask_confirmation "Do you want to disable the graphical environment to save RAM?" "n"; then
|
||||
log "Disabling graphical environment..."
|
||||
GUI_ACTION=disable bash "$gui_script"
|
||||
# Provide instructions for re-enabling
|
||||
log "${YELLOW}Note:${NC} The graphical environment has been disabled."
|
||||
log "${YELLOW}To re-enable it later, run:${NC}"
|
||||
log "${YELLOW} sudo GUI_ACTION=enable $gui_script${NC}"
|
||||
else
|
||||
log "${YELLOW}Graphical environment will remain enabled${NC}"
|
||||
fi
|
||||
return 0
|
||||
else
|
||||
log "${YELLOW}Skipping GUI manager configuration${NC}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to check if welcome script is already in profile
|
||||
welcome_script_in_profile() {
|
||||
local welcome_script="$1"
|
||||
grep -q "$welcome_script" /etc/profile
|
||||
return $?
|
||||
}
|
||||
|
||||
# Function to configure welcome script
|
||||
configure_welcome() {
|
||||
if ask_confirmation "Do you want to configure the welcome script?" "y"; then
|
||||
download_file "welcome.sh" "$SCRIPT_DIR" || return 1
|
||||
|
||||
local welcome_script="$SCRIPT_DIR/welcome.sh"
|
||||
|
||||
if welcome_script_in_profile "$welcome_script"; then
|
||||
log "${YELLOW}Welcome script already configured in /etc/profile${NC}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
log "Configuring welcome script..."
|
||||
echo "$welcome_script" | sudo tee -a /etc/profile > /dev/null
|
||||
log "${GREEN}Welcome script added to /etc/profile${NC}"
|
||||
return 0
|
||||
else
|
||||
log "${YELLOW}Skipping welcome script configuration${NC}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to setup static IP
|
||||
setup_static_ip() {
|
||||
if ask_confirmation "Do you want to configure a static IP address?" "y"; then
|
||||
download_file "set_static_ip.sh" "$SCRIPT_DIR" || return 1
|
||||
|
||||
local ip_script="$SCRIPT_DIR/set_static_ip.sh"
|
||||
local ip_address="$1" # IP address parameter
|
||||
local gateway_address="$2" # Gateway parameter
|
||||
|
||||
# If both IP and gateway were provided as parameters, use them
|
||||
if [ -n "$ip_address" ] && [ -n "$gateway_address" ]; then
|
||||
log "Configuring static IP as requested: $ip_address with gateway $gateway_address"
|
||||
|
||||
# Validate IP and gateway formats
|
||||
if validate_ip "$ip_address" && validate_gateway "$gateway_address"; then
|
||||
IP_ADDRESS_ENV="$ip_address" GATEWAY_ENV="$gateway_address" bash "$ip_script"
|
||||
return 0
|
||||
else
|
||||
log "${RED}Invalid IP or gateway format. Using default configuration.${NC}"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# If only IP was provided, ask for gateway
|
||||
if [ -n "$ip_address" ]; then
|
||||
# Validate IP format first
|
||||
if ! validate_ip "$ip_address"; then
|
||||
log "${RED}Invalid IP address format: $ip_address.${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Ask for gateway
|
||||
while true; do
|
||||
read -p "Enter gateway IP address (format: 192.168.1.1): " gateway_address < /dev/tty
|
||||
if validate_gateway "$gateway_address"; then
|
||||
log "Configuring static IP: $ip_address with gateway $gateway_address"
|
||||
IP_ADDRESS_ENV="$ip_address" GATEWAY_ENV="$gateway_address" bash "$ip_script"
|
||||
return 0
|
||||
else
|
||||
log "${RED}Invalid gateway format. Please use format like 192.168.1.1${NC}"
|
||||
fi
|
||||
done
|
||||
else
|
||||
# Prompt for both IP and gateway
|
||||
while true; do
|
||||
read -p "Enter static IP address (format: 192.168.1.100 or 192.168.1.100/24): " ip_address < /dev/tty
|
||||
if validate_ip "$ip_address"; then
|
||||
while true; do
|
||||
read -p "Enter gateway IP address (format: 192.168.1.1): " gateway_address < /dev/tty
|
||||
if validate_gateway "$gateway_address"; then
|
||||
log "Configuring static IP: $ip_address with gateway $gateway_address"
|
||||
IP_ADDRESS_ENV="$ip_address" GATEWAY_ENV="$gateway_address" bash "$ip_script"
|
||||
return 0
|
||||
else
|
||||
log "${RED}Invalid gateway format. Please use format like 192.168.1.1${NC}"
|
||||
fi
|
||||
done
|
||||
else
|
||||
log "${RED}Invalid IP address format. Please use format like 192.168.1.100 or 192.168.1.100/24${NC}"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
else
|
||||
log "${YELLOW}Skipping static IP configuration${NC}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to upgrade Debian distribution
|
||||
upgrade_debian_distro() {
|
||||
if ! ask_confirmation "Do you want to check for Debian distribution upgrades? (WARNING: This may break Raspberry Pi OS compatibility)" "n"; then
|
||||
log "${YELLOW}Skipping Debian distribution upgrade${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Download the upgrade script
|
||||
download_file "upgrade_debian.sh" "$SCRIPT_DIR" || {
|
||||
log "${RED}Failed to download upgrade_debian.sh${NC}"
|
||||
return 1
|
||||
}
|
||||
|
||||
# Execute the upgrade script
|
||||
log "Starting Debian distribution upgrade..."
|
||||
bash "$SCRIPT_DIR/upgrade_debian.sh" || {
|
||||
log "${RED}Failed to upgrade Debian distribution${NC}"
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to prompt for reboot
|
||||
prompt_for_reboot() {
|
||||
if ask_confirmation "Setup completed. Would you like to reboot now?" "n"; then
|
||||
log "${GREEN}Rebooting system...${NC}"
|
||||
sleep 2
|
||||
reboot
|
||||
else
|
||||
log "${YELLOW}Skipping reboot. Please remember to reboot later to apply all changes.${NC}"
|
||||
fi
|
||||
}
|
||||
|
||||
# Main execution
|
||||
main() {
|
||||
local mac_address=$1
|
||||
local gui_action=$2 # GUI
|
||||
local ip_address=$3 # Static IP
|
||||
local gateway_address=$4 # Gateway IP
|
||||
|
||||
log "Starting Raspberry Pi setup..."
|
||||
|
||||
# Create script directory if it doesn't exist
|
||||
mkdir -p "$SCRIPT_DIR"
|
||||
|
||||
# Install dependencies
|
||||
install_dependencies
|
||||
|
||||
# Check for Debian distribution upgrades
|
||||
# upgrade_debian_distro
|
||||
|
||||
# Setup static IP
|
||||
setup_static_ip "$ip_address" "$gateway_address"
|
||||
|
||||
# Configure MAC address
|
||||
setup_mac_address "$mac_address"
|
||||
|
||||
# Setup GUI manager
|
||||
setup_gui_manager "$gui_action"
|
||||
|
||||
# Setup crontab
|
||||
setup_crontab
|
||||
|
||||
# Configure welcome script
|
||||
configure_welcome
|
||||
|
||||
log "${GREEN}Setup completed successfully!${NC}"
|
||||
|
||||
# Prompt for reboot
|
||||
prompt_for_reboot
|
||||
}
|
||||
|
||||
# Call main function with all arguments
|
||||
main "$@"
|
||||
Vendored
+255
@@ -0,0 +1,255 @@
|
||||
#!/bin/bash
|
||||
# =============================================
|
||||
# Debian Distribution Upgrade Script
|
||||
# Upgrades Debian to the next stable release
|
||||
# =============================================
|
||||
# Configuration
|
||||
LOG_FILE="/var/log/debian_upgrade.log"
|
||||
BACKUP_DIR="/root/debian_upgrade_backup_$(date +%Y%m%d_%H%M%S)"
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[1;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Function to log messages
|
||||
log() {
|
||||
echo -e "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
|
||||
}
|
||||
|
||||
# Function to ask for confirmation
|
||||
ask_confirmation() {
|
||||
local message="$1"
|
||||
local default="$2" # "y" or "n"
|
||||
while true; do
|
||||
if [ "$default" = "y" ]; then
|
||||
read -p "$message [Y/n]: " choice < /dev/tty
|
||||
else
|
||||
read -p "$message [y/N]: " choice < /dev/tty
|
||||
fi
|
||||
# Default choice if user just presses Enter
|
||||
if [ -z "$choice" ]; then
|
||||
choice="$default"
|
||||
fi
|
||||
case "$choice" in
|
||||
y|Y|yes|Yes|YES)
|
||||
return 0
|
||||
;;
|
||||
n|N|no|No|NO)
|
||||
return 1
|
||||
;;
|
||||
*)
|
||||
echo "Please answer yes or no."
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
# Function to check if running on Raspberry Pi
|
||||
is_raspberry_pi() {
|
||||
if [ -f /proc/device-tree/model ] && grep -q "Raspberry Pi" /proc/device-tree/model; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to validate if curl is installed
|
||||
check_curl() {
|
||||
if ! command -v curl >/dev/null 2>&1; then
|
||||
log "Installing curl..."
|
||||
if ! apt install -y curl; then
|
||||
log "${RED}Failed to install curl${NC}"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to get current Debian version
|
||||
get_current_version() {
|
||||
CURRENT_VERSION=$(cat /etc/debian_version | cut -d'.' -f1)
|
||||
CURRENT_CODENAME=$(grep -oP '(?<=VERSION_CODENAME=).*' /etc/os-release)
|
||||
log "Current Debian version: $CURRENT_CODENAME (Debian $CURRENT_VERSION)"
|
||||
}
|
||||
|
||||
# Function to get next Debian stable version
|
||||
get_next_version() {
|
||||
if ! check_curl; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
NEXT_CODENAME=$(curl -s "http://ftp.debian.org/debian/dists/stable/Release" | grep -oP '(?<=Codename: ).*' | head -n 1)
|
||||
if [ -z "$NEXT_CODENAME" ]; then
|
||||
log "${RED}Failed to fetch next Debian version${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
NEXT_VERSION=$(curl -s "http://ftp.debian.org/debian/dists/${NEXT_CODENAME}/Release" | grep -oP '(?<=Version: ).*' | cut -d'.' -f1)
|
||||
log "Next Debian version available: $NEXT_CODENAME (Debian $NEXT_VERSION)"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to backup important configurations
|
||||
backup_configs() {
|
||||
log "Creating backup of important configurations in $BACKUP_DIR..."
|
||||
mkdir -p "$BACKUP_DIR" || {
|
||||
log "${RED}Failed to create backup directory${NC}"
|
||||
return 1
|
||||
}
|
||||
|
||||
# Backup APT sources
|
||||
cp -a /etc/apt/sources.list "$BACKUP_DIR/" || {
|
||||
log "${RED}Failed to backup /etc/apt/sources.list${NC}"
|
||||
return 1
|
||||
}
|
||||
cp -a /etc/apt/sources.list.d/ "$BACKUP_DIR/" 2>/dev/null || true
|
||||
|
||||
# Backup installed packages
|
||||
dpkg --get-selections > "$BACKUP_DIR/installed_packages.txt" || {
|
||||
log "${RED}Failed to backup installed packages${NC}"
|
||||
return 1
|
||||
}
|
||||
|
||||
# Backup important system configs
|
||||
cp -a /etc/network/interfaces "$BACKUP_DIR/" 2>/dev/null || true
|
||||
cp -a /etc/resolv.conf "$BACKUP_DIR/" 2>/dev/null || true
|
||||
cp -a /etc/ssh/sshd_config "$BACKUP_DIR/" 2>/dev/null || true
|
||||
|
||||
log "${GREEN}Backup completed successfully${NC}"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to update APT repositories
|
||||
update_repositories() {
|
||||
log "Updating APT repositories to $NEXT_CODENAME..."
|
||||
sed -i "s/$CURRENT_CODENAME/$NEXT_CODENAME/g" /etc/apt/sources.list || {
|
||||
log "${RED}Failed to update /etc/apt/sources.list${NC}"
|
||||
return 1
|
||||
}
|
||||
|
||||
if [ -d /etc/apt/sources.list.d/ ]; then
|
||||
find /etc/apt/sources.list.d/ -type f -name "*.list" -exec sed -i "s/$CURRENT_CODENAME/$NEXT_CODENAME/g" {} \; || {
|
||||
log "${RED}Failed to update repositories in /etc/apt/sources.list.d/${NC}"
|
||||
return 1
|
||||
}
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to perform the upgrade
|
||||
perform_upgrade() {
|
||||
log "Updating package lists..."
|
||||
if ! apt update; then
|
||||
log "${RED}Failed to update package lists${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
log "Performing full upgrade..."
|
||||
if ! apt full-upgrade -y; then
|
||||
log "${RED}Failed to perform full upgrade${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
log "Performing distribution upgrade to $NEXT_CODENAME..."
|
||||
if ! apt dist-upgrade -y; then
|
||||
log "${RED}Failed to perform distribution upgrade${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to clean up after upgrade
|
||||
cleanup() {
|
||||
log "Cleaning up..."
|
||||
apt autoremove -y || log "${YELLOW}Warning: Failed to autoremove packages${NC}"
|
||||
apt clean || log "${YELLOW}Warning: Failed to clean APT cache${NC}"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to verify upgrade
|
||||
verify_upgrade() {
|
||||
NEW_CODENAME=$(grep -oP '(?<=VERSION_CODENAME=).*' /etc/os-release)
|
||||
if [ "$NEW_CODENAME" != "$NEXT_CODENAME" ]; then
|
||||
log "${RED}Upgrade verification failed. Current codename is still $NEW_CODENAME${NC}"
|
||||
return 1
|
||||
fi
|
||||
log "${GREEN}Successfully upgraded to $NEXT_CODENAME (Debian $NEXT_VERSION)${NC}"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Main function
|
||||
main() {
|
||||
# Check if running as root
|
||||
if [ "$(id -u)" -ne 0 ]; then
|
||||
log "${RED}This script must be run as root. Use 'sudo'.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if this is a Debian system
|
||||
if [ ! -f /etc/debian_version ]; then
|
||||
log "${RED}This script is only for Debian-based systems.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if running on Raspberry Pi
|
||||
if is_raspberry_pi; then
|
||||
log "${RED}WARNING: This is a Raspberry Pi device.${NC}"
|
||||
log "${RED}Upgrading the Debian distribution may break compatibility with Raspberry Pi OS.${NC}"
|
||||
if ! ask_confirmation "Are you sure you want to continue?" "n"; then
|
||||
log "${YELLOW}Aborting Debian distribution upgrade${NC}"
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# Get current and next Debian versions
|
||||
get_current_version
|
||||
if ! get_next_version; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if upgrade is needed
|
||||
if [ "$CURRENT_CODENAME" == "$NEXT_CODENAME" ]; then
|
||||
log "No new Debian stable version available. Current version: $CURRENT_CODENAME"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Ask for confirmation
|
||||
if ! ask_confirmation "Do you want to upgrade from $CURRENT_CODENAME to $NEXT_CODENAME?" "n"; then
|
||||
log "${YELLOW}Skipping Debian distribution upgrade${NC}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Backup configurations
|
||||
if ! backup_configs; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Update repositories
|
||||
if ! update_repositories; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Perform upgrade
|
||||
if ! perform_upgrade; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Clean up
|
||||
cleanup
|
||||
|
||||
# Verify upgrade
|
||||
if ! verify_upgrade; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log "${YELLOW}Please reboot the system to complete the upgrade${NC}"
|
||||
exit 0
|
||||
}
|
||||
|
||||
# Call main function
|
||||
main
|
||||
Vendored
+435
@@ -0,0 +1,435 @@
|
||||
#!/bin/bash
|
||||
# =============================================
|
||||
# Welcome Script for Raspberry Pi/Debian Login
|
||||
# Displays comprehensive system information in an appealing format
|
||||
# =============================================
|
||||
|
||||
# Function to print separators
|
||||
print_separator() {
|
||||
printf "%*s\n" "${COLUMNS:-$(tput cols)}" '' | tr ' ' '='
|
||||
}
|
||||
|
||||
# Function to print section titles
|
||||
print_title() {
|
||||
echo -e "\n\033[1;34m$1\033[0m" # Bold blue
|
||||
print_separator
|
||||
}
|
||||
|
||||
# Function to print labeled values
|
||||
print_value() {
|
||||
printf "\033[1;32m%-25s\033[0m %s\n" "$1:" "$2" # Bold green for label
|
||||
}
|
||||
|
||||
# Function to detect system type
|
||||
detect_system_type() {
|
||||
local model=""
|
||||
local system_type=""
|
||||
|
||||
# Check for Raspberry Pi
|
||||
if [ -f /proc/device-tree/model ]; then
|
||||
model=$(tr -d '\0' < /proc/device-tree/model)
|
||||
if [[ "$model" == *"Raspberry Pi"* ]]; then
|
||||
system_type="Raspberry Pi - $model"
|
||||
else
|
||||
system_type="ARM Device - $model"
|
||||
fi
|
||||
# Check for other common SBCs
|
||||
elif grep -q "Orange Pi" /proc/cpuinfo 2>/dev/null; then
|
||||
system_type="Orange Pi"
|
||||
elif grep -q "Banana Pi" /proc/cpuinfo 2>/dev/null; then
|
||||
system_type="Banana Pi"
|
||||
elif grep -q "Odroid" /proc/cpuinfo 2>/dev/null; then
|
||||
system_type="Odroid"
|
||||
# Check for x86/x86_64 systems
|
||||
elif grep -q "x86" /proc/cpuinfo 2>/dev/null; then
|
||||
if grep -q "64" /proc/cpuinfo 2>/dev/null; then
|
||||
system_type="x86_64 System"
|
||||
else
|
||||
system_type="x86 System"
|
||||
fi
|
||||
else
|
||||
system_type="Unknown System"
|
||||
fi
|
||||
|
||||
echo "$system_type"
|
||||
}
|
||||
|
||||
# Function to get processor info
|
||||
get_processor_info() {
|
||||
local processor=""
|
||||
local cores=""
|
||||
local architecture=""
|
||||
|
||||
# Get processor model
|
||||
if [ -f /proc/cpuinfo ]; then
|
||||
processor=$(grep -m 1 "model name" /proc/cpuinfo | cut -d':' -f2 | sed 's/^ *//')
|
||||
if [ -z "$processor" ]; then
|
||||
processor=$(grep -m 1 "Processor" /proc/cpuinfo | cut -d':' -f2 | sed 's/^ *//')
|
||||
fi
|
||||
if [ -z "$processor" ]; then
|
||||
processor=$(grep -m 1 "Hardware" /proc/cpuinfo | cut -d':' -f2 | sed 's/^ *//')
|
||||
fi
|
||||
fi
|
||||
|
||||
# Get number of cores
|
||||
cores=$(grep -c "^processor" /proc/cpuinfo 2>/dev/null)
|
||||
if [ -z "$cores" ] || [ "$cores" = "0" ]; then
|
||||
cores=$(nproc 2>/dev/null)
|
||||
fi
|
||||
|
||||
# Get architecture
|
||||
architecture=$(uname -m 2>/dev/null)
|
||||
|
||||
# Format output
|
||||
if [ -n "$processor" ]; then
|
||||
echo "$processor ($cores cores, $architecture)"
|
||||
else
|
||||
echo "Unknown ($cores cores, $architecture)"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to get OS info
|
||||
get_os_info() {
|
||||
local os_name=""
|
||||
local os_version=""
|
||||
local kernel_version=""
|
||||
|
||||
# Get OS name and version
|
||||
if [ -f /etc/os-release ]; then
|
||||
os_name=$(grep "^PRETTY_NAME=" /etc/os-release | cut -d'"' -f2)
|
||||
os_version=$(grep "^VERSION=" /etc/os-release | cut -d'"' -f2)
|
||||
elif [ -f /etc/debian_version ]; then
|
||||
os_name="Debian"
|
||||
os_version=$(cat /etc/debian_version)
|
||||
fi
|
||||
|
||||
# Get kernel version
|
||||
kernel_version=$(uname -r)
|
||||
|
||||
# Format output
|
||||
if [ -n "$os_name" ]; then
|
||||
if [ -n "$os_version" ]; then
|
||||
echo "$os_name $os_version (Kernel: $kernel_version)"
|
||||
else
|
||||
echo "$os_name (Kernel: $kernel_version)"
|
||||
fi
|
||||
else
|
||||
echo "Unknown OS (Kernel: $kernel_version)"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to get GPU info (Raspberry Pi specific)
|
||||
get_gpu_info() {
|
||||
local gpu_mem=""
|
||||
local gpu_model=""
|
||||
|
||||
# Check if vcgencmd is available (Raspberry Pi)
|
||||
if command -v vcgencmd >/dev/null 2>&1; then
|
||||
gpu_mem=$(vcgencmd get_mem gpu | cut -d'=' -f2)
|
||||
gpu_model="VideoCore IV"
|
||||
else
|
||||
gpu_mem="Not available"
|
||||
# Try to get GPU info from lspci
|
||||
if command -v lspci >/dev/null 2>&1; then
|
||||
gpu_model=$(lspci | grep -i vga | cut -d':' -f3 | sed 's/^ *//')
|
||||
if [ -z "$gpu_model" ]; then
|
||||
gpu_model="Unknown GPU"
|
||||
fi
|
||||
else
|
||||
gpu_model="Unknown GPU"
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "$gpu_model ($gpu_mem)"
|
||||
}
|
||||
|
||||
# Function to get system load
|
||||
get_system_load() {
|
||||
local load_avg=""
|
||||
local uptime_info=""
|
||||
|
||||
load_avg=$(uptime | awk -F'load average: ' '{print $2}')
|
||||
uptime_info=$(uptime -p)
|
||||
|
||||
echo "Load: $load_avg | Uptime: $uptime_info"
|
||||
}
|
||||
|
||||
# Function to get network connections
|
||||
get_network_connections() {
|
||||
local connections=""
|
||||
|
||||
if command -v ss >/dev/null 2>&1; then
|
||||
connections=$(ss -s | grep "TCP:" | awk '{print $2 " connections"}')
|
||||
elif command -v netstat >/dev/null 2>&1; then
|
||||
connections=$(netstat -tun | grep -c ESTABLISHED)
|
||||
connections="$connections active connections"
|
||||
else
|
||||
connections="Not available"
|
||||
fi
|
||||
|
||||
echo "$connections"
|
||||
}
|
||||
|
||||
# =============================================
|
||||
# System Information
|
||||
# =============================================
|
||||
|
||||
# Welcome header
|
||||
clear
|
||||
print_separator
|
||||
echo -e "\033[1;36m" # Bold cyan
|
||||
figlet -f small "Welcome to $(hostname)"
|
||||
echo -e "\033[0m"
|
||||
print_separator
|
||||
|
||||
# System Overview
|
||||
print_title "System Overview"
|
||||
print_value "System Type" "$(detect_system_type)"
|
||||
print_value "Processor" "$(get_processor_info)"
|
||||
print_value "Operating System" "$(get_os_info)"
|
||||
print_value "GPU" "$(get_gpu_info)"
|
||||
print_value "System Load" "$(get_system_load)"
|
||||
|
||||
# Current date and time
|
||||
print_title "General Information"
|
||||
print_value "Date/Time" "$(date '+%Y-%m-%d %H:%M:%S')"
|
||||
print_value "User" "$(whoami)"
|
||||
print_value "Hostname" "$(hostname)"
|
||||
print_value "IP Address" "$(hostname -I | awk '{print $1}')"
|
||||
print_value "Active Network Connections" "$(get_network_connections)"
|
||||
|
||||
# =============================================
|
||||
# CPU Information
|
||||
# =============================================
|
||||
print_title "CPU Status"
|
||||
|
||||
# CPU Temperature
|
||||
temp=$(vcgencmd measure_temp 2>/dev/null | awk -F'=' '{print $2}' | awk -F"'" '{print $1}')
|
||||
if [ -n "$temp" ]; then
|
||||
print_value "Temperature" "$temp°C"
|
||||
else
|
||||
# Try alternative method for non-RPi systems
|
||||
if [ -f /sys/class/thermal/thermal_zone0/temp ]; then
|
||||
temp=$(cat /sys/class/thermal/thermal_zone0/temp)
|
||||
temp=$(echo "scale=1; $temp/1000" | bc 2>/dev/null)
|
||||
print_value "Temperature" "${temp}°C"
|
||||
else
|
||||
print_value "Temperature" "Not available"
|
||||
fi
|
||||
fi
|
||||
|
||||
# CPU Frequencies
|
||||
arm_freq=$(vcgencmd measure_clock arm 2>/dev/null | awk -F'=' '{print $2}')
|
||||
core_freq=$(vcgencmd measure_clock core 2>/dev/null | awk -F'=' '{print $2}')
|
||||
if [ -n "$arm_freq" ]; then
|
||||
print_value "ARM Frequency" "$(echo "scale=2; $arm_freq/1000000" | bc) MHz"
|
||||
fi
|
||||
if [ -n "$core_freq" ]; then
|
||||
print_value "Core Frequency" "$(echo "scale=2; $core_freq/1000000" | bc) MHz"
|
||||
fi
|
||||
|
||||
# CPU Usage
|
||||
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
|
||||
print_value "CPU Usage" "$(printf "%.1f" $cpu_usage)%"
|
||||
|
||||
# =============================================
|
||||
# Memory Information
|
||||
# =============================================
|
||||
print_title "Memory"
|
||||
|
||||
# Total and used memory
|
||||
total_mem=$(free -m | awk '/Mem:/ {print $2}')
|
||||
used_mem=$(free -m | awk '/Mem:/ {print $3}')
|
||||
mem_percent=$(echo "scale=1; $used_mem/$total_mem*100" | bc)
|
||||
print_value "Total Memory" "$total_mem MB"
|
||||
print_value "Used Memory" "$used_mem MB ($mem_percent%)"
|
||||
|
||||
# Swap information
|
||||
total_swap=$(free -m | awk '/Swap:/ {print $2}')
|
||||
used_swap=$(free -m | awk '/Swap:/ {print $3}')
|
||||
if [ "$total_swap" -gt 0 ]; then
|
||||
swap_percent=$(echo "scale=1; $used_swap/$total_swap*100" | bc)
|
||||
print_value "Total Swap" "$total_swap MB"
|
||||
print_value "Used Swap" "$used_swap MB ($swap_percent%)"
|
||||
else
|
||||
print_value "Swap" "Not enabled"
|
||||
fi
|
||||
|
||||
# =============================================
|
||||
# Storage Information
|
||||
# =============================================
|
||||
print_title "Storage"
|
||||
|
||||
# Disk space
|
||||
root_usage=$(df -h / | awk 'NR==2 {print $5}')
|
||||
root_size=$(df -h / | awk 'NR==2 {print $2}')
|
||||
root_avail=$(df -h / | awk 'NR==2 {print $4}')
|
||||
print_value "Root Partition" "$root_size total, $root_usage used, $root_avail available"
|
||||
|
||||
# Boot partition space (if exists)
|
||||
if [ -d "/boot" ]; then
|
||||
boot_usage=$(df -h /boot | awk 'NR==2 {print $5}')
|
||||
boot_size=$(df -h /boot | awk 'NR==2 {print $2}')
|
||||
boot_avail=$(df -h /boot | awk 'NR==2 {print $4}')
|
||||
print_value "Boot Partition" "$boot_size total, $boot_usage used, $boot_avail available"
|
||||
fi
|
||||
|
||||
# Disk I/O (if available)
|
||||
if command -v iostat >/dev/null 2>&1; then
|
||||
disk_io=$(iostat -d -x 1 1 | grep -A1 "Device" | tail -n +2)
|
||||
if [ -n "$disk_io" ]; then
|
||||
print_value "Disk I/O" "\n$disk_io"
|
||||
fi
|
||||
fi
|
||||
|
||||
# =============================================
|
||||
# Network Information
|
||||
# =============================================
|
||||
print_title "Network"
|
||||
|
||||
# Network interfaces
|
||||
interfaces=$(ip -o link show | awk -F': ' '{print $2}' | tr '\n' ' ')
|
||||
print_value "Interfaces" "$interfaces"
|
||||
|
||||
# IP addresses for each interface
|
||||
for iface in $(ip -o link show | awk -F': ' '{print $2}'); do
|
||||
ip_addr=$(ip -4 addr show $iface 2>/dev/null | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | tr '\n' ' ')
|
||||
if [ -n "$ip_addr" ]; then
|
||||
print_value " $iface IP" "$ip_addr"
|
||||
fi
|
||||
done
|
||||
|
||||
# Network speed (if available)
|
||||
if command -v ethtool >/dev/null 2>&1; then
|
||||
for iface in eth0 enp0s3 enx*; do
|
||||
if ip link show $iface >/dev/null 2>&1; then
|
||||
eth_speed=$(ethtool $iface 2>/dev/null | grep -i speed | awk '{print $2}')
|
||||
if [ -n "$eth_speed" ]; then
|
||||
print_value " $iface Speed" "$eth_speed"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
# Network connections
|
||||
print_value "Active Connections" "$(get_network_connections)"
|
||||
|
||||
# =============================================
|
||||
# Additional System Information
|
||||
# =============================================
|
||||
print_title "Additional Information"
|
||||
|
||||
# Last login - with fallback if 'last' command is not available
|
||||
if command -v last >/dev/null 2>&1; then
|
||||
last_login=$(last -n 1 $(whoami) 2>/dev/null | head -n 1 | awk '{print $1, $2, $3, $4, $5, $6, $7}')
|
||||
if [ -z "$last_login" ]; then
|
||||
last_login="No login history found"
|
||||
fi
|
||||
else
|
||||
last_login="Command 'last' not available"
|
||||
fi
|
||||
print_value "Last Login" "$last_login"
|
||||
|
||||
# Users currently logged in
|
||||
if command -v who >/dev/null 2>&1; then
|
||||
current_users=$(who | awk '{print $1}' | sort | uniq | paste -sd ",")
|
||||
if [ -z "$current_users" ]; then
|
||||
current_users="No users logged in"
|
||||
fi
|
||||
else
|
||||
current_users="Command 'who' not available"
|
||||
fi
|
||||
print_value "Logged in Users" "$current_users"
|
||||
|
||||
# System uptime in different formats
|
||||
if command -v uptime >/dev/null 2>&1; then
|
||||
uptime_pretty=$(uptime -p)
|
||||
uptime_since=$(uptime -s)
|
||||
print_value "Uptime" "$uptime_pretty (since $uptime_since)"
|
||||
else
|
||||
print_value "Uptime" "Command 'uptime' not available"
|
||||
fi
|
||||
|
||||
# Running services (top 5 by memory)
|
||||
print_title "Running Services"
|
||||
if command -v systemctl >/dev/null 2>&1; then
|
||||
echo -e "\033[1;32m UNIT LOAD ACTIVE SUB DESCRIPTION\033[0m"
|
||||
echo -e " ----------------------------------------------------------------"
|
||||
systemctl list-units --type=service --state=running --no-pager | head -n 6 | tail -n +2 | while read -r line; do
|
||||
unit=$(echo "$line" | awk '{print $1}')
|
||||
load=$(echo "$line" | awk '{print $2}')
|
||||
active=$(echo "$line" | awk '{print $3}')
|
||||
sub=$(echo "$line" | awk '{print $4}')
|
||||
desc=$(echo "$line" | awk '{for(i=5;i<=NF;i++) printf $i" "; print ""}')
|
||||
|
||||
# Truncate long descriptions
|
||||
if [ ${#desc} -gt 40 ]; then
|
||||
desc="${desc:0:37}..."
|
||||
fi
|
||||
|
||||
printf " \033[1;37m%-23s\033[0m \033[1;35m%-6s\033[0m \033[1;36m%-6s\033[0m \033[1;34m%-6s\033[0m %s\n" "$unit" "$load" "$active" "$sub" "$desc"
|
||||
done
|
||||
else
|
||||
print_value "Running Services" "systemctl not available"
|
||||
fi
|
||||
|
||||
# # Scheduled tasks (cron jobs)
|
||||
# print_title "Scheduled Tasks"
|
||||
# if command -v crontab >/dev/null 2>&1; then
|
||||
# if crontab -l 2>/dev/null | grep -q .; then
|
||||
# echo -e "\033[1;32m User Crontab:\033[0m"
|
||||
# crontab -l 2>/dev/null | sed 's/^/ /'
|
||||
# else
|
||||
# echo -e " \033[1;33mNo user crontab entries\033[0m"
|
||||
# fi
|
||||
|
||||
# if [ -d /etc/cron.d ] || [ -f /etc/crontab ]; then
|
||||
# echo -e "\n\033[1;32m System Crontab:\033[0m"
|
||||
# if [ -f /etc/crontab ]; then
|
||||
# cat /etc/crontab 2>/dev/null | grep -v "^#" | grep -v "^$" | sed 's/^/ /'
|
||||
# fi
|
||||
|
||||
# if [ -d /etc/cron.d ]; then
|
||||
# for cronfile in /etc/cron.d/*; do
|
||||
# if [ -f "$cronfile" ]; then
|
||||
# echo -e "\n\033[1;32m $cronfile:\033[0m"
|
||||
# cat "$cronfile" 2>/dev/null | grep -v "^#" | grep -v "^$" | sed 's/^/ /'
|
||||
# fi
|
||||
# done
|
||||
# fi
|
||||
# else
|
||||
# echo -e " \033[1;33mNo system crontab entries\033[0m"
|
||||
# fi
|
||||
# else
|
||||
# print_value "Scheduled Tasks" "crontab command not available"
|
||||
# fi
|
||||
|
||||
# # Systemd timers (if available)
|
||||
# if command -v systemctl >/dev/null 2>&1; then
|
||||
# if systemctl list-timers --no-pager 2>/dev/null | grep -q .; then
|
||||
# echo -e "\n\033[1;32m Systemd Timers:\033[0m"
|
||||
# systemctl list-timers --no-pager | sed 's/^/ /'
|
||||
# fi
|
||||
# fi
|
||||
|
||||
# Open files and network connections
|
||||
print_title "Open Files and Network Connections"
|
||||
if command -v lsof >/dev/null 2>&1; then
|
||||
echo -e "\033[1;32m Top 5 processes with most open files:\033[0m"
|
||||
lsof -n | awk '{print $1}' | sort | uniq -c | sort -nr | head -n 5 | sed 's/^/ /'
|
||||
|
||||
echo -e "\n\033[1;32m Network connections by type:\033[0m"
|
||||
echo -e " \033[1;36mTCP:\033[0m $(lsof -i TCP | wc -l)"
|
||||
echo -e " \033[1;36mUDP:\033[0m $(lsof -i UDP | wc -l)"
|
||||
echo -e " \033[1;36mTotal:\033[0m $(lsof -i | wc -l)"
|
||||
else
|
||||
print_value "Open Files" "lsof command not available"
|
||||
fi
|
||||
|
||||
# =============================================
|
||||
# Final Message
|
||||
# =============================================
|
||||
print_separator
|
||||
echo -e "\033[1;33mSystem ready! $(date '+%H:%M')\033[0m" # Bold yellow
|
||||
print_separator
|
||||
echo
|
||||
Reference in New Issue
Block a user