बस इसे करने का सबसे अच्छा तरीका संभवतः पार्स करना है /proc/net/dev(चेतावनी दी जाए कि /procयह पोर्टेबल नहीं है)। यहां एक bashस्क्रिप्ट है जिसे मैंने जल्दी से एक साथ रखा है जो इसे गणना करने में सक्षम होना चाहिए:
#!/bin/bash
_die() {
printf '%s\n' "$@"
exit 1
}
_interface=$1
[[ ${_interface} ]] || _die 'Usage: ifspeed [interface]'
grep -q "^ *${_interface}:" /proc/net/dev || _die "Interface ${_interface} not found in /proc/net/dev"
_interface_bytes_in_old=$(awk "/^ *${_interface}:/"' { if ($1 ~ /.*:[0-9][0-9]*/) { sub(/^.*:/, "") ; print $1 } else { print $2 } }' /proc/net/dev)
_interface_bytes_out_old=$(awk "/^ *${_interface}:/"' { if ($1 ~ /.*:[0-9][0-9]*/) { print $9 } else { print $10 } }' /proc/net/dev)
while sleep 1; do
_interface_bytes_in_new=$(awk "/^ *${_interface}:/"' { if ($1 ~ /.*:[0-9][0-9]*/) { sub(/^.*:/, "") ; print $1 } else { print $2 } }' /proc/net/dev)
_interface_bytes_out_new=$(awk "/^ *${_interface}:/"' { if ($1 ~ /.*:[0-9][0-9]*/) { print $9 } else { print $10 } }' /proc/net/dev)
printf '%s: %s\n' 'Bytes in/sec' "$(( _interface_bytes_in_new - _interface_bytes_in_old ))" \
'Bytes out/sec' "$(( _interface_bytes_out_new - _interface_bytes_out_old ))"
# printf '%s: %s\n' 'Kilobytes in/sec' "$(( ( _interface_bytes_in_new - _interface_bytes_in_old ) / 1024 ))" \
# 'Kilobytes out/sec' "$(( ( _interface_bytes_out_new - _interface_bytes_out_old ) / 1024 ))"
# printf '%s: %s\n' 'Megabits in/sec' "$(( ( _interface_bytes_in_new - _interface_bytes_in_old ) / 131072 ))" \
# 'Megabits out/sec' "$(( ( _interface_bytes_out_new - _interface_bytes_out_old ) / 131072 ))"
_interface_bytes_in_old=${_interface_bytes_in_new}
_interface_bytes_out_old=${_interface_bytes_out_new}
done
इस बात को ध्यान में रखें कि sleepलूप में ऑपरेशन करने में जितना समय लगता है, उतना समय नहीं लगता है, इसलिए यह गलत है। मेरे 600mhz कॉपरमाइन पर, लूप 0.011 सेकंड लेता है - अधिकांश प्रयोजनों के लिए एक नगण्य अशुद्धि। किलोबाइट / मेगाबिट आउटपुट का उपयोग करते समय ध्यान रखें कि बैश केवल पूर्णांक अंकगणित को समझता है।
/proc/net/dev, वास्तव में यह समझने के बिना कि यह जादू क्या और कैसे होता है।