Commit c353b94b authored by Igor Pecovnik's avatar Igor Pecovnik
Browse files

Setting IP for inactive interfaces

parent ad0b55d4
...@@ -347,13 +347,21 @@ exec 3>&1 ...@@ -347,13 +347,21 @@ exec 3>&1
2>&1 1>&3 | { 2>&1 1>&3 | {
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}')
if [[ -n "$localuuid" ]]; then
# adjust existing
nmcli con mod $localuuid ipv4.method manual ipv4.addresses "$address/$netmask" >/dev/null 2>&1 nmcli con mod $localuuid ipv4.method manual ipv4.addresses "$address/$netmask" >/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/$netmask" 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 +461,7 @@ function wlan_edit () ...@@ -453,7 +461,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 +551,56 @@ function get_wlan_interface () ...@@ -543,19 +551,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='*'
local ADAPTER=($(nmcli -t -f DEVICE connection show --active)) if [[ $1 == "default" ]]; then
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 +608,11 @@ function select_default_interface () ...@@ -563,7 +608,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)
[[ $IPADDR != "172.24.1.1" && -n $IPADDR ]] && LIST+=( "${i[0]//[[:blank:]]/}" "${IPADDR} ${ADD_SPEED}" ) if [[ $1 == "default" ]]; then
[[ $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 +622,7 @@ function select_default_interface () ...@@ -573,7 +622,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"
...@@ -954,18 +955,8 @@ function jobs () ...@@ -954,18 +955,8 @@ function jobs ()
--yes-label "Stop" --no-label "Cancel" --yesno "\nDo you want to stop and disable this service?" 7 50 --yes-label "Stop" --no-label "Cancel" --yesno "\nDo you want to stop and disable this service?" 7 50
exitstatus=$?; exitstatus=$?;
if [[ $exitstatus = 0 ]]; then if [[ $exitstatus = 0 ]]; then
function stop_display() service lightdm stop >/dev/null 2>&1 && systemctl disable lightdm.service >/dev/null 2>&1
{ service nodm stop >/dev/null 2>&1 && systemctl disable nodm.service >/dev/null 2>&1
bash -c "service lightdm stop >/dev/null 2>&1
systemctl disable lightdm.service >/dev/null 2>&1
service nodm stop >/dev/null 2>&1
systemctl disable nodm.service >/dev/null 2>&1"
}
if xhost >& /dev/null ; then
stop_display &
else
stop_display
fi
fi fi
else else
if ! is_package_manager_running; then if ! is_package_manager_running; then
...@@ -991,8 +982,6 @@ function jobs () ...@@ -991,8 +982,6 @@ function jobs ()
ln -s /lib/systemd/system/lightdm.service /etc/systemd/system/display-manager.service >/dev/null 2>&1 ln -s /lib/systemd/system/lightdm.service /etc/systemd/system/display-manager.service >/dev/null 2>&1
service lightdm start >/dev/null 2>&1 service lightdm start >/dev/null 2>&1
fi fi
# kill this bash script after desktop is up and if executed on console
[[ $(tty | sed -e "s:/dev/::") == tty* ]] && kill -9 $$
fi fi
fi fi
fi fi
......
...@@ -161,8 +161,8 @@ done ...@@ -161,8 +161,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