Unverified Commit 167e0dc2 authored by Igor Pečovnik's avatar Igor Pečovnik Committed by GitHub
Browse files

Merge pull request #57 from armbian/development

Development
parents 2119a82d 5b3f2aa6
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
# check_ht_capab # check_ht_capab
# check_vht_capab # check_vht_capab
# check_channels # check_channels
# netmask_to_cidr
# nm_ip_editor # nm_ip_editor
# systemd_ip_editor # systemd_ip_editor
# ip_editor # ip_editor
...@@ -330,6 +331,19 @@ function check_channels () ...@@ -330,6 +331,19 @@ function check_channels ()
} }
#
# convert netmask to CIDR
#
function netmask_to_cidr ()
{
IFS=' '
local bits=0
for octet in $(echo $1| sed 's/\./ /g'); do
binbits=$(echo "obase=2; ibase=10; ${octet}"| bc | sed 's/0//g')
let bits+=${#binbits}
done
echo "${bits}"
}
# #
...@@ -348,12 +362,26 @@ exec 3>&1 ...@@ -348,12 +362,26 @@ exec 3>&1
read -r address;read -r netmask;read -r gateway read -r address;read -r netmask;read -r gateway
if [[ $? = 0 ]]; then if [[ $? = 0 ]]; then
localuuid=$(LC_ALL=C nmcli -f UUID,DEVICE connection show | grep $1 | awk '{print $1}') localuuid=$(LC_ALL=C nmcli -f UUID,DEVICE connection show | grep $1 | awk '{print $1}')
nmcli con mod $localuuid ipv4.method manual ipv4.addresses "$address/$netmask" >/dev/null 2>&1 # convert netmask value to CIDR if required
if [[ $netmask =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
CIDR=$(netmask_to_cidr ${netmask})
else
CIDR=${netmask}
fi
if [[ -n "$localuuid" ]]; then
# adjust existing
nmcli con mod $localuuid ipv4.method manual ipv4.addresses "$address/$CIDR" >/dev/null 2>&1
nmcli con mod $localuuid ipv4.method manual ipv4.gateway "$gateway" >/dev/null 2>&1 nmcli con mod $localuuid ipv4.method manual ipv4.gateway "$gateway" >/dev/null 2>&1
nmcli con mod $localuuid ipv4.dns "8.8.8.8,$gateway" >/dev/null 2>&1 nmcli con mod $localuuid ipv4.dns "8.8.8.8,$gateway" >/dev/null 2>&1
nmcli con down $localuuid >/dev/null 2>&1 nmcli con down $localuuid >/dev/null 2>&1
sleep 2 sleep 2
nmcli con up $localuuid >/dev/null 2>&1 nmcli con up $localuuid >/dev/null 2>&1
else
# create new
nmcli con add con-name "armbian" ifname "$1" type 802-3-ethernet ip4 "$address/$CIDR" gw4 "$gateway" >/dev/null 2>&1
nmcli con mod "armbian" ipv4.dns "8.8.8.8,$gateway" >/dev/null 2>&1
nmcli con up "armbian" >/dev/null 2>&1
fi
fi fi
} }
} }
...@@ -453,7 +481,7 @@ function wlan_edit () ...@@ -453,7 +481,7 @@ function wlan_edit ()
{ {
# select default interfaces if there is more than one # select default interfaces if there is more than one
select_default_interface select_interface "default"
dialog --title " Configuration edit " --colors --backtitle "$BACKTITLE" --help-button --help-label "Cancel" --yes-label "Basic" \ dialog --title " Configuration edit " --colors --backtitle "$BACKTITLE" --help-button --help-label "Cancel" --yes-label "Basic" \
--no-label "Advanced" --yesno "\n\Z1Basic:\Z0 Change SSID, password and channel\n\n\Z1Advanced:\Z0 Edit /etc/hostapd.conf file" 9 70 --no-label "Advanced" --yesno "\n\Z1Basic:\Z0 Change SSID, password and channel\n\n\Z1Advanced:\Z0 Edit /etc/hostapd.conf file" 9 70
if [[ $? = 0 ]]; then if [[ $? = 0 ]]; then
...@@ -543,19 +571,56 @@ function get_wlan_interface () ...@@ -543,19 +571,56 @@ function get_wlan_interface ()
function select_interface ()
{
IFS=$'\r\n'
GLOBIGNORE='*'
local ADAPTER=($(nmcli device status | grep ethernet | awk '{ print $1 }' | grep -v lo))
local LIST=()
for i in "${ADAPTER[@]}"
do
local IPADDR=$(LC_ALL=C ip -4 addr show dev ${i[0]} | awk '/inet/ {print $2}' | cut -d'/' -f1)
ADD_SPEED=""
[[ $i == eth* || $i == en* ]] && ADD_SPEED=$(ethtool $i | grep Speed)
[[ $i == wl* ]] && ADD_SPEED=$(LC_ALL=C nmcli -f RATE,DEVICE,ACTIVE dev wifi list | grep $i | grep yes | awk 'NF==4{print $1""$2}NF==1' | sed -e 's/^/ Speed: /' | tail -1)
LIST+=( "${i[0]//[[:blank:]]/}" "${IPADDR} ${ADD_SPEED}" )
done
LIST_LENGTH=$((${#LIST[@]}/2));
if [ "$LIST_LENGTH" -eq 0 ]; then
SELECTED_ADAPTER="lo"
elif [ "$LIST_LENGTH" -eq 1 ]; then
SELECTED_ADAPTER=${ADAPTER[0]}
else
exec 3>&1
SELECTED_ADAPTER=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse --title "Select $1 interface" --clear \
--menu "" $((6+${LIST_LENGTH})) 74 14 "${LIST[@]}" 2>&1 1>&3)
exec 3>&-
fi
}
# #
# select interface if there is more than one and adjust metric # select interface if there is more than one and adjust metric
# #
# $1 = default | all
#
function select_default_interface () function select_default_interface ()
{ {
ALREADY_DEFINED=$(cat /etc/iptables.ipv4.nat 2> /dev/null | grep "POSTROUTING -o" | tail -1 | awk '{ print $4 }') ALREADY_DEFINED=$(cat /etc/iptables.ipv4.nat 2> /dev/null | grep "POSTROUTING -o" | tail -1 | awk '{ print $4 }')
if [[ -n "${ALREADY_DEFINED}" ]]; then if [[ -n "${ALREADY_DEFINED}" ]]; then
DEFAULT_ADAPTER=${ALREADY_DEFINED} DEFAULT_ADAPTER=${ALREADY_DEFINED}
else else
IFS=$'\r\n' IFS=$'\r\n'
GLOBIGNORE='*' GLOBIGNORE='*'
if [[ $1 == "default" ]]; then
local ADAPTER=($(nmcli -t -f DEVICE connection show --active)) local ADAPTER=($(nmcli -t -f DEVICE connection show --active))
else
local ADAPTER=($(nmcli device status | tail -n +2 | awk '{ print $1 }' | grep -v lo))
fi
local LIST=() local LIST=()
for i in "${ADAPTER[@]}" for i in "${ADAPTER[@]}"
do do
...@@ -563,7 +628,11 @@ function select_default_interface () ...@@ -563,7 +628,11 @@ function select_default_interface ()
ADD_SPEED="" ADD_SPEED=""
[[ $i == eth* || $i == en* ]] && ADD_SPEED=$(ethtool $i | grep Speed) [[ $i == eth* || $i == en* ]] && ADD_SPEED=$(ethtool $i | grep Speed)
[[ $i == wl* ]] && ADD_SPEED=$(LC_ALL=C nmcli -f RATE,DEVICE,ACTIVE dev wifi list | grep $i | grep yes | awk 'NF==4{print $1""$2}NF==1' | sed -e 's/^/ Speed: /' | tail -1) [[ $i == wl* ]] && ADD_SPEED=$(LC_ALL=C nmcli -f RATE,DEVICE,ACTIVE dev wifi list | grep $i | grep yes | awk 'NF==4{print $1""$2}NF==1' | sed -e 's/^/ Speed: /' | tail -1)
if [[ $1 == "default" ]]; then
[[ $IPADDR != "172.24.1.1" && -n $IPADDR ]] && LIST+=( "${i[0]//[[:blank:]]/}" "${IPADDR} ${ADD_SPEED}" ) [[ $IPADDR != "172.24.1.1" && -n $IPADDR ]] && LIST+=( "${i[0]//[[:blank:]]/}" "${IPADDR} ${ADD_SPEED}" )
else
[[ $IPADDR != "172.24.1.1" ]] && LIST+=( "${i[0]//[[:blank:]]/}" "${IPADDR} ${ADD_SPEED}" )
fi
done done
LIST_LENGTH=$((${#LIST[@]}/2)); LIST_LENGTH=$((${#LIST[@]}/2));
if [ "$LIST_LENGTH" -eq 0 ]; then if [ "$LIST_LENGTH" -eq 0 ]; then
...@@ -573,7 +642,7 @@ function select_default_interface () ...@@ -573,7 +642,7 @@ function select_default_interface ()
else else
exec 3>&1 exec 3>&1
DEFAULT_ADAPTER=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \ DEFAULT_ADAPTER=$(dialog --nocancel --backtitle "$BACKTITLE" --no-collapse \
--title "Select default interface" --clear --menu "" $((6+${LIST_LENGTH})) 74 14 "${LIST[@]}" 2>&1 1>&3) --title "Select $1 interface" --clear --menu "" $((6+${LIST_LENGTH})) 74 14 "${LIST[@]}" 2>&1 1>&3)
exec 3>&- exec 3>&-
fi fi
fi fi
......
...@@ -263,6 +263,7 @@ function jobs () ...@@ -263,6 +263,7 @@ function jobs ()
# Select dynamic or edit static IP address # Select dynamic or edit static IP address
# #
"IP" ) "IP" )
select_interface
# check if we have systemd networking in action # check if we have systemd networking in action
SYSTEMDNET=$(service systemd-networkd status | grep -w active | grep -w running) SYSTEMDNET=$(service systemd-networkd status | grep -w active | grep -w running)
dialog --title " IP address assignment " --colors --backtitle "$BACKTITLE" --help-button --help-label "Cancel" \ dialog --title " IP address assignment " --colors --backtitle "$BACKTITLE" --help-button --help-label "Cancel" \
...@@ -273,33 +274,33 @@ function jobs () ...@@ -273,33 +274,33 @@ function jobs ()
# dynamic # dynamic
if [[ $exitstatus = 0 ]]; then if [[ $exitstatus = 0 ]]; then
if [[ -n $SYSTEMDNET ]]; then if [[ -n $SYSTEMDNET ]]; then
filename="/etc/systemd/network/10-${DEFAULT_ADAPTER}.network" filename="/etc/systemd/network/10-${SELECTED_ADAPTER}.network"
if [[ -f $filename ]]; then if [[ -f $filename ]]; then
sed -i '/Network/,$d' $filename sed -i '/Network/,$d' $filename
echo -e "[Network]" >>$filename echo -e "[Network]" >>$filename
echo -e "DHCP=ipv4" >>$filename echo -e "DHCP=ipv4" >>$filename
fi fi
else else
if [[ -n $(LC_ALL=C nmcli device status | grep $DEFAULT_ADAPTER | grep connected) ]]; then if [[ -n $(LC_ALL=C nmcli device status | grep $SELECTED_ADAPTER ) ]]; then
nmcli connection delete uuid $(LC_ALL=C nmcli -f UUID,DEVICE connection show | grep $DEFAULT_ADAPTER | awk '{print $1}') >/dev/null 2>&1 nmcli connection delete uuid $(LC_ALL=C nmcli -f UUID,DEVICE connection show | grep $SELECTED_ADAPTER | awk '{print $1}') >/dev/null 2>&1
nmcli con add con-name "Armbian ethernet" type ethernet ifname $DEFAULT_ADAPTER >/dev/null 2>&1 nmcli con add con-name "Armbian ethernet" type ethernet ifname $SELECTED_ADAPTER >/dev/null 2>&1
nmcli con up "Armbian ethernet" >/dev/null 2>&1 nmcli con up "Armbian ethernet" >/dev/null 2>&1
else else
create_if_config "$DEFAULT_ADAPTER" "$DEFAULT_ADAPTER" "dynamic" > /etc/network/interfaces create_if_config "$SELECTED_ADAPTER" "$SELECTED_ADAPTER" "dynamic" > /etc/network/interfaces
fi fi
fi fi
fi fi
# static # static
if [[ $exitstatus = 1 ]]; then if [[ $exitstatus = 1 ]]; then
create_if_config "$DEFAULT_ADAPTER" "$DEFAULT_ADAPTER" "fixed" > /dev/null create_if_config "$SELECTED_ADAPTER" "$SELECTED_ADAPTER" "fixed" > /dev/null
if [[ -n $SYSTEMDNET ]]; then if [[ -n $SYSTEMDNET ]]; then
systemd_ip_editor "${DEFAULT_ADAPTER}" systemd_ip_editor "${SELECTED_ADAPTER}"
else else
if [[ -n $(LC_ALL=C nmcli device status | grep $DEFAULT_ADAPTER | grep connected) ]]; then if [[ -n $(LC_ALL=C nmcli device status | grep $SELECTED_ADAPTER ) ]]; then
nm_ip_editor "$DEFAULT_ADAPTER" nm_ip_editor "$SELECTED_ADAPTER"
else else
ip_editor "$DEFAULT_ADAPTER" "$DEFAULT_ADAPTER" "/etc/network/interfaces" ip_editor "$SELECTED_ADAPTER" "$SELECTED_ADAPTER" "/etc/network/interfaces"
fi fi
fi fi
fi fi
...@@ -524,7 +525,7 @@ function jobs () ...@@ -524,7 +525,7 @@ function jobs ()
[[ ! -f /etc/network/interfaces ]] && echo "source /etc/network/interfaces.d/*" > /etc/network/interfaces [[ ! -f /etc/network/interfaces ]] && echo "source /etc/network/interfaces.d/*" > /etc/network/interfaces
# select default interfaces if there is more than one # select default interfaces if there is more than one
#select_default_interface select_default_interface
NETWORK_CONF="/etc/network/interfaces" NETWORK_CONF="/etc/network/interfaces"
......
...@@ -168,8 +168,8 @@ done ...@@ -168,8 +168,8 @@ done
function submenu_networking () function submenu_networking ()
{ {
# if there is more than one connected device # select default interface if there is more than one connected
select_default_interface #select_interface "default"
while true; do while true; do
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment