telecomToperl
telecomToperl

Reputation: 39

Matching known hosts warning in regex

How could I match the following where the IP address can change:

Warning: Permanently added '100.124.61.161' (RSA) to the list of known hosts.

Thanks in advance!

Upvotes: 0

Views: 109

Answers (2)

Srihari Karanth
Srihari Karanth

Reputation: 2167

You can try the below code, change the string to restrict only specific texts.

if($string =~ m/Warning: Permanently added '(.*?)' \(RSA\) to the list of known hosts\./)
{
   print "Match Successful, IP address: $1\n";
}
else
{
   print "String did not match\n";
}

Upvotes: 3

user557597
user557597

Reputation:

A general regex for the ipv4 (no port) would be
(?<!\d)(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(?:\.(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}(?!\d)

Explained

 (?<! \d )
 (?:
      \d                      # 0 - 9
   |  [1-9] \d                # 10 - 99
   |  1 \d{2}                 # 100 - 199
   |  2 [0-4] \d              # 200 - 249
   |  25 [0-5]                # 250 - 255
 )
 (?:
      \.
      (?:
           \d 
        |  [1-9] \d 
        |  1 \d{2} 
        |  2 [0-4] \d 
        |  25 [0-5] 
      )
 ){3}
 (?! \d )

Upvotes: 1

Related Questions