in reply to Module to get domain name from hostname

See Mozilla::PublicSuffix, which is a list maintained by Mozillla.

  • Comment on Re: Module to get domain name from hostname

Replies are listed 'Best First'.
Re^2: Module to get domain name from hostname
by gatkins (Initiate) on Jan 29, 2015 at 15:59 UTC
    Thanks for the prompt answer but this is not what I'm looking for.
    This outputs just the TLD which is net.br for example. I need something that outputs something.net.br (domain, insteat just the TLD)

      If you have stripped away the TLD, the last non-dot characters are the domain name. Most likely, a regular expression is enough:

      my $tld = public_suffix($hostname) or die "Invalid TLD found in '$hostname'"; $hostname =~ /([^.]+).\Q$tld\E$/ or die "Couldn't find domain name in '$hostname'"; my $domainname= $1; print "Domain name is $domainname.$tld\n";

        Thanks, that does the job.
        I have found another way, just now, using Domain::PublicSuffix

        Both methods work well, thanks again!

        Using <STDIN> works (perl b3.pl < host.txt), but i prefer to run the script without writing the external file into the terminal.
        root@sugzum:~/host# cat b2.pl #!/usr/bin/perl use feature qw(say); use Mozilla::PublicSuffix qw(public_suffix); open F, &quot;<host.txt&quot; or die $!; while (<F>) { $hostname = &quot;$_&quot;; } close F; my $tld = public_suffix($hostname) or die &quot;Invalid TLD found in '$hostname'&quot;; $hostname =~ /([^.]+).\Q$tld\E$/ or die &quot;Couldn't find domain name in '$hostname'&quot;; my $domainname= $1; print &quot;Domain name is $domainname.$tld\n&quot;; root@sugzum:~/ssh2smtp# cat host.txt 200-206-30-98.customer.tdatabrasil.net.br root@sugzum:~/ssh2smtp# perl b2.pl Invalid TLD found in '200-206-30-98.customer.tdatabrasil.net.br ' at b2.pl line 9.

        Sorry, I'm a beginner. I can't figure it out to understand how this works.

        I'm experiencing another issue now. How can I get this script to read $hostname variable from host.txt? Tried several solutions, but none worked. I could use $ARGV[0]; but it doesn't suit my needs. I need it to read the hostname from an external file. Thank you!