Как получить текущий сетевой трафик через командную строку в простом формате

Этот способ - это немного больше работы, чем некоторые другие возможные решения, но когда я удаляю файлы, мне нравится проверять, что уходит навсегда. В приведенных ниже шагах вы можете увидеть «приложение» в вашей PWD, как указано в вашем вопросе.

Сначала создайте новый текстовый файл, содержащий имена каждой папки, которую вы хотите сохранить (не удалять), с помощью одного папка на строку. Сохраните его как to-keep.txt, например:

config
logs

Затем скопируйте следующее в текстовый редактор и сохраните его как rm-exclude.sh, чтобы все три файла находились в одном каталоге.

[ f2]

Сделайте его исполняемым с помощью:

chmod +x rm-exclude.sh

Затем запустите его со следующим, где PATH - это путь к папке «приложение» из вашего PWD , В вашем примере PATH будет просто application.

./rm-exclude.sh PATH

Наконец, отметьте to-delete.txt, чтобы убедиться, что ничего не удалено, чего не должно быть, и запустите:

readarray -t dels < to-delete.txt; for del in "${dels[@]}"; do rm -rf "$del"; done

Если вам не нужно проверять содержимое файла txt, вы можете просто скопировать и вставить указанную выше команду в конец rm-exclude.sh, так что запуск скрипта делает все до тех пор, пока вы to-keep.txt уже заполнены. Конечным результатом должно быть то, что каждая прямая вложенная папка application не в to-keep.txt будет удалена вместе с их содержимым.

2
задан 8 November 2017 в 23:24

15 ответов

Вероятно, вы хотите посмотреть на vnstat

$ vnstat -d -i wlp3s0

wlp3s0  /  daily

     day         rx      |     tx      |    total    |   avg. rate
 ------------------------+-------------+-------------+---------------
 2017-10-24   166,63 MiB |   37,33 MiB |  203,96 MiB |   19,34 kbit/s
 2017-10-25   214,68 MiB |  130,26 MiB |  344,94 MiB |   32,71 kbit/s
 2017-10-26     1,01 GiB |    4,09 GiB |    5,10 GiB |  495,31 kbit/s
 2017-10-27     1,16 GiB |  113,28 MiB |    1,27 GiB |  123,04 kbit/s
 2017-10-28   201,09 MiB |  299,21 MiB |  500,31 MiB |   47,44 kbit/s
 2017-10-29     1,92 GiB |  174,35 MiB |    2,09 GiB |  202,92 kbit/s
 2017-10-30   559,56 MiB |   69,18 MiB |  628,73 MiB |   59,61 kbit/s
 2017-10-31   397,73 MiB |   43,62 MiB |  441,35 MiB |   41,85 kbit/s
 2017-11-01   665,81 MiB |   83,05 MiB |  748,86 MiB |   71,00 kbit/s
 2017-11-02   282,20 MiB |  239,91 MiB |  522,11 MiB |   49,50 kbit/s
 2017-11-03     4,06 GiB |    4,85 GiB |    8,92 GiB |  865,61 kbit/s
 2017-11-04   220,95 MiB |   40,12 MiB |  261,07 MiB |   24,75 kbit/s
 2017-11-05   320,91 MiB |    8,86 GiB |    9,18 GiB |  890,93 kbit/s
 2017-11-06   639,67 MiB |   13,77 GiB |   14,39 GiB |    1,40 Mbit/s
 2017-11-07   694,91 MiB |   80,48 MiB |  775,39 MiB |   73,52 kbit/s
 2017-11-08   178,64 MiB |   32,43 MiB |  211,07 MiB |   28,97 kbit/s
 ------------------------+-------------+-------------+---------------
 estimated       257 MiB |      46 MiB |     303 MiB |

Вы можете получать почасовые ежедневные и ежемесячные статистические данные.

3
ответ дан 22 May 2018 в 16:24

Вероятно, вы хотите посмотреть на vnstat

