Get a "Bad hostname" error on Linux

541 views
Skip to first unread message

nixbuilder

unread,
Aug 8, 2012, 3:47:50 PM8/8/12
to WWW::Mechanize users
Since going some OS and package upgrades, I am now getting errors when
try to connect with a previously very stable script.

When I do a $mech->get($URL), it returns "Bad hostame". However the
hostname does resolve when using nslookup or dig.

So while running my script I also ran wireshark to see what was
happening... the only thing I noticed was that DNS queries were going
for AAAA records (IPv6) first... getting nothing back because there is
no IPv6 on the requested machine, and then doing a query for the "A"
record, which returns with the IP address. But the $mech->get() does
not seem to pick up on the "A" IP address.

Any ideas?

BTW... versions are Linux=3.4.6, glibc=2.16.0 perl=5.14.2 and WWW-
Mechanize=1.72.

Natxo Asenjo

unread,
Aug 10, 2012, 4:46:45 PM8/10/12
to www-mecha...@googlegroups.com
On Wed, Aug 8, 2012 at 9:47 PM, nixbuilder <nixbu...@gmail.com> wrote:
Since going some OS and package upgrades, I am now getting errors when
try to connect with a previously very stable script.

When I do a $mech->get($URL), it returns "Bad hostame".  However the
hostname does resolve when using nslookup or dig.

So while running my script I also ran wireshark to see what was
happening... the only thing I noticed was that DNS queries were going
for AAAA records (IPv6) first... getting nothing back because there is
no IPv6 on the requested machine, and then doing a query for the "A"
record, which returns with the IP address.  But the $mech->get() does
not seem to pick up on the "A" IP address.
 

without seeing some code it is hard to tell.

You can try adding show_progress(1) to $mech in order to get some debugging info about what is going on; you can also disable ipv6 (although I do not think that is going to make any difference).
 
--
natxo

nixbuilder

unread,
Aug 17, 2012, 10:17:19 AM8/17/12
to www-mecha...@googlegroups.com
The code snippet is pretty simple:
#!/usr/bin/perl
use strict;
use WWW::Mechanize;
use WWW::Mechanize::Link;
use HTTP::Request::Common;
use HTTP::Cookies;
use LWP;
use Encode;
use DBI;
use IPC::Open2;
use Date::Calc qw( Decode_Month );
use File::Copy;
use Term::Menu;
use Switch;

my $cookie_jar = HTTP::Cookies->new;
my $agent = WWW::Mechanize->new(show_progress => 1);

# give the agent a place to stash the cookies
$agent->cookie_jar($cookie_jar);

#-----------------------------------------------------------------------------
# Get the top page
#-----------------------------------------------------------------------------
my $URL="http://hostname.company.com/e-enterprise/formslogin.asp?/e-enterprise";
$agent->get($URL);
print "Loaded login page\n" unless $agent->{'status'} != 200;

And the result is:
** GET http://host.company.com/e-enterprise/formslogin.asp?/e-enterprise ==> 500 Can't connect to host.company.com:80 (Bad hostname)
Error GETing http://host.company.com/e-enterprise/formslogin.asp?/e-enterprise: Can't connect to host.company.com.com:80 (Bad hostname) at ./xxxxx.pl line 28

But if I use the actual IP address, something peculiar happens:
** GET http://172.22.48.130/e-enterprise/formslogin.asp?/e-enterprise ==> 200 OK
Loaded login page
** POST http://HOST.COMPANY.COM:80/e-enterprise/formslogin.asp?/e-enterprise ==> 500 Can't connect to HOST.COMPANY.COM:80 (Bad hostname)
Error POSTing http://HOST.COMPANY.COM:80/e-enterprise/formslogin.asp?/e-enterprise: Can't connect to HOST.COMPANY.COM:80 (Bad hostname) at xxxxxx.pl

Notice that for some reason, if I use the IP address in $URL, the code does a reverse DNS lookup (notice now how the FQDN is capitalized). and subsequent calls fail.

nixbuilder

unread,
Aug 21, 2012, 4:16:03 PM8/21/12
to www-mecha...@googlegroups.com
Solved the problem... it was IPv6... turned off resolving IPv6 and it fixed the problem...


On Wednesday, August 8, 2012 2:47:50 PM UTC-5, nixbuilder wrote:
Reply all
Reply to author
Forward
0 new messages