I have a dynamic local Lan IP at work (temporarily), but in order to connect from home, I ssh to the host A (static IP) and from there to my machine.

However, since the IP changes, I would like to save in host A frequently, for instance every time the connection is up (I often loose my ethernet connection due to poor cables).

I'm using NetowrkManager so I created a file in /etc/NetworkManager/dispatcher.d/90exportIP which runs the following:

 ssh hostA "echo $(date)  enp4s0f2 IP: $(/sbin/ifconfig enp4s0f2 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://') >> todaysElanIP";

This gets my IP and saves it in file todaysElanIP in host A.

The problem is this script is run as root, and my keys are not there. I sometimes just run this command on bash and it works. But I would like it to run automatically every time the network is up.

I tried runuser -l myUser, but ssh-agent is not accessible like that.

It doesn't has to work every time. For instance, at boot time I haven't yet added my key to ssh-agent. If next time the network is down and then up again the export happens, that would work for me.

It's a bit confusing how I can easily do something with a non-root user and it seems so hard as root.

  • Would you like to perform the ssh hostA so you'll be logged in as a regular user instead of root? – Yaron Aug 10 '17 at 11:52
  • @Yaron Yes, I think that would work. It should be my user, because I have ssh agent running with the key to hostA. – Fernando César Aug 10 '17 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.