Skip to content

Commit

Permalink
Merge pull request #37 from felipealfonsog/development
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
felipealfonsog authored Jul 23, 2024
2 parents f5e93ac + 6a89cf6 commit 14e76fc
Show file tree
Hide file tree
Showing 3 changed files with 408 additions and 60 deletions.
222 changes: 164 additions & 58 deletions CLI/Connmaster-CLI.sh
Original file line number Diff line number Diff line change
@@ -1,112 +1,217 @@
#!/bin/bash

# Configuration file to store networks and passwords
CONFIG_FILE="$HOME/.connman_networks"

# Function to check if ConnMan is running and start it if not
check_connman() {
if ! pgrep -x "connmand" > /dev/null; then
echo "ConnMan is not running. Starting ConnMan..."
sudo systemctl start connman
if ! pgrep -x "connmand" > /dev/null; then
echo "Failed to start ConnMan. Exiting..."
exit 1
fi
fi
}

# Function to scan and list WiFi networks
function scan_networks() {
scan_networks() {
echo "Scanning WiFi networks..."
connmanctl scan wifi
connmanctl services

local raw_networks
IFS=$'\n' read -r -d '' -a raw_networks < <(connmanctl services | grep wifi && printf '\0')

echo "Available networks:"
> "$CONFIG_FILE" # Clear the configuration file only if it is empty

for raw_network in "${raw_networks[@]}"; do
ssid=$(echo "$raw_network" | awk -F' - ' '{print $1}' | xargs) # Extract SSID
network_id=$(echo "$raw_network" | awk -F' - ' '{print $2}' | xargs) # Extract Network ID

# Ensure SSID and Network ID are not empty
if [ -z "$ssid" ]; then
ssid="Unknown SSID"
fi
if [ -z "$network_id" ]; then
network_id="Unknown Network ID"
fi

# Print the result formatted
printf "%-40s - %s\n" "$ssid" "$network_id"

# Save network ID to configuration file only if it's not already saved
if ! grep -q "^$network_id," "$CONFIG_FILE"; then
echo "$network_id," >> "$CONFIG_FILE"
fi
done
}

# Function to check if networks have been scanned
check_scanned() {
if [ ! -s "$CONFIG_FILE" ]; then
echo "You need to scan networks first to get the network IDs."
return 1
else
return 0
fi
}

# Function to connect to a WiFi network
function connect_network() {
echo "Enter the WiFi network name (SSID):"
read ssid
connect_network() {
check_scanned || return

echo "Enter the network ID of the WiFi network you want to connect to:"
read -r network_id

if [ -z "$network_id" ]; then
echo "Invalid network ID."
return
fi

echo "Enter the password (leave blank if none):"
read -s password

network_id=$(connmanctl services | grep "$ssid" | awk '{print $3}')

if [ -z "$password" ]; then
connmanctl connect "$network_id"
else
connmanctl connect "$network_id"
connmanctl config "$network_id" --passphrase "$password"
connmanctl tether wifi "$network_id" "$password"
fi

echo "Do you want to set the network to autoconnect? (y/n):"
read autoconnect

if [ "$autoconnect" = "y" ]; then
connmanctl config "$network_id" --autoconnect yes
# Check if the operation was successful
if [ $? -eq 0 ]; then
echo "Operation successful."
# Save network ID and password to configuration file
sed -i "/^$network_id,/ s/,$//" "$CONFIG_FILE"
echo "$network_id,$password" >> "$CONFIG_FILE"
else
connmanctl config "$network_id" --autoconnect no
echo "Operation failed."
fi

# Save the configuration to the file
echo "$network_id,$password,$autoconnect" >> "$CONFIG_FILE"
}

# Function to disconnect from a WiFi network
function disconnect_network() {
echo "Enter the name of the WiFi network (SSID) you want to disconnect:"
read ssid
network_id=$(connmanctl services | grep "$ssid" | awk '{print $3}')
disconnect_network() {
check_scanned || return

echo "Enter the network ID of the WiFi network you want to disconnect from:"
read -r network_id

if [ -z "$network_id" ]; then
echo "Invalid network ID."
return
fi

# Print the command being executed
echo "Executing command: connmanctl disconnect \"$network_id\""

connmanctl disconnect "$network_id"

# Check if the operation was successful
if [ $? -eq 0 ]; then
echo "Operation successful."
else
echo "Operation failed."
fi
}

# Function to configure autoconnect
function configure_autoconnect() {
echo "Enter the WiFi network name (SSID):"
read ssid
network_id=$(connmanctl services | grep "$ssid" | awk '{print $3}')
# Function to enable or disable autoconnect
configure_autoconnect() {
check_scanned || return

echo "Enter the network ID of the WiFi network you want to configure:"
read -r network_id

if [ -z "$network_id" ]; then
echo "Invalid network ID."
return
fi

echo "Do you want to set the network to autoconnect? (y/n):"
read autoconnect
read -r autoconnect

if [ "$autoconnect" = "y" ]; then
connmanctl config "$network_id" --autoconnect yes
command="connmanctl config $network_id --autoconnect yes"
else
connmanctl config "$network_id" --autoconnect no
command="connmanctl config $network_id --autoconnect no"
fi

# Update the configuration in the file
sed -i "/^$network_id,/d" "$CONFIG_FILE"
echo "$network_id,,$autoconnect" >> "$CONFIG_FILE"
}
# Print the command being executed
echo "Executing command: $command"

# Function to remove autoconnect configuration from a specific network
function remove_autoconnect() {
echo "Enter the WiFi network name (SSID) to remove autoconnect:"
read ssid
network_id=$(connmanctl services | grep "$ssid" | awk '{print $3}')
connmanctl config "$network_id" --autoconnect no
# Execute the command
eval $command

# Update the configuration in the file
sed -i "/^$network_id,/d" "$CONFIG_FILE"
echo "$network_id,," >> "$CONFIG_FILE"
# Check if the operation was successful
if [ $? -eq 0 ]; then
echo "Operation successful."
else
echo "Operation failed."
fi
}

# Function to connect to a saved network with priority to autoconnect
connect_saved_network() {
echo "Attempting to connect to saved network..."

# Check each network ID in the configuration file
while IFS=, read -r network_id password; do
if [ -z "$network_id" ]; then
continue
fi

echo "Attempting to connect to saved network: $network_id"

if [ -z "$password" ]; then
connmanctl connect "$network_id"
else
connmanctl tether wifi "$network_id" "$password"
fi

# Check if connection was successful
if [ $? -eq 0 ]; then
echo "Connected to $network_id"
return
else
echo "Failed to connect to $network_id."
fi
done < "$CONFIG_FILE"

echo "No saved networks available or all attempts failed."
}

# Function to display credits
function display_credits() {
echo "****************************************"
echo "ConnWifiMaster - ConnMan Network Manager"
echo "----------------------------------------"
display_credits() {
echo "***************************************************"
echo "ConnWifiMaster CLI - ConnMan Network Manager"
echo "___________________________________________________"
echo "By Computer Science Engineer: "
echo "Felipe Alfonso González"
echo "f.alfonso@res-ear.ch - github.com/felipealfonsog"
echo "****************************************"
echo "***************************************************"
}

# Create config file if it doesn't exist
# Create configuration file if it doesn't exist
if [ ! -f "$CONFIG_FILE" ]; then
touch "$CONFIG_FILE"
fi

# Check if ConnMan is active
check_connman

# Main menu
while true; do
echo "----------------------------------------"
echo "ConnMan CLI - Network Manager"
echo "----------------------------------------"
echo "1. Scan WiFi networks"
echo "---------------------------------------------------"
echo "ConnWifiMaster CLI - ConnMan Network Manager"
echo "___________________________________________________"
echo "1. Scan/List WiFi networks"
echo "2. Connect to a WiFi network"
echo "3. Disconnect from a WiFi network"
echo "4. Configure autoconnect for a WiFi network"
echo "5. Remove autoconnect configuration from a WiFi network"
echo "6. Credits"
echo "4. Enable or Disable autoconnect for a WiFi network"
echo "5. Connect to a saved Wifi network"
echo "6. Credits by Engineer"
echo "7. Exit"
echo "----------------------------------------"
echo "---------------------------------------------------"
read -p "Choose an option: " option

case $option in
Expand All @@ -123,7 +228,7 @@ while true; do
configure_autoconnect
;;
5)
remove_autoconnect
connect_saved_network
;;
6)
display_credits
Expand All @@ -136,3 +241,4 @@ while true; do
;;
esac
done

5 changes: 3 additions & 2 deletions CLI/v.0.0.1.sh → CLI/v.0.0.4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ function remove_autoconnect() {

# Function to display credits
function display_credits() {
echo "----------------------------------------"
echo "****************************************"
echo "ConnWifiMaster - ConnMan Network Manager"
echo "----------------------------------------"
echo "By Computer Science Engineer: "
echo "Felipe Alfonso González"
echo "f.alfonso@res-ear.ch - github.com/felipealfonsog"
echo "----------------------------------------"
echo "****************************************"
}

# Create config file if it doesn't exist
Expand All @@ -98,6 +98,7 @@ fi
while true; do
echo "----------------------------------------"
echo "ConnMan CLI - Network Manager"
echo "----------------------------------------"
echo "1. Scan WiFi networks"
echo "2. Connect to a WiFi network"
echo "3. Disconnect from a WiFi network"
Expand Down
Loading

0 comments on commit 14e76fc

Please sign in to comment.