title: systemd: start service at boot time after network is really up (for WoL purpose)
body: <p>I have a computer at work which I sometimes wakeup from home in order to access it but when boots and gets another IP address from our DHCP server, how can I access it?</p>
  
  <p>The situation and my “workflow” is as follows:</p>
  
  <ul>
  <li>From my home PC I connect to the office's VPN</li>
  <li>ssh to a dedicated server in office's LAN (it has a fixed IP address)</li>
  <li>on that server I call a script that broadcasts a WoL packet with my office PC's MAC address</li>
  <li>my office PC starts up (it really does, for sure!)</li>
  </ul>
  
  <p>Now in theory I'd be able to SSH to my office PC if only I'd knew its IP address.
  Sometimes it gets the same, sometimes it changes.
  To circumvent this I had the following idea:</p>
  
  <ul>
  <li>after booting my office PC does an SSH to the office server and writes its own IP address into some text file on the server</li>
  <li>I examine that file on the server (which I can connect to because of its fixed IP), find the IP address of my office PC, and can then SSH from my home PC to my office PC</li>
  </ul>
  
  <p>All computers run Linux; Ubuntu 14.04 at home, SLES on the office server, OpenSUSE 13.1 on my office PC. This is all not a problem.</p>
  
  <p>For this all to work I simply need a script on my office PC that runs at boot time <em>when the network is up and running</em>.</p>
  
  <p>My script (publish_ip.sh) is like follows:</p>
  
  <pre><code># get own IP address:
  ip=$(ip addr show | awk '$1=="inet" &amp;&amp; $2 !~ /127\.0\.0\.1/ { split($2, a, "/"); print a[1]}');
  
  # SSH to the office server (10.64.5.84) and write own IP address to a file there:
  ssh -x -e none 10.64.5.84 "echo $(date) $ip &gt;&gt; office_pc_address.txt"
  </code></pre>
  
  <p>To run this script at boot time I created a systemd service file, publish-ip.service, for my office PC:</p>
  
  <pre><code>[Unit]
  Description=publishes own IP address
  Wants=network.target
  After=network.target
  
  [Service]
  Type=oneshot
  ExecStartPre=/usr/bin/sleep 30
  ExecStart=/home/perldog/bin/publish_ip.sh
  User=perldog
  
  [Install]
  WantedBy=multi-user.target
  </code></pre>
  
  <p>But this is what I always get on my office PC:</p>
  
  <pre><code>linux-tz7m:/usr/lib/systemd/system # systemctl status publish-ip.service
  publish-ip.service - publishes own IP address
     Loaded: loaded (/usr/lib/systemd/system/publish-ip.service; enabled)
     Active: failed (Result: exit-code) since Mon 2016-02-29 12:17:34 CET; 4 days ago
    Process: 1688 ExecStart=/home/perldog/bin/publish_ip.sh (code=exited, status=255)
    Process: 1016 ExecStartPre=/usr/bin/sleep 30 (code=exited, status=0/SUCCESS)
   Main PID: 1688 (code=exited, status=255)
  
  Feb 29 12:17:34 linux-tz7m publish_ip.sh[1688]: ssh: connect to host 10.64.5.84 port 22: Network is unreachable
  </code></pre>
  
  <p>Obviously my service starts and also calls my script but the SSH command in that script fails with <code>Network is unreachable</code>.</p>
  
  <p>I tried everything in my service file so that it runs only <strong><em>after</em></strong> the network is up, but I don't get it. I tried <code>Wants=network.target</code>,
  <code>After=network.target</code>, <code>WantedBy=multi-user.target</code>, and even inserted an <code>ExecStartPre=/usr/bin/sleep 30</code>. Nothing worked.
  I always get <code>Network is unreachable</code> when my script is called and tries to SSH to the office server.</p>
  
  <p>Question: What settings are required in my service file so that it runs only <em>after</em> the office server is reachable with SSH?</p>
  
  <p>Note: When I'm at office and my office PC is up-and-running, both my script and the service work perfectly, i.e. <code>systemctl start publish-ip.service</code>
  works without any errors.</p>