Perl obtains the external IP address of the local machine.

Source: Internet
Author: User

Note:Read the output of the "netstat-p tcp" command, extract the data in the foreign address section, and count the multiple connections of the same IP address.

Advantages:Compared with the output of the netstat command, you can more intuitively find abnormal external connection IP addresses.

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    

#! /Usr/bin/perl-wuse strict; ######################################## ############################### file: foreign. PL # function: view the IP information of all external connections to the local machine # Description: extract the data of foreign address by reading the output of the "netstat-p tcp" command, # Count multiple connections of the same IP address. # Advantages: Compared with the output of the netstat command, you can more intuitively find abnormal external connection IP addresses. # Version: v0.1 # Time: # Author: semi-idle # Test Platform: # sco_sv scosysv 3.2 5.0.6 i386 ################################## ##################################### open (netstat, "netstat-p tcp |") or die "can't open wordcount: $! "; My % foreign_hash; while (<netstat>) {# match characters in the format of a class-type IP address (for example, 11.11.11.) If (/(? : \ D + \.) +/) {my $ match =$ &; # Remove the. (point) characters that match strings before the end of the string $ match = ~ S /. $ //; $ foreign_hash {$ match} ++ ;}} close netstat; foreach my $ key (Keys % foreign_hash) {print "$ key \ t => \ t $ foreign_hash {$ key} \ n ";}

After execution:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.