Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 2 Apr 2022 16:57:04 -0700
From:      David Christensen <dpchrist@holgerdanske.com>
To:        questions@freebsd.org
Subject:   Re: extracting an IPv4 address from text?
Message-ID:  <3bd9bbf1-d904-b8e1-1595-e19abf75ea48@holgerdanske.com>
In-Reply-To: <25160.44484.392802.868667@jerusalem.litteratus.org>
References:  <25160.44484.392802.868667@jerusalem.litteratus.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On 4/2/22 13:10, Robert Huff wrote:
> 
> Hello:
> 	Let's suppose I want to parse a line from auth.log and extract
> the IP address (if any) to stdout.
> 	I'm assuming there is a robust way to do this using standard
> command-line tools ... but my brain is flailing on the exact method.
> Anyone have a example they'd be willing to share?  Or is this a
> problem already solved?
> 
> 
> 				Hopefully,
> 
> 
> 					Robert Huff
> 


Here is one way [1, 2]:

2022-04-02 15:20:35 toor@f3 ~
# cat /var/log/auth.log | perl -ne '/(\d{1,3}(\.\d{1,3}){3})/; print 
"$1\n"' | sort | uniq

192.168.5.11
192.168.5.13
192.168.5.220
192.168.5.76


Note that only the root user can read or write /var/log/auth.log.  So, 
if a non-root process needs the information, one approach is to use 
sudo(8) [3].


David


References:

[1] https://www.oreilly.com/library/view/learning-the-unix/0596002610/

[2] https://www.oreilly.com/library/view/learning-perl-8th/9781492094944/

[3] https://mwl.io/nonfiction/tools#sudo



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3bd9bbf1-d904-b8e1-1595-e19abf75ea48>