Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks

I want to allow a script to run only if it is physically inside a specific domain. Let's say that the script should work only if it is on a computer which is inside the network of the University of Wien (www.univie.ac.at). In order to do so, the script needs to read the network/domain in which the computer is (As you can see from the terms I am using, I am really a non-expert in all these IP & co. matters, but I am fairly okay in Perl programming ;). At the moment I can read the IP with the following:

use warnings; use Net::Address::IP::Local; print my $address = Net::Address::IP::Local->public;

Is the ip adress I am getting here stable enough to allow me to check if the computer is inside the university network over a longer period of time? If not, what is the best way to check it? Thanks

Replies are listed 'Best First'.
Re: Running script only within a specific domain/network
by haukex (Archbishop) on Jul 19, 2017 at 11:07 UTC
    I want to allow a script to run only if it is physically inside a specific domain.

    Why? As a security measure, this won't be effective. But to answer your question: www.univie.ac.at resolves to 131.130.70.8, and a Whois lookup at http://www.ripe.net/ shows that "Vienna University Computer Center" owns the entire block of IP addresses 131.130.0.0 to 131.130.255.255. So you could check whether an IP address is in that range. But IPs are easily spoofed, so again, as a security measure it isn't very effective. But it might be fine if you just wanted to have a guess at the location of the machine.

Re: Running script only within a specific domain/network
by cavac (Prior) on Jul 19, 2017 at 11:10 UTC

    You can use Net::CIDR to check if the IP falls within specific range(s).

    #!/usr/bin/env perl use strict; use warnings; use Net::CIDR; my @ips = ('192.168.0.23', '20.30.40.50'); my @uninetworks = ('192.168.0.0/16', '10.0.0.0/8'); foreach my $ip (@ips) { if(Net::CIDR::cidrlookup($ip, @uninetworks)) { print "$ip is at university network.\n"; } else { print "$ip is unsafe!!!1!\n" } }

    This results in:

    192.168.0.23 is at university network. 20.30.40.50 is unsafe!!!1!

    You should be able to get the list of the corresponding networks you want to check from your friendly system administrator.

    Edit: Always, always use strict;

    "For me, programming in Perl is like my cooking. The result may not always taste nice, but it's quick, painless and it get's food on the table."
A reply falls below the community's threshold of quality. You may see it by logging in.