How to use the command "nm-online" (with examples)
This article will demonstrate various use cases of the command “nm-online”, which is used to ask NetworkManager whether the network is connected.
Use case 1: Find out whether the network is connected and print the result to stdout
Code:
nm-online
Motivation: This example is useful when you need to quickly check if the network is connected. By running this command, you can obtain a simple “yes” or “no” response, indicating whether the network is currently connected.
Explanation: The “nm-online” command, when run without any arguments, asks NetworkManager whether the network is connected. It returns a boolean value of “yes” or “no” to indicate the connectivity status.
Example output:
yes
In this example, the output indicates that the network is connected.
Use case 2: Wait n seconds for a connection (30 by default)
Code:
nm-online --timeout n
Motivation: This example is useful when you want to wait for a network connection before performing certain tasks. The “–timeout” argument allows you to specify the number of seconds to wait for a connection. By default, the command will wait for 30 seconds.
Explanation: The “–timeout” argument is used to specify the number of seconds to wait for a network connection. It can be followed by the desired timeout duration in seconds (n). When the timeout duration is reached, the command will exit and return the connectivity status.
Example output:
no
In this example, if the network is not connected within the specified timeout duration, the command will exit and return “no” to indicate the lack of network connectivity.
Conclusion:
The “nm-online” command is a useful tool for checking network connectivity status and waiting for a network connection. By understanding its different use cases and the arguments it accepts, you can effectively manage network connections in your system.