3

I want to send my computer's IP automatically every time it boots. I edit /etc/rc.local

sleep 10
ifconfig > /tmp/myip
scp /tmp/myip <server>
exit 0

I tried, and there is no inet addr, Bcast, Mask in /tmp/myip. I guess network starting after the script. So how to do it? Thank you very much!

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Justme0
  • 33

3 Answers3

2

ifconfig does not show output, probably because by the time it runs, the networking setup is not fully completed. Furthermore, there are not 100% guarantees the dynamic IP address won't change over the server uptime.

If using dhclient, move the script to the directory /etc/dhcp/dhclient-exit-hooks.d, so it executes after getting an IP address via DHCP. Debian usually populates this directory; if it does not exist it might have to be created.

The script can be changed for sending the IP address at boot, and every time it changes. Be aware that depending in your IP address DHCP lease lifetime, you might or might not be interested in copying it everytime.

If in an ISP/Internet environment, it might be also (more) interesting to use a dynamic DNS service.

Please do see this for more details Better method for acting on IP address change from the ISP?

and

http://manpages.ubuntu.com/manpages/wily/man8/dhclient-script.8.html

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
2

The solution depends on which dhcp client daemon are you using. Most distributions (on *bsd and linux) use dhcpcd or dhclient. In both cases, you could insert your script in the client configuration.

  • dhcpcd runs /etc/dhcpcd.sh script, if it exists, each time a controlled interface goes up or down. You can simply insert your scp in this script.

    Hooking into DHCP events
      dhcpcd will run /etc/dhcpcd.sh, or the script specified by the -c,
      --script option. It will set $1 to a shell compatible file that holds
      various configuration settings obtained from the DHCP server and $2 to
      either up, down or new depending on the state of dhcpcd.  dhcpcd ignores
      the exist code of the script.
    
  • dhclient invokes the ETCDIR/dhclient-exit-hooks script just after he set the interface. You can follow the instructions in Hook section of dhclient-script man page.

    After all processing has completed, CLIENTBINDIR/dhclient-script checks
    for  the  presence  of an executable ETCDIR/dhclient-exit-hooks script,
    which if present is invoked using the ´.´ command.  The exit status  of
    dhclient-script  will be passed to dhclient-exit-hooks in the exit_sta-
    tus shell variable, and will always be zero if the script succeeded  at
    the  task  for  which  it was invoked.   The rest of the environment as
    described previously for dhclient-enter-hooks is  also  present.    The
    ETCDIR/dhclient-exit-hooks  script  can modify the valid of exit_status
    to change the exit status of dhclient-script.
    
andcoz
  • 17,130
  • In the dhclient manual citation, you are lacking "All executable scripts in ETCDIR/dhclient-exit-hooks.d/* are also invoked." – Rui F Ribeiro May 26 '16 at 08:26
  • @RuiFRibeiro probably I have an old version of dhclient manual (2.6.2) because it does not talk about dhclient-exit-hooks.d. – andcoz May 26 '16 at 08:54
  • Jessie with dhclient 4.3.1 here. What are you running? – Rui F Ribeiro May 26 '16 at 08:55
  • @RuiFRibeiro sorry for the late reply. My current version of ISC DHCP Client is 4.2.6. I do not remember where I cut that 2.6.2 from :-( – andcoz Jun 06 '16 at 14:57
-2
ifconfig ethx > /tmp/myip

x is your interface number like eth0