How to remove IP addresses from a file ?

how to extract the ip address of a file

I created this script for how to extract the ip address of a log file.

The script used as first argument the log file you want to extract the IPs and as the second argument the file where we want to loose ips.
If you do not enter the output file created in the directory where a file named these listaips.

The script to extract the ip address of a file

#!/bin/bash
# script programmed by Ignacio Alba
# http://Aplicacionesysistemas.com
# 
# if you only want to extract a file ip :
#cat origin | grip -oi "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"|sort >> destination

#help if no parameters are set
if [ -from $1 ];then
    echo -e "You have to tell me what the source and destination file with the following syntax"
    echo ""
    echo -e "\and[91m. / extraeip source destination  e[0m "
    echo ""
    echo -e "if you put destination I think the  e[91mlistaips  and[0m in the directory where you enchemtras"
exit
else
# readlink shows the full path of the file.
origin = $(readlink -f $1)
path = $(pwd)
# Variable depending on whether or not gets second parameter
if [ -from $2 ];then
    cat $1 | grip -oi "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"|sort >> listaips
    echo -e " \and[92source mArchivo          :    \and[91m $ home  e[0m "
    echo -e " \and[92mArchivo destination IPs list:    \and[91m $ path / listaips  e[0m "     
else
    destino=$(readlink -f $2)
    cat $1 | grip -oi "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"|sort >> $2
    echo -e " \and[92source mArchivo          :    \and[91m $ home  e[0m "
    echo -e " \and[92mArchivo destination IPs list:    \and[91$ m  destination and[0m " 
be
be
# Farewell message
echo ""
echo ""
sleep 1s;echo -e "\and[92Please visit us MnO  e[0m" 

sleep 1s;echo -e "            \and[42me[97me[1mhttp://aplicacionesysistemas.com  e[0m"

As extract ip addresses from a file

1) Create a new text file called extraeips.

nano extraeips

2) Paste the contents of the script (Ctrl Mayus V) keep with Ctrl S to x we ​​save.

3) We execute permissions to the script for the user (Thanks Hector Herrera)

chmod u x extraeips

4) Run the script

./extraeips source destination

Example

./extraeips /var/log/auth.log /home/aplicacionesysistemas/Documentos/listaips

or also

./extraeips origin

Example

./extraeips /var/log/auth.log

In this case we create a list of IP addresses in the directory where we are.

If you think of any way to improve the script or add new functionalities tell us as you would

 

To create this script I used information from the following pages.

http://www.forosdelweb.com/f41/sed-extraer-ips-archivo-log-517127/
http://es.kioskea.net/faq/bash-372

If you liked or useful result, Can you give the compartirlo +1 on social networks, my encouraged me a lot to keep writing.


About Ignacio Alba Obaya

Microcomputer and Technical Training for Employment. Lover of new technologies. Manager GNU / Linux and Windows.

Leave a Reply