name resolution does not work in the /etc/network/if-up.d script

I have a raspberry pi behind a NAT and a remote virtual server.
I want to be able to access the pi via a remote shell from the outside.

To do this, I created an executable file /etc/network/if-up.d/ssh_tunnel on the containing pi

sudo -u myname ssh-fCNR *: 2222: localhost: 22 myname@1.2.3.4

Although this command works, when it is run as root from the command line, it does not work at startup.

The redirection of stderr to a file gives me

ssh: connect to the host 1.2.3.4 port 22: the network is inaccessible

What could I do better?