Re: extracting an IPv4 address from text?

From: David Christensen <dpchrist_at_holgerdanske.com>
Date: Sat, 02 Apr 2022 23:57:04 UTC
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