hardware.sh 2.55 KB
Newer Older
1
2
3
#!/bin/sh

get_all_ethernet_nics() {
4
5
6
7
8
9
10
    for i in /sys/class/net/*; do
        # type = 1 means ethernet (ARPHDR_ETHER, see Linux' sources,
        # beginning of include/linux/if_arp.h)
        if [ "$(cat "${i}"/type)" = 1 ]; then
            basename "${i}"
        fi
    done
11
12
}

13
nic_exists() {
14
    [ -e /sys/class/net/"${1}" ]
15
16
}

17
nic_is_up() {
18
    [ "$(cat /sys/class/net/"${1}"/operstate)" = "up" ]
19
}
20
21
22

# The following "nic"-related functions require that the argument is a
# NIC that exists
23

24
25
26
27
28
29
30
31
nic_ipv4_addr() {
    ip addr show "${1}" | sed -n 's,^\s*inet \([0-9\.]\+\)/.*$,\1,p'
}

nic_ipv6_addr() {
    ip addr show "${1}" | sed -n 's,^\s*inet6 \([0-9a-fA-F:]\+\)/.*$,\1,p'
}

32
# Will just output nothing on failure
33
get_current_mac_of_nic() {
34
35
36
37
38
    local mac
    mac="$(macchanger "${1}" | sed -n "s/^Current\s*MAC:\s*\([0-9a-f:]\+\)\s.*$/\1/p" || :)"
    if echo "${mac}:" | grep -q "^\([0-9a-fA-F]\{2\}:\)\{6\}$"; then
        echo "${mac}"
    fi
39
40
41
42
43
44
45
}

get_module_used_by_nic() {
    basename "$(readlink "/sys/class/net/${1}/device/driver/module")"
}

get_name_of_nic() {
46
47
48
  vendor=$(udevadm info -x --query=property /sys/class/net/${1} | sed -n "s/ID_VENDOR_FROM_DATABASE='\(.*\)'/\\1/p" || : )
  device=$(udevadm info -x --query=property /sys/class/net/${1} | sed -n "s/ID_MODEL_FROM_DATABASE='\(.*\)'/\\1/p" || : )
  echo "${vendor} ${device}"
49
50
51
}

# Auxillary function for mod_rev_dep(). It recurses over the graph of
Tails developers's avatar
Tails developers committed
52
53
54
55
# kernel module depencies of $@ (note that it only works for loaded
# modules). To deal with circular dependencies a global variable
# MOD_REV_DEP_VISITED keeps track of already visited nodes, and it
# should be unset before the first call of this function.
56
57
58
mod_rev_dep_aux() {
  local mod
  local rev_deps
59
  for mod in "${@}"; do
60
61
62
63
64
65
66
67
68
69
70
71
72
    if echo ${MOD_REV_DEP_VISITED} | grep -qw ${mod}; then
        continue
    fi
    MOD_REV_DEP_VISITED="${MOD_REV_DEP_VISITED} ${mod}"
    # extract the "Used by" column for $mod from lsmod
    rev_deps=$(lsmod | \
               sed -n "s/^${mod}\s\+\S\+\s\+\S\+\s\+\(\S\+\)/\1/p" | \
               tr ',' ' ')
    mod_rev_dep_aux ${rev_deps}
    echo ${mod}
  done
}

Tails developers's avatar
Tails developers committed
73
# Prints a list of all loaded modules depending on $1, including $1. It's
74
# ordered by descending "maximum dependency distance" from $1, so the
75
# output is ideal if we want to unload $1 and (by necessity) all
76
77
78
79
80
81
82
83
84
# modules that uses $1.
mod_rev_dep() {
  MOD_REV_DEP_VISITED=""
  mod_rev_dep_aux ${1}
}

# Unloads module $1, and all modules that (transatively) depends on
# $1 (i.e. its reverse dependencies).
unload_module_and_rev_deps() {
85
  /sbin/modprobe -r $(mod_rev_dep ${1})
86
}