[PATCH 1/9] 40network: add net-lib.sh

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



net-lib.sh is a library of useful functions for network stuff.

More things may get added/moved here in the future.

Signed-off-by: Will Woods <wwoods@xxxxxxxxxx>
---
 modules.d/40network/module-setup.sh |    1 +
 modules.d/40network/net-lib.sh      |   33 +++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 0 deletions(-)
 create mode 100644 modules.d/40network/net-lib.sh

diff --git a/modules.d/40network/module-setup.sh b/modules.d/40network/module-setup.sh
index ef7818a..2f4d93d 100755
--- a/modules.d/40network/module-setup.sh
+++ b/modules.d/40network/module-setup.sh
@@ -76,6 +76,7 @@ install() {
     inst "$moddir/ifup" "/sbin/ifup"
     inst "$moddir/netroot" "/sbin/netroot"
     inst "$moddir/dhclient-script" "/sbin/dhclient-script"
+    inst "$moddir/net-lib.sh" "/lib/net-lib.sh"
     inst_simple "$moddir/dhclient.conf" "/etc/dhclient.conf"
     inst_hook pre-udev 50 "$moddir/ifname-genrules.sh"
     inst_hook pre-udev 60 "$moddir/net-genrules.sh"
diff --git a/modules.d/40network/net-lib.sh b/modules.d/40network/net-lib.sh
new file mode 100644
index 0000000..0ed80b8
--- /dev/null
+++ b/modules.d/40network/net-lib.sh
@@ -0,0 +1,33 @@
+#!/bin/bash
+
+get_ip() {
+    local iface="$1" ip=""
+    ip=$(ip -o -f inet addr show $iface)
+    ip=${ip%%/*}
+    ip=${ip##* }
+}
+
+iface_for_remote_addr() {
+    set -- $(ip -o route get to $1)
+    echo $5
+}
+
+iface_for_mac() {
+    local interface="" mac="$(echo $1 | tr '[:upper:]' '[:lower:]')"
+    for interface in /sys/class/net/*; do
+        if [ $(cat $interface/address) = "$mac" ]; then
+            echo ${interface##*/}
+        fi
+    done
+}
+
+iface_has_link() {
+    local interface="$1" flags=""
+    [ -n "$interface" ] || return 2
+    interface="/sys/class/net/$interface"
+    [ -d "$interface" ] || return 2
+    flags=$(cat $interface/flags)
+    echo $(($flags|0x41)) > $interface/flags # 0x41: IFF_UP|IFF_RUNNING
+    [ "$(cat $interface/carrier)" = 1 ] || return 1
+    # XXX Do we need to reset the flags here? anaconda never bothered..
+}
-- 
1.7.7.6

--
To unsubscribe from this list: send the line "unsubscribe initramfs" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux SCSI]

  Powered by Linux