How To: Perl for the Aspiring Hacker (Control Statements)

Perl for the Aspiring Hacker (Control Statements)

In this tutorial we will be go over how we can make use of conditionals.

In Perl the main conditional is the if statement. The if statement in
Perl is used like this:

If (example){
} else {
#execute a different type of code than above.

It is literally that simple! I know, right?

Let's make something cool with that.

use Net::Ping;
$host = "";
$example = Net::Ping->new("icmp", 1, 64);
if ($example->ping($host)) {
print "$host is very much alive.\n";
} else {
print "$host is dead. Hopefully he resurrects.\n";

So, let's review the source code. First we start with the Shebang. After the shebang we use a Perl Module, "NET::PING;". This might be the first time you see "Net::Ping;" if you're new to Perl. This module specifically exists to do ping functions. After that, we gave an IP address to the $host variable so it can than feed the IP to the module. We than say that we want to send our ping by ICMP (OTW covered what ICMP is I believe.). We than setup an 'if' statement that will tell us if the host returned with any errors. If it did, than the script will return with a " is dead. Hopefully he resurrects." If the host returns with 0 errors, it'll come back with '"Host is very much alive."

That is all for today! Thank you.

Want to start making money as a white hat hacker? Jump-start your hacking career with our 2020 Premium Ethical Hacking Certification Training Bundle from the new Null Byte Shop and get over 60 hours of training from cybersecurity professionals.

Buy Now (90% off) >

Other worthwhile deals to check out:

Join the Next Reality AR Community

Get the latest in AR — delivered straight to your inbox.

Be the First to Comment

Share Your Thoughts

  • Hot
  • Latest