$ vnstat -d -i wlp3s0 wlp3s0 / daily day rx | tx | total | avg. rate ------------------------+-------------+-------------+--------------- 2017-10-24 166,63 MiB | 37,33 MiB | 203,96 MiB | 19,34 kbit/s 2017-10-25 214,68 MiB | 130,26 MiB | 344,94 MiB | 32,71 kbit/s 2017-10-26 1,01 GiB | 4,09 GiB | 5,10 GiB | 495,31 kbit/s 2017-10-27 1,16 GiB | 113,28 MiB | 1,27 GiB | 123,04 kbit/s 2017-10-28 201,09 MiB | 299,21 MiB | 500,31 MiB | 47,44 kbit/s 2017-10-29 1,92 GiB | 174,35 MiB | 2,09 GiB | 202,92 kbit/s 2017-10-30 559,56 MiB | 69,18 MiB | 628,73 MiB | 59,61 kbit/s 2017-10-31 397,73 MiB | 43,62 MiB | 441,35 MiB | 41,85 kbit/s 2017-11-01 665,81 MiB | 83,05 MiB | 748,86 MiB | 71,00 kbit/s 2017-11-02 282,20 MiB | 239,91 MiB | 522,11 MiB | 49,50 kbit/s 2017-11-03 4,06 GiB | 4,85 GiB | 8,92 GiB | 865,61 kbit/s 2017-11-04 220,95 MiB | 40,12 MiB | 261,07 MiB | 24,75 kbit/s 2017-11-05 320,91 MiB | 8,86 GiB | 9,18 GiB | 890,93 kbit/s 2017-11-06 639,67 MiB | 13,77 GiB | 14,39 GiB | 1,40 Mbit/s 2017-11-07 694,91 MiB | 80,48 MiB | 775,39 MiB | 73,52 kbit/s 2017-11-08 178,64 MiB | 32,43 MiB | 211,07 MiB | 28,97 kbit/s ------------------------+-------------+-------------+--------------- estimated 257 MiB | 46 MiB | 303 MiB |

Вы можете получать почасовые ежедневные и ежемесячные статистические данные.

3
ответ дан 18 July 2018 в 03:38

Вероятно, вы хотите посмотреть на vnstat

$ vnstat -d -i wlp3s0 wlp3s0 / daily day rx | tx | total | avg. rate ------------------------+-------------+-------------+--------------- 2017-10-24 166,63 MiB | 37,33 MiB | 203,96 MiB | 19,34 kbit/s 2017-10-25 214,68 MiB | 130,26 MiB | 344,94 MiB | 32,71 kbit/s 2017-10-26 1,01 GiB | 4,09 GiB | 5,10 GiB | 495,31 kbit/s 2017-10-27 1,16 GiB | 113,28 MiB | 1,27 GiB | 123,04 kbit/s 2017-10-28 201,09 MiB | 299,21 MiB | 500,31 MiB | 47,44 kbit/s 2017-10-29 1,92 GiB | 174,35 MiB | 2,09 GiB | 202,92 kbit/s 2017-10-30 559,56 MiB | 69,18 MiB | 628,73 MiB | 59,61 kbit/s 2017-10-31 397,73 MiB | 43,62 MiB | 441,35 MiB | 41,85 kbit/s 2017-11-01 665,81 MiB | 83,05 MiB | 748,86 MiB | 71,00 kbit/s 2017-11-02 282,20 MiB | 239,91 MiB | 522,11 MiB | 49,50 kbit/s 2017-11-03 4,06 GiB | 4,85 GiB | 8,92 GiB | 865,61 kbit/s 2017-11-04 220,95 MiB | 40,12 MiB | 261,07 MiB | 24,75 kbit/s 2017-11-05 320,91 MiB | 8,86 GiB | 9,18 GiB | 890,93 kbit/s 2017-11-06 639,67 MiB | 13,77 GiB | 14,39 GiB | 1,40 Mbit/s 2017-11-07 694,91 MiB | 80,48 MiB | 775,39 MiB | 73,52 kbit/s 2017-11-08 178,64 MiB | 32,43 MiB | 211,07 MiB | 28,97 kbit/s ------------------------+-------------+-------------+--------------- estimated 257 MiB | 46 MiB | 303 MiB |

Вы можете получать почасовые ежедневные и ежемесячные статистические данные.

