<![CDATA[Latest posts for the topic "Code for Fun: perl script GetIP"]]> /hvaonline/posts/list/23.html JForum - http://www.jforum.net Code for Fun: perl script GetIP Code:
#!/usr/bin/perl
# Get IP perl script
# Written By Mr.Khoai
# Sep 7, 2006
# Copyrights--You have the rights to copy
#########################################

# This script uses http://whatismyip.com to determine the real WAN IP.
#+It may not be absolutely corect if user goes throug a proxy server. 
#+This script requires:
#	1. PERL!!! This is perl script. No perl == No run
#	2. A working network. This includes proper configurations of
#+NICs and an internet connection.
#	3. wget. This is a handy tool to download any file on the 
#+line. wget also support ftp,sll, etc. For more information, please
#+refer to GNU wget @ http://www.gnu.org/software/wget

my $tmp="/tmp/__GetIPtmp";							# temp
`wget http://whatismyip.com -q -O $tmp`;					# wget http://www.whatismyip.com/index.html
										# wwwected to $tmp
open($F, $tmp);
while (my $line = <$F>) {							# Get each line
	if ($line =~ /(^<TITLE>.+ )(\d{1,3}\.\d{1,3}\.\d{1,3})(.+<\/TITLE>)/)
#		      |^^^^^^^^^^^||^^^^^^^^^^^^^^^^^^^^^^^^^||^^^^^^^^^^^^|
#		      |		  ||	 		     |+------------=====> End with </TITLE>
#		      |		  |+-------------------------===================> Some thing look like IP
#		      +-----------==============================================> Start with <TITLE>
	{	
		$line=$2;							# Only get the IP Part
		&printStuff($line);
		close ($F);
		`rm $filename`;
		exit (0);
	}
}
print "Opps, cant see your IP. Check all the requirements to see if you meet 'em all!\n";

sub printStuff(){
	my $ip = shift;
	my $msg = "+-------------------------------------+\n";
	$msg   .= "|           GetIP perl script         |\n";
	$msg   .= "|          Written by Mr.Khoai        |\n";
	$msg   .= "+-------------------------------------+\n";
	$msg   .= "\nYour IP is: $ip\n";
	print $msg;
}
Tuy đoạn code chả có gì to bự nhưng cũng giúp ta thấy được regular expression lợi hại thế nào. PerlRegEx dùng để parse text thì khỏi chê. Bao nhiêu cảm xúc + bức xúc của tuần đầu tiên được đi học lập trình Khoai đã "nhét" vào đây cả. Mr.Khoai]]>
/hvaonline/posts/list/3729.html#21759 /hvaonline/posts/list/3729.html#21759 GMT