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

How do I tell LWP to use ipv4 and not ipv6?

Background that might be useful: I have a script that grabs data from our website. Using something like

my $ua = LWP::UserAgent->new; print "Setting Responce" if $debug; print "Why does it hang here for 2min" if $debug; my $response = $ua->request(GET 'https://ourcopanyswebsite.com/xorl/sc +ript147.php'); print "retrieving content\n" if $debug; my $returned_info = $response->content; # do stuff to returned_info and set $useful_data from it print $useful_data;

It works but there's a 120 second delay (and it is always 120 seconds) where the comment asks why does it hang here.

I have noticed if I on the command line do: wget https://ourcopanyswebsite.com/xorl/script147.php

that there is a similar long delay However if I do wget --inet4-only https://ourcopanyswebsite.com/xorl/script147.php it works fine with no delay.

While I am curious about the delay, some of the useful data I'm trying to get is the IPv4 address of the server running the script and the remote site will send the IPv6 address instead if it goes over that. So I really want to force IPv4 on LWP instead of figuring out the cause of the delay. If relevant, the server it is running on is Ubuntu 22.04.3 LTS.

Update: Corion's solution works: Adding before the request $ua->local_address("10.0.0.107") works.

Replies are listed 'Best First'.
Re: LWP and ipv4
by Corion (Patriarch) on Dec 27, 2023 at 07:30 UTC

    You likely want the local_address option to the constructor, and set that to the interface address you want to use (local_address).

Re: LWP and ipv4
by Polyglot (Chaplain) on Dec 27, 2023 at 05:17 UTC
    Perhaps others will have some solution to your LWP question--I have not found anything helpful there. I looked deep into some of the dependencies of the package and found nothing at all addressing the IPv4/IPv6 protocols, so as far as I can tell, LWP is IPv4/6 agnostic, and knows nothing about them.

    I suspect your problem is deeper than Perl, and has more to do with the underlying OS and network system. From the man pages of wget, I note the following points:

    Download Options --bind-address=ADDRESS When making client TCP/IP connections, bind to ADDRESS on t +he local machine. ADDRESS may be specified as a hostname or IP address. This + option can be useful if your machine is bound to multiple IPs. --bind-dns-address=ADDRESS [libcares only] This address overrides the route for DNS re +quests. If you ever need to circumvent the standard settings from /etc/resolv.conf, this option to +gether with --dns-servers is your friend. ADDRESS must be specifi +ed either as IPv4 or IPv6 address. Wget needs to be built with libcares for this option to be available. --dns-servers=ADDRESSES [libcares only] The given address(es) override the standard + nameserver addresses, e.g. as configured in /etc/resolv.conf. ADDRESSES may be specified + either as IPv4 or IPv6 addresses, comma-separated. Wget needs to be +built with libcares for this option to be available. -4 --inet4-only -6 --inet6-only Force connecting to IPv4 or IPv6 addresses. With --inet4-o +nly or -4, Wget will only connect to IPv4 hosts, ignoring AAAA records in DNS, a +nd refusing to connect to IPv6 addresses specified in URLs. Conversely, with --in +et6-only or -6, Wget will only connect to IPv6 hosts and ignore A records and IP +v4 addresses. Neither options should be needed normally. By default, an +IPv6-aware Wget will use the address family specified by the host's DNS record. If +the DNS responds with both IPv4 and IPv6 addresses, Wget will try them in se +quence until it finds one it can connect to. (Also see "--prefer-family" option desc +ribed below.) These options can be used to deliberately force the use of +IPv4 or IPv6 address families on dual family systems, usually to aid debugging or to deal + with broken network configuration. Only one of --inet6-only and --inet +4-only may be specified at the same time. Neither option is available in Wget compiled wi +thout IPv6 support. --prefer-family=none/IPv4/IPv6 When given a choice of several addresses, connect to the ad +dresses with specified address family first. The address order returned + by DNS is used without change by default. This avoids spurious errors and connect attempts when acces +sing hosts that resolve to both IPv6 and IPv4 addresses from IPv4 networks. For examp +le, www.kame.net resolves to 2001:200:0:8002:203:47ff:fea5:3085 and to 203.1 +78.141.194. When the preferred family is "IPv4", the IPv4 address is used first; when the +preferred family is "IPv6", the IPv6 address is used first; if the sp +ecified value is "none", the address order returned by DNS is used without change. Unlike -4 and -6, this option doesn't inhibit access to any + address family, it only changes the order in which the addresses are accessed. Als +o note that the reordering performed by this option is stable---it doesn't +affect order of addresses of the same family. That is, the relative order of all IPv +4 addresses and of all IPv6 addresses remains intact in all cases.
    Note the several references to "DNS" in the above. It may help to check your DNS settings, and if you are only directing the script to a single IP, you might try adding an entry to it in your local /etc/hosts file in order to remove the delay in DNS lookup.

    Blessings,

    ~Polyglot~