IP


Fedora/Bash: Get the IP of enp0s3

Following is a small snippet that will print on screen the IP of enp0s3 (or any other device if you change the name) while in Fedora.
As you will see, it is not a very sound solution as it depends on the structure of the output of ifconfig enp0s3.

Nevertheless is works (for Fedora at least)! 🙂

ifconfig enp0s3 | grep "inet " | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | cut -d ' ' -f 2

What this line does is: first it prints out the configuration information for enp0s3, then finds the line that contains the inet, then using sed it will trim the result (in other words, it will remove all leading and all trailing white-space from the pipe), finally cut gets the second column of the data after separating the line using the space symbol.

The Fedora version that was used for this tutorial is

$cat /etc/fedora-release 
Fedora release 23 (Twenty Three)

The version of ifconfig for this tutorial is

$ifconfig --version
net-tools 2.10-alpha

In case you want to assign the IP of enp0s3 to a variable, you can easily do as follows

IP=`ifconfig enp0s3 | grep "inet " | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | cut -d ' ' -f 2`;

Ubuntu/Bash: Get the IP of eth0

Following is a small snippet that will print on screen the IP of eth0 while in Ubuntu (Both server and desktop versions).
As you will see, it is not a very sound solution as it depends on the structure of the output of ifconfig eth0.

Nevertheless is works (for Ubuntu at least)! 🙂

ifconfig eth0 | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1

What this line does is: first it prints out the configuration information for eth0, then finds the line that contains the inet addr, using cut it gets the second column of the data after separating the line using the : symbol. Right now in the pipe we will have something similar to this 192.168.1.37 Bcast, so we need to filter out the last part as well. We do the last filtering by using cut again, this time by getting the first column while using the space character as the delimiter.

The Ubuntu version that was used for this tutorial is

$lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 14.04.4 LTS
Release:	14.04
Codename:	trusty

The version of ifconfig for this tutorial is

$ifconfig --version
net-tools 1.60
ifconfig 1.42 (2001-04-13)

In case you want to assign the IP of eth0 to a variable, you can easily do as follows

ETH0=`ifconfig eth0 | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1`;

How to set a static IP Address from the Command Line in GNU/Linux using ifconfig and route 5

Assuming you want to make the following changes to the network device eth0

  1. Change the IP to the static value 192.168.1.2
  2. Set the Subnet Mask to 255.255.255.0
  3. Set the Default Gateway for the device to be 192.168.1.1

you can perform these changes using the following two commands


sudo ifconfig eth0 192.168.1.2 netmask 255.255.255.0;
sudo route add default gw 192.168.1.1 eth0;

ifconfig

ifconfig is an application that allows you to configure a network interface.
It is used to configure the kernel-resident network interfaces. and it is used at boot time to set up interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is needed.
If no arguments are given, ifconfig displays the status of the currently active interfaces. If a single interface argument is given, it displays the status of the given interface only; if a single -a argument is given, it displays the status of all interfaces, even those that are down. Otherwise, it configures an interface.

route

route is an application that allows you to show and manipulate the IP routing table. The primary use of route is to set up static routes to specific hosts or networks via an interface after it has been configured with the ifconfig program.
When the add or del options are used, route modifies the routing tables. Without these options, route displays the current contents of the routing tables.


Ubuntu Linux: How to register machine IPs and domain name to web-server using php

First of all you will need a page the will keep record of the registrations.

To do the paste the following code in a *.php file on a php-enabled webserver:

<html>
<body>

<?php
if (isset($_GET["ip"]) && isset($_GET["name"]))
{
  echo "Current Time " . gmdate("d/m/Y H:i:s e", $_SERVER['REQUEST_TIME']) . " IP " . $_GET["ip"] . " Hostname " . $_GET["name"] . "!<br/>"; 
  $file=fopen("cookies.txt","a") or $file=fopen("log.txt","x");
  fwrite($file,"Time " . gmdate("d/m/Y H:i:s e", $_SERVER['REQUEST_TIME']) . " IP " . $_GET["ip"] . " Hostname " . $_GET["name"] . "\n" );
  fclose($file);
}
else
{
  echo "Welcome Murloc!<br />";
  echo "Following the SSH Login Log File:<br /><br />";
  $file=fopen("log.txt","r") or $file=fopen("cookies.txt","x");
  while(!feof($file))
  {
    echo fgets($file). "<br />";
  }
  fclose($file);
  echo "Your IP is ".$_SERVER['REMOTE_ADDR']. "<br />Current Time " . gmdate("d/m/Y H:i:s e", $_SERVER['REQUEST_TIME']) . "<br /><br />";
  echo "Bye Bye Bob<br />";
}
?>
</body>
</html>

The above php script will store your IP and Hostname if you provide them on the URL along with the time that the event happened and if you do not then it will list the registered IPs.

NOTE:Not sure how safe this code is

In order to invoke it / send you IP and hostname, issue form bash the following:

IPs=`ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`
wget "http://www.example.com/ip.php?ip=$IPs&name=$HOSTNAME" -o /dev/null