3
ответ дан 24 July 2018 в 17:53

Команда ip (ранее netstat) предоставит вам этот тип информации, но вам нужно будет разобрать, чтобы получить только число AFAICT (я не эксперт в этом).

[ f1]

дает выход как:

3: wlan15: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1372 qdisc mq state UP mode DORMANT group default qlen 1000
    link/ether a0:f3:c1:28:2b:68 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    78.2M      137k     0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    146M       197k     0       0       0       0

Так например:

ip -s -c link show wlan15 | tail -n1 | cut -d " " -f5

Давал бы 146013456 или что-то вроде, 146M в необработанных байтах в качестве объема передачи на конкретном интерфейсе. tail берет последнюю строку (используйте ее с head, чтобы выбрать произвольную строку [есть много других способов]), cut разделяет этот вывод на поля с «» как разделитель и выбирая пятое поле. [ ! d4]

Использование этого значения в вашей программе для расчета средней скорости передачи должно быть простым.

2
ответ дан 22 May 2018 в 16:24

Мое предложение - это следующий сценарий, основанный на ifconfig и один мой предыдущий ответ, где приведены дополнительные пояснения.

Сценарий доступен как репозиторий GitHub по адресу: https://github.com / pa4080 / traffic-get

1. Создайте исполняемый файл сценария под названием get-traffic, который находится в /usr/local/bin, чтобы быть доступным как команда оболочки (, где ).

1. Содержимое скрипта get-traffic:

#!/bin/bash

# Set the default values or Read the users input
[ -z "${1}" ] && IFACE="eth0"  || IFACE="$1"       # Get the name of the target interface, default: eth0
[ -z "${2}" ] && UNIT="MB"     || UNIT="$2"        # Get the unit (B, KB, MB, GB, Kib, Mib, Gib), default: MB
[ -z "${3}" ] && PERIOD="30"   || PERIOD="$3"      # Get the period of measure in seconds, default: 30
[ -z "${4}" ] && OUTPUT="verb" || OUTPUT="${4,,}"  # Get the type of the output (verbose, all, incoming, outgoing, total) in lower case, default: all
LANG=C # Set envvar $LANG to `C` due to grep, awk, etc.

# Do the conversion
if   [ "$UNIT" == "B"   ]; then UN="1"
elif [ "$UNIT" == "KB"  ]; then UN="1000"
elif [ "$UNIT" == "KiB" ]; then UN="1024"
elif [ "$UNIT" == "MB"  ]; then UN="1000000"
elif [ "$UNIT" == "MiB" ]; then UN="1048576"
elif [ "$UNIT" == "GB"  ]; then UN="1000000000"
elif [ "$UNIT" == "GiB" ]; then UN="1073741824"
else echo "Wrong UNIT."; exit 1; fi

# Whether the $PERIOD is integer
if ! [[ "$PERIOD" =~ ^[0-9]+$ ]]; then echo "Enter the PERIOD in seconds"; exit 1; fi

