2

Do you know a single line command to capture NIC speed of the primary interface of the server.

For example, for eth0

  • Get hostname using uname -n
  • Get its IP using nslookup `uname -n`
  • Search for the IP in ifconfig output to know on which interface it's configured
  • Using ethtool <interface captured in above command> | grep -i speed

Can all above command be executed in single command?

  • 3
    What exactly do you mean by primary interface? Your method to try and obtain it doesn't make sense to me. What would it be for a router for instance? – Stéphane Chazelas Apr 30 '15 at 19:58
  • Server could have many interface eth0,eth1,eth2 or em1,em2 or em3 .Primary interface means where ip of the server is configured and I need to get its nic speed.At this moment i dont know if its configured on em2 or em3 or eth5. In one line command how to fetch nic speed of that particular interface? – Venom Apr 30 '15 at 20:03
  • There's no such thing as an IP address of a server. There may be IPv4 and/or IPv6 addresses assigned to any network interface of a server. Usually, servers have at least 2 network interfaces: lo and generally at least one ethernet or bond one, often bridge ones, often vlan ones... – Stéphane Chazelas Apr 30 '15 at 20:17
  • Why must this be executed as a single command (line)? – roaima Apr 30 '15 at 21:53
  • 1
    @roaima...the reason I asked in single command for my convenience...I can push this one line command to several server in a script to capture data . – Venom Apr 30 '15 at 23:34
6

Try:

cat /sys/class/net/eth0/speed

I'm not sure what you mean by primary interface. On a host with an IPv4 stack, you could retrieve the interface where the first default route is with:

ip route show 0/0 | grep -Pom1 'dev +\K[^ ]+'

(assuming GNU grep). So:

cat "/sys/class/net/$(ip route show 0/0 | grep -Pom1 'dev +\K[^ ]+')/speed"

Not all IPv4 connected hosts have a default route. You may prefer one interface by which to reach a particular host instead, like a known internet one like 8.8.8.8 or since you're running those commands over ssh, the IP address we see the ssh connection coming from (assuming you're connecting over IPv4 and not IPv6):

ip route show to match "${SSH_CLIENT%% *}" | grep -Pom1 'dev +\K[^ ]+'

Of course that doesn't work if that interface is not an ethernet one (it's common to have a bridge interface there on servers for instance).

As a different heuristic, you could get the name of the non-virtual interface that is up, that does have a speed and that has transmitted the greatest number of packets with something like:

readlink -f /sys/class/net/* | awk -F / '
   $4 != "virtual" && \
     getline speed < ($0 "/speed") && \
     getline state < ($0 "/operstate") && \
     state == "up" && \
     getline tx < ($0 "/statistics/tx_packets") {
       if (tx > max) {returned_speed=speed; max=tx}
     }
   END{print returned_speed}'

Last, a comment on you asking for a single command: note that the sh in ssh is for shell. So the code it runs on the remote machine is shell code. You can run several commands and multi-line scripts there. The thing to remember though is that the code is interpreted by the login shell of the remote user which is not guaranteed to be Bourne-like (though in practice nowadays on Linux-based systems usually is). So you can do for instance:

remote_code=$(cat << \end_of_script
  readlink -f /sys/class/net/* | awk -F / '
     $4 != "virtual" && \
       getline speed < ($0 "/speed") && \
       getline state < ($0 "/operstate") && \
       state == "up" && \
       getline tx < ($0 "/statistics/tx_packets") {
         if (tx > max) {returned_speed=speed; max=tx}
       }
     END{print returned_speed}'
end_of_script
)

for host in host1 host2 host3; do
  speed=$(ssh "$host" "$remote_code")
  printf '%20s: %s\n' "$host" "$speed"
done

That remote_code above is in a syntax compatible with shells of the Bourne, rc and fish families, but not (t)csh.

  • but you never know if its configured on eth0 ..it can be on eth2 or even em1.It should capture values not specific to server interface name. – Venom Apr 30 '15 at 19:53
  • @Venom then with all due respect you're asking the wrong question. The question you should have asked is "How can I determine which network interface is a server's primary interface?". But as Stéphane already said, your proposed method to make this determination using hostname and DNS queries and IP address matching is arbitrary at best and flawed at worst. – Celada Apr 30 '15 at 20:08
  • I agree..I just gave my lame idea...Could you pls give a better way . ??I should have asked " How can I determine nic speed of network interface which is a server's primary interface?" – Venom Apr 30 '15 at 20:10
  • The problem is that there is no good definition for "a server's primary interface". – Celada Apr 30 '15 at 20:13
  • 2
    @Venom, I have a server with several interfaces. None has a default route. All are valid addresses for the server. How should we define which one is "primary"? – roaima Apr 30 '15 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.