You are not logged in.

#1 2013-01-24 22:55:06

jwm-art
Member
Registered: 2011-02-01
Posts: 87

Obtain public IP address without polling-like behaviour

Hi,

I'm wanting to use systemd to obtain the my public IP address but it does it before the network is up. I'm using DHCPCD.

I don't want to consider Network Manager, it seems overkill for a desktop machine.

So far, I've created /etc/systemd/system/public-ip.service

[Unit]
Description=Obtain public IP address
After=network.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/public_ip.sh /tmp/publicip

[Install]
WantedBy=multi-user.target

and enabled it.

The public_ip.sh script greps the output of wget in order to write the public ip address to the file. The idea here is to use this information within a (popular) system information monitor which updates every second - I don't want to be sending requests to dyndns.org every second to obtain a piece of information which doesn't change? (Hmmm actually, I have experienced it changing during an ssh session IIRC).

Any ideas?

Offline

#2 2013-01-25 05:30:36

chris_l
Member
Registered: 2010-12-01
Posts: 390

Re: Obtain public IP address without polling-like behaviour

More than just using After, add also Requires.

I haven't tested this, but try it:

[Unit]
Description=Obtain public IP address
After=network.target
Requires=network.target

[Service]
Type=oneshot
ExecStart=/bin/bash -c "/usr/bin/wget 'http://whatismyipaddress.com/' -U=msie -O- -q|/bin/sed -n '/LOOKUPADDRESS/ s/.*value=\"\([^\"]*\).*/\1/p' > /tmp/publicip"

[Install]
WantedBy=multi-user.target

Added a line to get the public ip.

EDIT: yeah, he is right, using Requires does not guarantee having an ip. my bad tongue

Last edited by chris_l (2013-01-25 08:14:25)


"open source is about choice"
No.
Open source is about opening the source code complying with this conditions, period. The ability to choose among several packages is just a nice side effect.

Offline

#3 2013-01-25 07:20:42

Stebalien
Member
Registered: 2010-04-27
Posts: 1,237
Website

Re: Obtain public IP address without polling-like behaviour

Do not add Requires. That will just try to start the network target. Read this: http://www.freedesktop.org/wiki/Softwar … workTarget
One way to do this would be to write a script that keeps on calling wget until it connects (the network is ready) and then writes your IP address to a file.

Last edited by Stebalien (2013-01-25 07:21:29)


Steven [ web : git ]
GPG:  327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C
Do not email: honeypot@stebalien.com

Offline

#4 2013-01-25 08:11:17

chris_l
Member
Registered: 2010-12-01
Posts: 390

Re: Obtain public IP address without polling-like behaviour

Hmm fair enough, you are right wink
Still, if you call such script from a service, I would add Requires anyway. Why? it makes network.target to start. yes, I understood that network.target started != having ip, but, network.target started means dhcpcd is doing its job and (hopefully) is about to get an ip any moment soon, so it would require less wget retries.


"open source is about choice"
No.
Open source is about opening the source code complying with this conditions, period. The ability to choose among several packages is just a nice side effect.

Offline

#5 2013-01-26 05:37:44

Stebalien
Member
Registered: 2010-04-27
Posts: 1,237
Website

Re: Obtain public IP address without polling-like behaviour

No really, don't. The service that provides networking pulls in network.target (take a look at the netcfg, dhcpcd, and NetworkManager units).


Steven [ web : git ]
GPG:  327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C
Do not email: honeypot@stebalien.com

Offline

#6 2013-01-26 19:22:58

chris_l
Member
Registered: 2010-12-01
Posts: 390

Re: Obtain public IP address without polling-like behaviour

So, what would be better then? Requiring dhcpcd?
What would you recommend to do, in order to not have wget trying to connect since the begin of boot, but instead, making it start its connection attempts in a moment closer to the event of getting ip?

By the way jwm_art, since you experimented a ip change once, maybe also creating a timer service (or a cron job) for repeting the process every few hours would be good


"open source is about choice"
No.
Open source is about opening the source code complying with this conditions, period. The ability to choose among several packages is just a nice side effect.

Offline

#7 2013-01-26 22:45:25

Stebalien
Member
Registered: 2010-04-27
Posts: 1,237
Website

Re: Obtain public IP address without polling-like behaviour

That's why you use After. The dhcpcd service has a Wants/Before dependency on network.target. If your service has an After/(optional WantedBy) dependency on network.target, it will be started after dhcpcd.


Steven [ web : git ]
GPG:  327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C
Do not email: honeypot@stebalien.com

Offline

#8 2013-01-28 01:03:27

jwm-art
Member
Registered: 2011-02-01
Posts: 87

Re: Obtain public IP address without polling-like behaviour

Thanks for your replies.

Stebalien wrote:

Do not add Requires. That will just try to start the network target. Read this: http://www.freedesktop.org/wiki/Softwar … workTarget
One way to do this would be to write a script that keeps on calling wget until it connects (the network is ready) and then writes your IP address to a file.

Thanks for the link.  I'd been wondering about the point of network.target.

I'm guessing here... but I could add

Restart=always

into the .service file and in the script:

/* psuedo code */
if wget failed sleep for a second
else sleep for 5 minutes (or so)

Obviously not meeting the robustness the aforementioned link recommends, but a start.

Offline

Board footer

Powered by FluxBB