Я хочу получить ежедневное использование Интернета с моей машины Ubuntu в формате Json с помощью Vnstat. Для получения ежедневного использования в терминале я использую следующую команду:
vnstat -d -i wlp2s0
вывод будет:
wlp2s0 / daily
day rx | tx | total | avg. rate
------------------------+-------------+-------------+---------------
,ۋ6� 60 KiB | 27 KiB | 87 KiB | 0.01 kbit/s
,ۋ6� 333.00 MiB | 170.16 MiB | 503.16 MiB | 47.71 kbit/s
,ۋ6� 626.23 MiB | 39.64 MiB | 665.87 MiB | 63.13 kbit/s
,ۋ6� 172.47 MiB | 177.32 MiB | 349.79 MiB | 33.16 kbit/s
,ۋ6� 11.88 MiB | 1.66 MiB | 13.54 MiB | 1.28 kbit/s
,ۋ6� 0 KiB | 0 KiB | 0 KiB | 0.00 kbit/s
,ۋ6� 380.47 MiB | 21.22 MiB | 401.69 MiB | 38.09 kbit/s
,ۋ6� 173.32 MiB | 14.71 MiB | 188.03 MiB | 17.83 kbit/s
,ۋ6� 0 KiB | 0 KiB | 0 KiB | 0.00 kbit/s
,ۋ6� 0 KiB | 0 KiB | 0 KiB | 0.00 kbit/s
,ۋ6� 17.49 MiB | 4.33 MiB | 21.82 MiB | 2.07 kbit/s
,ۋ6� 70 KiB | 73 KiB | 143 KiB | 0.01 kbit/s
,ۋ6� 15.12 MiB | 1.95 MiB | 17.07 MiB | 1.62 kbit/s
,ۋ6� 18.45 MiB | 5.86 MiB | 24.31 MiB | 3.55 kbit/s
------------------------+-------------+-------------+---------------
estimated 27 MiB | 7 MiB | 34 MiB |
Итак, как получить только общее, rx и tx значение, которое это estimated 27 MiB | 7 MiB | 34 MiB | из вышеприведенного вывода в формате Json в форме:
{"daily_usage":{"rx":27,"tx":7,"total":34}}
На самом деле я пытаюсь передать этот json-формат скрипту python позже Thaks заранее!
vnstat имеет опции вывода в машиночитаемых форматах. Из man vnstat:
--json mode
Show database content for selected interface or all interfaces
in json format. All traffic values in the output are in KiB. An
optional mode parameter can be used for limiting the output to
only selected information. Everything is shown by default.
Setting mode to 'h' will output only hours, 'd' days, 'm' months
and 't' the top 10.
--xml mode
Show database content for selected interface or all interfaces
in xml format. All traffic values in the output are in KiB. An
optional mode parameter can be used for limiting the output to
only selected information. Everything is shown by default.
Setting mode to 'h' will output only hours, 'd' days, 'm' months
and 't' the top 10.
Просто сделайте vnstat -i wlp2s0 --json d и проанализируйте его на Python, чтобы получить какое-либо поле, в котором вы нуждаетесь. [F5] не требуется и будет игнорироваться, поскольку опция --json принимает аргумент mode.
vnstat имеет опции вывода в машиночитаемых форматах. Из man vnstat:
--json mode
Show database content for selected interface or all interfaces
in json format. All traffic values in the output are in KiB. An
optional mode parameter can be used for limiting the output to
only selected information. Everything is shown by default.
Setting mode to 'h' will output only hours, 'd' days, 'm' months
and 't' the top 10.
--xml mode
Show database content for selected interface or all interfaces
in xml format. All traffic values in the output are in KiB. An
optional mode parameter can be used for limiting the output to
only selected information. Everything is shown by default.
Setting mode to 'h' will output only hours, 'd' days, 'm' months
and 't' the top 10.
Просто сделайте vnstat -i wlp2s0 --json d и проанализируйте его на Python, чтобы получить какое-либо поле, в котором вы нуждаетесь. [F5] не требуется и будет игнорироваться, поскольку опция --json принимает аргумент mode.
vnstat имеет опции вывода в машиночитаемых форматах. Из man vnstat:
--json mode
Show database content for selected interface or all interfaces
in json format. All traffic values in the output are in KiB. An
optional mode parameter can be used for limiting the output to
only selected information. Everything is shown by default.
Setting mode to 'h' will output only hours, 'd' days, 'm' months
and 't' the top 10.
--xml mode
Show database content for selected interface or all interfaces
in xml format. All traffic values in the output are in KiB. An
optional mode parameter can be used for limiting the output to
only selected information. Everything is shown by default.
Setting mode to 'h' will output only hours, 'd' days, 'm' months
and 't' the top 10.
Просто сделайте vnstat -i wlp2s0 --json d и проанализируйте его на Python, чтобы получить какое-либо поле, в котором вы нуждаетесь. [F5] не требуется и будет игнорироваться, поскольку опция --json принимает аргумент mode.
@ Sjn73, так что @muru имеет правильную идею.
Единственное, что я хотел (но не могу, пока) прокомментировать, это то, что вы можете просто написать: vnstat --json d
Это переключит mode, упомянутое в документация только ежедневно. Обратите внимание, что это вход в флаг --json, а не тот же флаг, что и флаг -d.
Everything is shown by default.
Setting mode to 'h' will output only hours, 'd' days, 'm' months
and 't' the top 10.
#!/bin/bash
#get the last line
IN=$(vnstat -d | (tail -n1))
#remove estimated
INR=${IN//estimated}
#convert to array
arrOUT=(${INR//|/ })
#format the output
OUTPUT="{\"daily_usage\":{\"rx\": ${arrOUT[0]}, \"tx\": ${arrOUT[2]}, \"total\": ${arrOUT[4]} }"
OUTPUT2="{\"daily_usage\":{\"rx\": ${arrOUT[0]} ${arrOUT[1]}, \"tx\": ${arrOUT[2]} ${arrOUT[3]}, \"total\": ${arrOUT[4]} ${arrOUT[5]} }"
#pick one
echo $OUTPUT
echo $OUTPUT2
сохранить в your_script.sh права на изменение файла, чтобы сделать его исполняемым как bash your_script.sh @ Sjn73, так что @muru имеет правильную идею.
Единственное, что я хотел (но не могу, пока) прокомментировать, это то, что вы можете просто написать: vnstat --json d
Это переключит mode, упомянутое в документация только ежедневно. Обратите внимание, что это вход в флаг --json, а не тот же флаг, что и флаг -d.
Everything is shown by default.
Setting mode to 'h' will output only hours, 'd' days, 'm' months
and 't' the top 10.
#!/bin/bash
#get the last line
IN=$(vnstat -d | (tail -n1))
#remove estimated
INR=${IN//estimated}
#convert to array
arrOUT=(${INR//|/ })
#format the output
OUTPUT="{\"daily_usage\":{\"rx\": ${arrOUT[0]}, \"tx\": ${arrOUT[2]}, \"total\": ${arrOUT[4]} }"
OUTPUT2="{\"daily_usage\":{\"rx\": ${arrOUT[0]} ${arrOUT[1]}, \"tx\": ${arrOUT[2]} ${arrOUT[3]}, \"total\": ${arrOUT[4]} ${arrOUT[5]} }"
#pick one
echo $OUTPUT
echo $OUTPUT2
сохранить в your_script.sh изменить разрешения на файл и сделать его исполняемым запуска bash your_script.sh @ Sjn73, так что @muru имеет правильную идею.
Единственное, что я хотел (но не могу, пока) прокомментировать, это то, что вы можете просто написать: vnstat --json d
Это переключит mode, упомянутое в документация только ежедневно. Обратите внимание, что это вход в флаг --json, а не тот же флаг, что и флаг -d.
Everything is shown by default.
Setting mode to 'h' will output only hours, 'd' days, 'm' months
and 't' the top 10.
#!/bin/bash
#get the last line
IN=$(vnstat -d | (tail -n1))
#remove estimated
INR=${IN//estimated}
#convert to array
arrOUT=(${INR//|/ })
#format the output
OUTPUT="{\"daily_usage\":{\"rx\": ${arrOUT[0]}, \"tx\": ${arrOUT[2]}, \"total\": ${arrOUT[4]} }"
OUTPUT2="{\"daily_usage\":{\"rx\": ${arrOUT[0]} ${arrOUT[1]}, \"tx\": ${arrOUT[2]} ${arrOUT[3]}, \"total\": ${arrOUT[4]} ${arrOUT[5]} }"
#pick one
echo $OUTPUT
echo $OUTPUT2
сохранить в your_script.sh права на изменение файла, чтобы сделать его исполняемым как bash your_script.sh