Assign IP address to a variable
Created by A. Martínez on 2019-05-09
4z.com
Table des matičres
Assign IP address to a variable
This document aims at explaining how to retrieve the IP address of a server and store it in a variable.
The "ifconfig" application is utilized in Unix-like operating systems to configure the network interfaces of the system. If we just execute it:
ifconfig
we will see an output like this:
We can see the list of the network interfaces and their IP addresses. We have two interfaces: the "ens3" interface, and the "lo" interface.
The "ens3" interface is the common network interface which connects our server to an outside network.
The "lo" interface ("loopback") is a virtual network interface that the server has to communicate to itself. We can connect to a server which is running in our local machine by means of this interface.
Thus, the info we want to retrieve will be in the "ens3", this is, the public IP address. In this case, this server has just one interface to the outside, but it can have some more.
Now, we need the IP address as a string. Let us propose two approaches.
The "grep" command looks for specific patterns in a file. The "grep" command will print each line in which a match occurs (actually, it has many options to print the output).
We are going to link (to "pipe") the output generated by the "ifconfig" command to be the input of the grep command. In this case, we can see this output as the file to look for the pattern. In order to pipe commands in Linux, we use the "|" symbol.
However, it is still pending to decide which pattern to locate. It should be a unique pattern in order to retrieve just the line that contains the IP address. Could be "broadcast" this desired pattern? Yes. So, let us try it:
ifconfig | grep "broadcast"
We can see that now we are closer since the output just has one line. Nonetheless, we still need to filter this line, and here is where "awk" comes into play.
"awk" is a language itself. It can be seen as a text processor to perform complex data extractions. One of its main commands is the "print" command. The "print" command will print the whole line of a text. If we want to just print the fields within this record/line, we can use the "$" symbol, and then the position in the record.
In the record that we have to filter, the IP address can be seen as the second field, right? We are going to "awk" this record and ask it to provide the field in the second position:
ifconfig | grep "broadcast" | awk '{print $2}'
Finally, the output of this last command is the desired public IP of the server.
In this approach, we are going to substitute the "awk" command by the "cut" one.
The "cut" command extracts pieces of text from input lines, so it can be a suitable tool for us.
In this situation, we will use the "-c" option. This option tells to "cut" to extract a character range in the specified positions within the line. For example, "cut -c 14-28", means that we are retrieving the characters from the 14th to the 28th position of the line.
We can implement this with the following command:
ifconfig | grep "broadcast" | cut -c 14-28
which finally shows the searched IP address.
In many situations, we will need to store this string (the IP address) in a variable. The usual way to do this is the following: variable=$(some_funny_command). By doing so, we are taking the output of the "some_funny_command" command, and assigning it to a variable. Let us pose the following example:
date
my_variable=$(date)
echo $variable
We can see that the output of the "date" command is stored in the variable "my_variable". Then, we just print the content of "my_variable", which obviously is the same of "date" (at the time that the "date" command was assigned to "my_variable").
Now we are ready to apply this idea to the present situation. We can store the whole command designed in the previous sections to the variable we want.
IP_address1=$(ifconfig | grep "broadcast" | awk '{print $2}')
echo $IP_address1
IP_address=$(ifconfig | grep "broadcast" | cut -c 14-28)
echo $IP_address2
To validate this document, the netmask of the "lo" interface (output of the "ifconfig" command) should be retrieved.
Both methods should be utilized.
Finally, the netmask should be assigned to a variable.
Provide the screenshots with the output of each step.