# Get the IP address of the interface
get_ip(){ /sbin/ifconfig "$IFACE" 2>/dev/null | grep -Po '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -1; }

# The main program: If the interface has IP adders it is UP
if [[ "$(get_ip)" =~ ${IPPT} ]]; then
        bRX="$(/sbin/ifconfig "$IFACE" | grep -Po "RX bytes:[0-9]+" | sed 's/RX bytes://')" # Get the incoming traffic into the Beginning of the period
        bTX="$(/sbin/ifconfig "$IFACE" | grep -Po "TX bytes:[0-9]+" | sed 's/TX bytes://')" # Get the outgoing traffic into the Beginning of the period
        bXX=$(( bRX + bTX )) # Calculate the total traffic into the Beginning of the PERIOD

        sleep "$PERIOD" # Sleep for the PERIOD, seconds

        eRX="$(/sbin/ifconfig "$IFACE" | grep -Po "RX bytes:[0-9]+" | sed 's/RX bytes://')" # Get the incoming traffic into the End of the period
        eTX="$(/sbin/ifconfig "$IFACE" | grep -Po "TX bytes:[0-9]+" | sed 's/TX bytes://')" # Get the outgoing traffic into the End of the period
        eXX=$(( eRX + eTX )) # Calculate the total traffic into the End of the PERIOD

        RX=$(awk -v e="${eRX}" -v b="${bRX}" -v un="${UN}" 'BEGIN{ print ( e - b) / un }') # Calculate the amount of the incoming traffic for the PERIOD
        TX=$(awk -v e="${eTX}" -v b="${bTX}" -v un="${UN}" 'BEGIN{ print ( e - b) / un }') # Calculate the amount of the outgoing traffic for the PERIOD
        XX=$(awk -v e="${eXX}" -v b="${bXX}" -v un="${UN}" 'BEGIN{ print ( e - b) / un }') # Calculate the amount of the total traffic for the PERIOD

        # Output
        if   [[ "$OUTPUT" =~ ^verb ]]; then printf 'Interface: %s\nUnit: %s\nPeriod of measure: %s sec.\n\nReceived: %s\nTransmited: %s\nTotal: %s\n' "$IFACE" "$UNIT" "$PERIOD" "$RX" "$TX" "$XX"
        elif [[ "$OUTPUT" =~ ^all  ]]; then printf '%s\n%s\n%s\n' "$RX" "$TX" "$XX"
        elif [[ "$OUTPUT" =~ ^in   ]]; then printf '%s\n' "$RX"
        elif [[ "$OUTPUT" =~ ^out  ]]; then printf '%s\n' "$TX"
        elif [[ "$OUTPUT" =~ ^tot  ]]; then printf '%s\n' "$XX"
        else echo "Wrong OTPUT type."; fi
else
        echo "The INTERFACE \"$IFACE\" is down."
fi
Сценарий доступен как репозиторий GitHub по адресу: https://github.com/pa4080/traffic-get [ ! d11]

3. Синтаксис вызова сценария:

get-traffic <interface name> <units of measurement> <period of measure> <type of the output>
get-traffic enp0s25 MiB 30 total

3. Входные параметры:

<interface name> используют команду ifconfig, чтобы получить интерфейс имя. По умолчанию: eth0 <units of measurement> доступные значения: B, KB, KiB, MB, Mib, Gb, Gib. По умолчанию: MB <period of measure> в секундах. По умолчанию: 30 <type of the output> доступные значения: verbose, all, incoming, outgoing, total.

5. Примеры использования:

2
ответ дан 22 May 2018 в 16:24

Вы можете получить значения самостоятельно так же, как это делают другие программы, просмотрев содержимое, например. /proc/net/dev или /sys/class/net/eth0/statistics/rx_bytes периодически и вычисляя дельту самостоятельно.

Итак, для вашего конкретного примера,

start=$(< /sys/class/net/eth0/statistics/rx_bytes) # get received bytes
sleep 30                                           # wait 30 seconds (make this a variable in your program!)
end=$(< /sys/class/net/eth0/statistics/rx_bytes)
delta=$((end-start))                               # calculate bytes received over the period
echo $(($((delta+512))/1024))                      # roughly round to nearest 1k
2
ответ дан 22 May 2018 в 16:24

Пример с жестким кодированием с использованием ifconfig. Для более долгосрочного мониторинга vnstat более уместно.

#!/bin/bash
INTERFACE=wlp2s0
A=($(ifconfig $INTERFACE | grep bytes | sed -e 's/[(|)]//g' -e 's/:/ /g' -e 's/  */ /g'))
sleep 30
B=($(ifconfig $INTERFACE | grep bytes | sed -e 's/[(|)]//g' -e 's/:/ /g' -e 's/  */ /g'))
echo -e ${A[@]}"\n"${B[@]}
AVG=$(expr ${B[2]} - ${A[2]})
AVGKB=$(echo AVG | awk '{ byte =$1 /1024; print byte " KB" }')
#AVGMB==$(echo AVG | awk '{ byte =$1 /1024/1024; print byte " MB" }')
#AVGMB==$(echo AVG | awk '{ byte =$1 /1024/1024/1024/; print byte " GB" }')
echo -e "30 sec average\n"$AVG"\n"$AVGKB
2
ответ дан 22 May 2018 в 16:24

Пример с жестким кодированием с использованием ifconfig. Для более долгосрочного мониторинга vnstat более уместно.

#!/bin/bash INTERFACE=wlp2s0 A=($(ifconfig $INTERFACE | grep bytes | sed -e 's/[(|)]//g' -e 's/:/ /g' -e 's/ */ /g')) sleep 30 B=($(ifconfig $INTERFACE | grep bytes | sed -e 's/[(|)]//g' -e 's/:/ /g' -e 's/ */ /g')) echo -e ${A[@]}"\n"${B[@]} AVG=$(expr ${B[2]} - ${A[2]}) AVGKB=$(echo AVG | awk '{ byte =$1 /1024; print byte " KB" }') #AVGMB==$(echo AVG | awk '{ byte =$1 /1024/1024; print byte " MB" }') #AVGMB==$(echo AVG | awk '{ byte =$1 /1024/1024/1024/; print byte " GB" }') echo -e "30 sec average\n"$AVG"\n"$AVGKB
2
ответ дан 18 July 2018 в 03:38

Вы можете получить значения самостоятельно так же, как это делают другие программы, просмотрев содержимое, например. /proc/net/dev или /sys/class/net/eth0/statistics/rx_bytes периодически и вычисляя дельту самостоятельно.

Итак, для вашего конкретного примера,

start=$(< /sys/class/net/eth0/statistics/rx_bytes) # get received bytes sleep 30 # wait 30 seconds (make this a variable in your program!) end=$(< /sys/class/net/eth0/statistics/rx_bytes) delta=$((end-start)) # calculate bytes received over the period echo $(($((delta+512))/1024)) # roughly round to nearest 1k
2
ответ дан 18 July 2018 в 03:38

Мое предложение - это следующий сценарий, основанный на ifconfig и один мой предыдущий ответ, где приведены дополнительные пояснения.

Сценарий доступен как репозиторий GitHub по адресу: https://github.com / pa4080 / traffic-get

1. Создайте исполняемый файл сценария под названием get-traffic, который находится в /usr/local/bin, чтобы быть доступным как команда оболочки (, где ).

1. Содержимое скрипта get-traffic:

#!/bin/bash # Set the default values or Read the users input [ -z "${1}" ] && IFACE="eth0" || IFACE="$1" # Get the name of the target interface, default: eth0 [ -z "${2}" ] && UNIT="MB" || UNIT="$2" # Get the unit (B, KB, MB, GB, Kib, Mib, Gib), default: MB [ -z "${3}" ] && PERIOD="30" || PERIOD="$3" # Get the period of measure in seconds, default: 30 [ -z "${4}" ] && OUTPUT="verb" || OUTPUT="${4,,}" # Get the type of the output (verbose, all, incoming, outgoing, total) in lower case, default: all LANG=C # Set envvar $LANG to `C` due to grep, awk, etc. # Do the conversion if [ "$UNIT" == "B" ]; then UN="1" elif [ "$UNIT" == "KB" ]; then UN="1000" elif [ "$UNIT" == "KiB" ]; then UN="1024" elif [ "$UNIT" == "MB" ]; then UN="1000000" elif [ "$UNIT" == "MiB" ]; then UN="1048576" elif [ "$UNIT" == "GB" ]; then UN="1000000000" elif [ "$UNIT" == "GiB" ]; then UN="1073741824" else echo "Wrong UNIT."; exit 1; fi # Whether the $PERIOD is integer if ! [[ "$PERIOD" =~ ^[0-9]+$ ]]; then echo "Enter the PERIOD in seconds"; exit 1; fi # Get the IP address of the interface get_ip(){ /sbin/ifconfig "$IFACE" 2>/dev/null | grep -Po '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -1; } # The main program: If the interface has IP adders it is UP if [[ "$(get_ip)" =~ ${IPPT} ]]; then bRX="$(/sbin/ifconfig "$IFACE" | grep -Po "RX bytes:[0-9]+" | sed 's/RX bytes://')" # Get the incoming traffic into the Beginning of the period bTX="$(/sbin/ifconfig "$IFACE" | grep -Po "TX bytes:[0-9]+" | sed 's/TX bytes://')" # Get the outgoing traffic into the Beginning of the period bXX=$(( bRX + bTX )) # Calculate the total traffic into the Beginning of the PERIOD sleep "$PERIOD" # Sleep for the PERIOD, seconds eRX="$(/sbin/ifconfig "$IFACE" | grep -Po "RX bytes:[0-9]+" | sed 's/RX bytes://')" # Get the incoming traffic into the End of the period eTX="$(/sbin/ifconfig "$IFACE" | grep -Po "TX bytes:[0-9]+" | sed 's/TX bytes://')" # Get the outgoing traffic into the End of the period eXX=$(( eRX + eTX )) # Calculate the total traffic into the End of the PERIOD RX=$(awk -v e="${eRX}" -v b="${bRX}" -v un="${UN}" 'BEGIN{ print ( e - b) / un }') # Calculate the amount of the incoming traffic for the PERIOD TX=$(awk -v e="${eTX}" -v b="${bTX}" -v un="${UN}" 'BEGIN{ print ( e - b) / un }') # Calculate the amount of the outgoing traffic for the PERIOD XX=$(awk -v e="${eXX}" -v b="${bXX}" -v un="${UN}" 'BEGIN{ print ( e - b) / un }') # Calculate the amount of the total traffic for the PERIOD # Output if [[ "$OUTPUT" =~ ^verb ]]; then printf 'Interface: %s\nUnit: %s\nPeriod of measure: %s sec.\n\nReceived: %s\nTransmited: %s\nTotal: %s\n' "$IFACE" "$UNIT" "$PERIOD" "$RX" "$TX" "$XX" elif [[ "$OUTPUT" =~ ^all ]]; then printf '%s\n%s\n%s\n' "$RX" "$TX" "$XX" elif [[ "$OUTPUT" =~ ^in ]]; then printf '%s\n' "$RX" elif [[ "$OUTPUT" =~ ^out ]]; then printf '%s\n' "$TX" elif [[ "$OUTPUT" =~ ^tot ]]; then printf '%s\n' "$XX" else echo "Wrong OTPUT type."; fi else echo "The INTERFACE \"$IFACE\" is down." fi Сценарий доступен как репозиторий GitHub по адресу: https://github.com/pa4080/traffic-get [ ! d11]

3. Синтаксис вызова сценария:

get-traffic <interface name> <units of measurement> <period of measure> <type of the output> get-traffic enp0s25 MiB 30 total

3. Входные параметры:

<interface name> используют команду ifconfig, чтобы получить интерфейс имя. По умолчанию: eth0 <units of measurement> доступные значения: B, KB, KiB, MB, Mib, Gb, Gib. По умолчанию: MB <period of measure> в секундах. По умолчанию: 30 <type of the output> доступные значения: verbose, all, incoming, outgoing, total.

5. Примеры использования:

2
ответ дан 18 July 2018 в 03:38

Команда ip (ранее netstat) предоставит вам этот тип информации, но вам нужно будет разобрать, чтобы получить только число AFAICT (я не эксперт в этом).

localhost-$ ip -s -h -c link show wlan15

дает выход как:

3: wlan15: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1372 qdisc mq state UP mode DORMANT group default qlen 1000 link/ether a0:f3:c1:28:2b:68 brd ff:ff:ff:ff:ff:ff RX: bytes packets errors dropped overrun mcast 78.2M 137k 0 0 0 0 TX: bytes packets errors dropped carrier collsns 146M 197k 0 0 0 0

Так например:

ip -s -c link show wlan15 | tail -n1 | cut -d " " -f5

Давал бы 146013456 или что-то вроде, 146M в необработанных байтах в качестве объема передачи на конкретном интерфейсе. tail берет последнюю строку (используйте ее с head, чтобы выбрать произвольную строку [есть много других способов]), cut разделяет этот вывод на поля с «» как разделитель и выбирая пятое поле. [ ! d4]

Использование этого значения в вашей программе для расчета средней скорости передачи должно быть простым.

2
ответ дан 18 July 2018 в 03:38

Пример с жестким кодированием с использованием ifconfig. Для более долгосрочного мониторинга vnstat более уместно.

#!/bin/bash INTERFACE=wlp2s0 A=($(ifconfig $INTERFACE | grep bytes | sed -e 's/[(|)]//g' -e 's/:/ /g' -e 's/ */ /g')) sleep 30 B=($(ifconfig $INTERFACE | grep bytes | sed -e 's/[(|)]//g' -e 's/:/ /g' -e 's/ */ /g')) echo -e ${A[@]}"\n"${B[@]} AVG=$(expr ${B[2]} - ${A[2]}) AVGKB=$(echo AVG | awk '{ byte =$1 /1024; print byte " KB" }') #AVGMB==$(echo AVG | awk '{ byte =$1 /1024/1024; print byte " MB" }') #AVGMB==$(echo AVG | awk '{ byte =$1 /1024/1024/1024/; print byte " GB" }') echo -e "30 sec average\n"$AVG"\n"$AVGKB
2
ответ дан 24 July 2018 в 17:53

Вы можете получить значения самостоятельно так же, как это делают другие программы, просмотрев содержимое, например. /proc/net/dev или /sys/class/net/eth0/statistics/rx_bytes периодически и вычисляя дельту самостоятельно.

Итак, для вашего конкретного примера,

start=$(< /sys/class/net/eth0/statistics/rx_bytes) # get received bytes sleep 30 # wait 30 seconds (make this a variable in your program!) end=$(< /sys/class/net/eth0/statistics/rx_bytes) delta=$((end-start)) # calculate bytes received over the period echo $(($((delta+512))/1024)) # roughly round to nearest 1k
2
ответ дан 24 July 2018 в 17:53

Мое предложение - это следующий сценарий, основанный на ifconfig и один мой предыдущий ответ, где приведены дополнительные пояснения.

Сценарий доступен как репозиторий GitHub по адресу: https://github.com / pa4080 / traffic-get

1. Создайте исполняемый файл сценария под названием get-traffic, который находится в /usr/local/bin, чтобы быть доступным как команда оболочки (, где ).

1. Содержимое скрипта get-traffic:

#!/bin/bash # Set the default values or Read the users input [ -z "${1}" ] && IFACE="eth0" || IFACE="$1" # Get the name of the target interface, default: eth0 [ -z "${2}" ] && UNIT="MB" || UNIT="$2" # Get the unit (B, KB, MB, GB, Kib, Mib, Gib), default: MB [ -z "${3}" ] && PERIOD="30" || PERIOD="$3" # Get the period of measure in seconds, default: 30 [ -z "${4}" ] && OUTPUT="verb" || OUTPUT="${4,,}" # Get the type of the output (verbose, all, incoming, outgoing, total) in lower case, default: all LANG=C # Set envvar $LANG to `C` due to grep, awk, etc. # Do the conversion if [ "$UNIT" == "B" ]; then UN="1" elif [ "$UNIT" == "KB" ]; then UN="1000" elif [ "$UNIT" == "KiB" ]; then UN="1024" elif [ "$UNIT" == "MB" ]; then UN="1000000" elif [ "$UNIT" == "MiB" ]; then UN="1048576" elif [ "$UNIT" == "GB" ]; then UN="1000000000" elif [ "$UNIT" == "GiB" ]; then UN="1073741824" else echo "Wrong UNIT."; exit 1; fi # Whether the $PERIOD is integer if ! [[ "$PERIOD" =~ ^[0-9]+$ ]]; then echo "Enter the PERIOD in seconds"; exit 1; fi # Get the IP address of the interface get_ip(){ /sbin/ifconfig "$IFACE" 2>/dev/null | grep -Po '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -1; } # The main program: If the interface has IP adders it is UP if [[ "$(get_ip)" =~ ${IPPT} ]]; then bRX="$(/sbin/ifconfig "$IFACE" | grep -Po "RX bytes:[0-9]+" | sed 's/RX bytes://')" # Get the incoming traffic into the Beginning of the period bTX="$(/sbin/ifconfig "$IFACE" | grep -Po "TX bytes:[0-9]+" | sed 's/TX bytes://')" # Get the outgoing traffic into the Beginning of the period bXX=$(( bRX + bTX )) # Calculate the total traffic into the Beginning of the PERIOD sleep "$PERIOD" # Sleep for the PERIOD, seconds eRX="$(/sbin/ifconfig "$IFACE" | grep -Po "RX bytes:[0-9]+" | sed 's/RX bytes://')" # Get the incoming traffic into the End of the period eTX="$(/sbin/ifconfig "$IFACE" | grep -Po "TX bytes:[0-9]+" | sed 's/TX bytes://')" # Get the outgoing traffic into the End of the period eXX=$(( eRX + eTX )) # Calculate the total traffic into the End of the PERIOD RX=$(awk -v e="${eRX}" -v b="${bRX}" -v un="${UN}" 'BEGIN{ print ( e - b) / un }') # Calculate the amount of the incoming traffic for the PERIOD TX=$(awk -v e="${eTX}" -v b="${bTX}" -v un="${UN}" 'BEGIN{ print ( e - b) / un }') # Calculate the amount of the outgoing traffic for the PERIOD XX=$(awk -v e="${eXX}" -v b="${bXX}" -v un="${UN}" 'BEGIN{ print ( e - b) / un }') # Calculate the amount of the total traffic for the PERIOD # Output if [[ "$OUTPUT" =~ ^verb ]]; then printf 'Interface: %s\nUnit: %s\nPeriod of measure: %s sec.\n\nReceived: %s\nTransmited: %s\nTotal: %s\n' "$IFACE" "$UNIT" "$PERIOD" "$RX" "$TX" "$XX" elif [[ "$OUTPUT" =~ ^all ]]; then printf '%s\n%s\n%s\n' "$RX" "$TX" "$XX" elif [[ "$OUTPUT" =~ ^in ]]; then printf '%s\n' "$RX" elif [[ "$OUTPUT" =~ ^out ]]; then printf '%s\n' "$TX" elif [[ "$OUTPUT" =~ ^tot ]]; then printf '%s\n' "$XX" else echo "Wrong OTPUT type."; fi else echo "The INTERFACE \"$IFACE\" is down." fi Сценарий доступен как репозиторий GitHub по адресу: https://github.com/pa4080/traffic-get [ ! d11]

3. Синтаксис вызова сценария:

get-traffic <interface name> <units of measurement> <period of measure> <type of the output> get-traffic enp0s25 MiB 30 total

3. Входные параметры:

<interface name> используют команду ifconfig, чтобы получить интерфейс имя. По умолчанию: eth0 <units of measurement> доступные значения: B, KB, KiB, MB, Mib, Gb, Gib. По умолчанию: MB <period of measure> в секундах. По умолчанию: 30 <type of the output> доступные значения: verbose, all, incoming, outgoing, total.

5. Примеры использования:

2
ответ дан 24 July 2018 в 17:53

Команда ip (ранее netstat) предоставит вам этот тип информации, но вам нужно будет разобрать, чтобы получить только число AFAICT (я не эксперт в этом).

localhost-$ ip -s -h -c link show wlan15

дает выход как:

3: wlan15: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1372 qdisc mq state UP mode DORMANT group default qlen 1000 link/ether a0:f3:c1:28:2b:68 brd ff:ff:ff:ff:ff:ff RX: bytes packets errors dropped overrun mcast 78.2M 137k 0 0 0 0 TX: bytes packets errors dropped carrier collsns 146M 197k 0 0 0 0

Так например:

ip -s -c link show wlan15 | tail -n1 | cut -d " " -f5

Давал бы 146013456 или что-то вроде, 146M в необработанных байтах в качестве объема передачи на конкретном интерфейсе. tail берет последнюю строку (используйте ее с head, чтобы выбрать произвольную строку [есть много других способов]), cut разделяет этот вывод на поля с «» как разделитель и выбирая пятое поле. [ ! d4]

Использование этого значения в вашей программе для расчета средней скорости передачи должно быть простым.

2
ответ дан 24 July 2018 в 17:53

Другие вопросы по тегам:

Похожие вопросы: