Using cURL to retreive public IP for tmux config creates too many connections

0

I am currently using the command: curl icanhazip.com and the option set -g status-left ... in my tmux.conf to retrieve and display my public IP in status bar.

This works great, although I am really bothered by the number of requests that are being made to retrieve the same IP over and over again.

Question:

How can I acheive this (retrieve/display public IP in tmux status–bar) with only one communication stream per session (or something less redundant) using the tmux configuration?

I have looked through curl's man page and tried various rate limiting args in the command [to no avail], though I think this is something specific to tmux.

I could use an external script or function of some kind to accomplish this although i'd like to keep this simple.

Any suggestions to solve this or alternate methods entirely would be great!

edit: formatting

jredd

Posted 2014-08-15T13:47:27.247

Reputation: 776

Is there a problem with using ifconfig to get the IP addr of your interface? – JezC – 2014-08-15T14:10:32.840

In most cases ifconfig or ip addr will only display my local/NAT IP. That is sufficient for most of my VPS' which are not abstracted by NAT. – jredd – 2014-08-15T14:18:02.663

What triggers a change in the NAT-ed IP address? Powercycle? Out of your control? Timer? Non-use for a while? Something else? – JezC – 2014-08-15T14:53:15.850

In my tmux.conf I have a line in there for my local IP using ipconfig in addition to a line that gets my public facing WAN IP. On my VPS these two are the same. But on my local machines and servers they will not be the same. – jredd – 2014-08-15T15:03:43.810

Answers

1

Cache the information:

  1. Add a cron job that runs every 5 or 10 minutes:

    curl http://icanhazip.com > ~/.cache/my-ip
    

    Some sources provide this information over connectionless protocols and therefore won't be as strict with their limits as connection-based HTTP sources:

    dig +short myip.opendns.com @208.67.222.222 > ~/.cache/my-ip
    
  2. In tmux, change the command to simply read from your cache file:

    cat ~/.cache/my-ip
    

This could be done by a single script too, but it would be much less simple:

#!/usr/bin/env perl
use LWP::Simple;

sub read_cached_ip {
    my ($file) = @_;
    return undef if !-e $file; # check if file exists
    my $mtime = (stat $file)[9];
    return undef if time - $mtime >= 5*60; # check if file has expired
    if (open(my $fh, "<", $file)) {
        chomp(my $ip = <$fh>);
        close($ip);
        return $ip;
    }
    return undef;
}

sub update_ip {
    my ($file) = @_;
    my $ip = LWP::Simple::get("http://icanhazip.com");
    if (open(my $fh, ">", $file)) {
        print $fh "$ip\n";
        close($ip);
    }
    return $ip;
}

my $file = $ENV{HOME}."/.cache/my-ip";
my $ip = read_cached_ip($file) // update_ip($file);
print "$ip\n";

user1686

Posted 2014-08-15T13:47:27.247

Reputation: 283 655

Thank you for the suggestion. I think I am going add something like this to my .profile or some rc script in all of my machines to cache this in $HOME/.cache/public_ip (or something similar). – jredd – 2014-08-15T14:43:36.187