Git Product home page Git Product logo

synology-diskstation-scripts's Introduction

synology-diskstation-scripts

Scripts that help with Synology Diskstation maintenance

Why do I need this?

tl;dr version:

You are running Synology Diskstation DNS and DHCP services and you want dynamic DHCP reservations to update DNS immediately.

Background

Synology Diskstation has an embedded DNS server that can be enabled for your network. This does the standard thing of resolving hostnames to IP addresses. So, lets say you have a VMware ESXi server on your local network. You arbitrarily name it esxi1.homelan.net. The DNS server is where you map the name esxi1.homelan.net to the static IP address of 192.168.1.10. From then on, all other devices in your network can access this server using the name esxi.homelan.net. Only the DNS server needs to remember the IP address. Nothing new here.

Synology diskstation also has a DHCP server that you can use to dynamically assign IP addreses to hosts on your network. This means you can power up a new laptop, ipad, or guest VM on your network and it will be able to use the network without configuring anything. Under the covers, they use DHCP to get an available IP address from your DHCP server.

Synology can host both of these services and they both work well by themselves. However, they don't talk to each other. They both deal with IP addresses and hostnames, yet they operate independently. For example, when you power on your laptop, the laptop will essentially say something like this: "Hello, my hostname is garylaptop and I need an available IP address". The DHCP server will gladly assign an unused IP address, but that is where things end. The DNS server knows nothing about this hostname to IP address assignment. This means that no other host on the network will be able to refer to the laptop if they only know the hostname. You can't, for example, do something like ssh garylaptop from another host on your network.

For the average consumer client device like a laptop or ipad, this is normally fine. It is unlikely that other devices on the network want to communicate with the laptop using a hostname.

This becomes more of an issue when you have more devices and servers running on your network. The default for most new servers and clients is to use DHCP to get an IP address. This makes things simpler for setting up that new linux distribution, but gets in the way when you want to experimient with some server software on there. One approach is to manually assign a static IP address and create a static DNS entry for this new server. This is something you would want to do if you know you want to keep that server around for a while. But if you are just messing around with something new, it is quite handy to have all of this taken care of for you. This is where this project comes in.

diskstation_dns_modify.sh

This script can be used to configure a synology diskstation to automatically update its internal DNS records from its internal DHCP server. As of 2014-10-20 Synology Diskstation DSM cannot do this from the GUI.

Credit

The script originated from Tim Smith here:

http://forum.synology.com/enu/viewtopic.php?f=233&t=88517

Original docs:

https://www.youtube.com/watch?v=T22xytAWq3A&list=UUp8GcSEeUnLY8d6RAT6Y3Mg

changelog

2014-10-20 Modified to honor DNS static IP addresses

For example, lets say you have configured DNS in synology DSM. You are manually assigning static IP addresses using the DSM GUI in the range of 192.168.1.1 - 192.168.1.99. You have also configured Synology DHCP to serve dynamic IP addresses starting at 192.168.1.100. This is all works fine. However, without this script here, Synology will not update DNS records when the DHCP server assigns a new dynamic IP address. This script does that and it now does it without overwriting static DNS entries in the range of 192.168.1.1 - 192.168.1.99.

2014-11-10 Fixed a bug where DNS update would fail if DHCP client does not specify a hostname

2014-11-23 A new script is available to update DNS within 10 seconds of a new DHCP reservation

2015-05-01 DHCP leases in /etc/dhcpd/dhcpd.conf.leases are now supported.

2015-08-13 A new script is available to start this service each time the synology diskstation boots up. You no longer need to have any Task Scheduler items when using this script

2015-08-13 Updated documentation

2015-08-13 Updated file path in scripts to match a more generic location of the admin account directory.

2016-04-18 Fixed issue #5 (script output would redirect to the wrong place)

2016-04-18 Fixed issue #6 (documentation)

2016-04-18 Fixed issue #7 (added new lease file location for DSM 6.0)

2016-09-17 log file now prints out more debug information about both static and dynamic addresses populated into DNS records

2016-09-17 S99pollDHCP.sh should now work correctly under both DSM 5 and DSM 6

2016-09-17 Fixed issue #11 Serial # is now updated for DNS zone master forward and reverse files

2017-02-22 Fixed issue #19 S99pollDHCP.sh now has a "status" command and also handles the situation where multiple poll-dhcp-changes.sh process may have been erroneously running

2017-03-06 diskstation_dns_modify.sh now uses a more deterministic method for gathering network interfaces instead of making assumptions about which network interfaces are present PR #17

2017-03-06 diskstation_dns_modify.sh now removes invalid characters from generated DNS hostnames per RFC 952 PR #17

2021-7-21 Fixed issue #34 Better error checking for using DHCP files created when running DSM 7.

Deployment

You will need to:

  1. Install two scripts into the "admin" account. These scripts should be owned by root and executable:

    DiskStation> ls -l /var/services/homes/admin/*sh
    -rwxr-xr-x    1 root     root          7798 May  1 15:07 /var/services/homes/admin/diskstation_dns_modify.sh
    -rwxr-xr-x    1 root     root           283 Nov 21  2014 /var/services/homes/admin/poll-dhcp-changes.sh
    

    These scripts do not need to be modified.

  2. Install the settings file to the admin directory:

    DiskStation> ls -l /var/services/homes/admin/settings                              
    -rw-r--r--    1 root     root           109 Sep 24  2015 /var/services/homes/admin/settings
    

    The settings file needs to be modified to match your network. See the comments in the diskstation_dns_modify.sh script for details.

  3. Install the start script into /usr/local/etc/rc.d/ directory. It also should be owed by root and executable.

DiskStation> ls -l /usr/local/etc/rc.d
-rwxr-xr-x    1 root     root           693 Aug  6 13:40 S99pollDHCP.sh

This S99pollDHCP.sh script will be called during the Synology DSM boot process the next time the server is restarted. This script can also be started manually:

DiskStation> /usr/local/etc/rc.d/S99pollDHCP.sh start

and stopped:

DiskStation> /usr/local/etc/rc.d/S99pollDHCP.sh stop

Warning If you manually start the server manually like this, this script will only run while you are logged on to the ssh console. The script will stop once you log off. DNS will not be updated from new reservations until this script is started again. To run the script permanently, you will need to reboot your Synology Diskstation. This is because there is no "nohup" command in the standard Synology DSM.

Starting via Task Scheduler

If it is inconvenient to restart your Synology Diskstation, there is an alternate way to start this service using the DSM Task Scheduler (Main Menu -> Control Panel -> Task Scheduler). The script will run indefinitely when started this way. This is basically a way to simulate "nohup" without going down the path of installing extra software on Synology DSM just to avoid having to do a reboot the first time the software is installed.

  1. Open Task Scheduler
  2. Click Create -> User-defined script
  3. Key in a name for the task. Anything is fine here.
  4. Uncheck the "Enabled" button. You don't want this task to start this service over and over on some schedule. We are just going to use the task scheduler to start our service one time.
  5. Key in this in the User-defined script area and click OK:
        /usr/local/etc/rc.d/S99pollDHCP.sh start >>/var/services/homes/admin/startS99pollDHCP.log

You can now select the script from the list and Click run. Boom. Done.

Troubleshooting

Each time this script detects that that there is a DHCP change, DNS will be updated. It may take up to 10 seconds for DNS to be updated after a new DHCP reservation. A log file of this process is kept at /var/services/homes/admin/logs/dhcp-dns.log.

You can also view the DNS log from the normal DSM UI. This can be useful if there is some sort of conflict between static DNS entries that you defined in the DSM DNS UI and new DHCP hostnames.

synology-diskstation-scripts's People

Contributors

gclayburg avatar wolfspiritm avatar xantios avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

synology-diskstation-scripts's Issues

Path to diskstation_dns_modify.sh is fixed

Path to diskstation_dns_modify.sh is hard coded in poll-dhcp-changes.sh. This means that if the scripts are installed in the admin dir with a git clone, the execution fails.

Suggestion for improvement: Keep all manual added entries intact

Hello,

Thank you for working on this script. I started using it myself and ran into the problem that I want to keep adding A Records myself. The script only prints them when they are in the range of static leases but that ended with multiple lines of the same host whenever the host releases and renews it's IP.

I fixed that with a way that is (in my opinion) way better then keeping out every A Record or PTR record.
I changed the printDhcpAsRecords Function to add a ";dynamic" comment at the end of every line that is dynamicly added:

           if (RecordType == "PTR") {print 1000 + arr[4] "\t" ReverseIP ".in-addr.arpa. " RENEW " PTR " NAME "." YourNetworkName ". ;dynamic"}
           if (RecordType == "A") print 2000 + arr[4] "\t" NAME "." YourNetworkName ". " RENEW " A " IP " ;dynamic"

After that I changed the printPartialDNSFile to include that check like that:

       if( $3 == "A"){
          # Do not print dynamic entries. They will be added afterwards from the lease files.
          if ($5 != ";dynamic") {
            PrintThis=1;
          } else{
            PrintThis=0;
          }
        } else if($3 == "PTR" ){
          if ($5 != ";dynamic") {
            PrintThis=1;
          } else{
            PrintThis=0;
          }
        } else
          PrintThis=1;

The DSM UI will not remove this comment if you add new entries as well as it doesn't seem to care about the comment. The only time it is removed is if you manually edit a dynamic A entry which will make it to a static one.

Thought I will share this change here. Maybe you'd like to include it into the script.

Same device appears twice in the dns list

Which is caused eg by my phone sometimes using my 5G wifi and other times my 2G4 network. The Synology DNS has problems coping with this, causing other devices to not resolved. (the devices appearing after the double entry)

Solution would be to ensure entries are uniwue, and in case of double dhcp entries, use the entry with the longest remaining lease time ( thus the youngest, or the bottommost?)

I am quite a noob at shell programming, otherwise I would try fixing this myself ๐Ÿˆ‚๏ธ
image

the galaxy-a5-hans device also appears on my dns server twice (no screenshot since I dropped one to get it working again)

Creates duplicates IPs in DNS

If you have existing A records in DNS then the diskstation_dns_modify.sh script when run for the first time will create duplicate A records in DNS. The problem is that those records cannot be deleted using the GUI. I had to do surgery to correct this.

synology-diskstation-scripts

Hi,
I've got a question.

YourNetworkName=home.lan
ForwardMasterFile=home.lan
ReverseMasterFile=1.168.192.in-addr.arpa

What in case when I have "large" network.
172.26.200.0/22?
How to use ReverseMasterFile?

NS record issue

Hello all,
First, thank you for these scripts!
Everything is working properly but each time that an update is done, all my NS records are deleted.
How can I fix this issue?

Synology S99pollDHCP.sh not working on DSM 6.0.1

While the ps command has been changed in DSM 6.0.1, you'll get an error, when checking, whether poll-dhcp-changes.sh is running or not.

The lines in S99pollDHCP.sh should read:
POLL_RUNNING=ps x | grep poll-dhcp-changes | grep -v grep |wc -l
MYPID=ps x | grep poll-dhcp-changes | grep -v grep | awk -F' ' '{print $1}'

Same issue in startDHCP-DNS.sh. Should read:
POLL_RUNNING=ps x | grep poll-dhcp-changes | grep -v grep |wc -l

" 1 ;" lines in forward & reverse master file

I've been getting these lines at the beginning and end of both the forward and reverse master file, which leads the DNS server to reject them with error "zone xx.xx/IN not loaded due to errors."

Unfortunately I have no idea how to create a pull request to submit a proposed fix - can you help, please?

Changing frequency and schedule

I was wondering how I could change the update to happen every hour and then only run during certain hours? I would assume I could create a task to stop and start when I want and the frequency would be to change the poll-dhcp-changes.sh loop from 5 to 3600 for 1 hour?

Small problem when starting S99pollDHCP.sh

the script throws an error every time it is run after the first time when reying to recreate the directory /var/services/homes/admin.

And the file that the out put is redirected into is a little unusual :-) it is currently set to 'pwd'. I have attached my file changes .
S99pollDHCP_zaf.txt

In the documentation there is no mention of the new settings file. It still says to edit the file diskstation_dns_modify.sh.

Other than that great script and documentation, all works great!

Zafrusteria

Can you help / advise how to create a wildcard entry?

This script is really helping to align my dev environment to how I want it.

As well as registering the A record for new machine name I would also like to add a wildcard entry for sub-domains of the machine. IE:

*.newDHCPmachine.mydns-domain.com

Could you advise what changes I need to make to the script?

Thanks

Cannot locate /var/services/homes/admin/

Hi.
I have a new Synology DS420+ and am trying to run the script.
I cannot locate /var/services/homes/admin with SSH as root.

root@MySynology:~# cd /var/services/homes/
-ash: cd: /var/services/homes/: No such file or directory

Do you know where the script should be installed now ?
Thanks.

special characters in hostnames

I'm too old school, I'll see if I can get a pull request submitted but I'm so use to things like SVN lol..

I digress, many hostnames these days have special characters such as a dash "-" in them. The version of named that Synology is shipping blows up when these types of hostnames exist and will not load the zone file. Given this, something like the following (or strip out the - and not the whole hostname):

               if (NAME !~ /-/) {
                    if (RecordType == "PTR") {print 1000 + arr[4] "\t" ReverseIP ".in-addr.arpa.\t" RENEW "\tPTR\t" NAME "." YourNetworkName ".\t;dynamic"}
                    if (RecordType == "A") print 2000 + arr[4] "\t" NAME "." YourNetworkName ".\t" RENEW "\tA\t" IP "\t;dynamic"
               }

support for multiple zones

Hi all,
this script has been working flawlessly for me for quite some time. Unfortunately my requirements have now changed and I would like to update dns-records for multiple zone on multiple subnets. I have a router which provides the networks on different interfaces and dhcp relays so the synology can give out leases. That part works fine. My setup looks like this:
fw eth0: WAN
fw eth1: 192.168.0.1/24 with synology at 0.205 as DNS/DHCP, domain: intern
fw eth2: 192.168.10.1/24 domain: lan2.intern
fw eth3: 192.168.20.1/24 domain: lan3.intern

the synology gives out dhcp leases from the correct ranges to the correct interfaces. is there an easy way to adapt the script to update different zone files according to the IP of the client? maybe just run multiple instances of the script, one for each zone, with different settings files?
Cheers!

Synology DCHP / DNS update script broken in DSM 6.0-7321

The script that takes the Synology DHCP database and creates DNS A records from them is unfortunately broken in DSM 6. The script runs and deletes all A records from the DNS zone file, but it no longer repopulates it. So, if you update to DSM 6, beware... you'll have an empty zone file. I attempted to reach out to Tim Smith through the Synology forums, but his last post was in 2014 and he hasn't responded yet. I have been trying to figure this one out myself, but I'm in over my head. Any chance you could take a look at it and see if you can figure out why it's not working?

The original thread where this script was discussed can be found here:
https://forum.synology.com/enu/viewtopic.php?f=233&t=88517&p=429340

Many thanks,
-Chris Johnson

DSM7

New path dhcpd.conf.leases
DSM6 ATIME=stat /etc/dhcpd/dhcpd-leases.log | grep Modify
DSM7 ATIME=stat /etc/dhcpd/dhcpd.conf.leases g | grep Modify
EDIT:dhcpd.conf.leases

Task Scheduler sample correction

This is great stuff. Thank you! Only issue I had was that your example for starting the poll script via Task Scheduler is missing the "start" command. It should be:

/usr/local/etc/rc.d/S99pollDHCP.sh start >>/var/services/homes/admin/startS99pollDHCP.log

Dynamic PTR-Record will be missing when IP is subset of any static IP

Hi Gary,
Your script works pretty good on my DS, but PTR-Records will be missing in generated reverse zone file, when IP is a subset of any static IP - e.g.:
static record: 42.178.168.192.in-addr.arpa. 86400 PTR .....
dynamic record: 2.178.168.192.in-addr.arpa. 86400 PTR .....
Best regards
Bernd

incorrect syntax if-condition S99pollDHCP.sh, line 42

When I tried to execute the S99pollDHCP.sh script to stop it from the commandline I got back:

root@Synna:/usr/local/etc/rc.d# ./S99pollDHCP.sh stop
./S99pollDHCP.sh: line 42: [: : integer expression expected

Looking at that line it seems the text quotation marks are improperly placed. (But I'm a noob at linux scripting) Correcting the conditional statement as follows made it work:
if [ "$MYPID -gt 1" ]; then

I tried also as followed (akin to line 29)
if [ $MYPID -gt "1" ]; then
but this results in error:

root@Synna:/usr/local/etc/rc.d# ./S99pollDHCP.sh stop
./S99pollDHCP.sh: line 42: [: -gt: unary operator expected

Oddly enough this line 29 executes normally when starting the script with:

root@Synna:/usr/local/etc/rc.d# ./S99pollDHCP.sh start
2017-02-21_18:12:35 - is poll-dhcp-changes.sh running?
2017-02-21_18:12:35 - starting poll-dhcp-changes

I'm still a noob when it comes to github and linux scripting so I didn't want to try and make a fork or whatever is needed to make a change in the code. Please verify if this is correct, and not break some other consideration.

Also, I noticed this script only works for the default case of a administrative user "admin". For security reasons I disabled that user and made a custom administrator user. Can these scripts be made more flexible in this regard? Or was there some reason to not deviate from default?

Different approach to fix the same problem

My DS214Play NAS ran into trouble evenry now and then using your nifty script in that it failed resolving local computers in my network. Resolving outside the network was no problem.

You may want to take a look at the solution I provided, it's on stackoverflow:

https://stackoverflow.com/questions/53163225/how-can-i-update-a-non-windows-dns-server-a-records-from-dhcp-on-a-synology-nas

Nevertheless your script gave me the inspiration to work around it :)

Multiple LAN segments - minor

First thanks for this handy script.

I have two of my multi-port 1813+ in use across different scopes. Segments are 192.168.0.0 & 192.168.1.0 I've manually enabled NAT across segments just for information - although not terribly relevant.

I have the reverse zone set correctly with the segment I want to populate dynamically (192.168.1.0), The script is also adding the 0.0 segment node's information as there is no filter to manage more than one segment (understandably). It isn't a biggie - but just thought I'd mention it.

FWIW Synology does a terrible job at isolating DHCP broadcasts across segments as well. I've had less than productive discussions with them and they've simple said "they are aware"...

zones populated, but no names' resolution

hello everyone,

I'm trying to use this, but the settings file was being ignored. So I wrote the settings directly in diskstation_dns_modify.sh and now the zones are being filled (you might want to try this if your PTR are being populated - as you're using the same subnet as the author - but not your A records). Alas, no cigar. nslookup can't resolve local names from my pc, neither can my android phone.

Here's my situation: I'm trying to have my android phone reliably find the machines on the lan through their names instead of me having to start a network discovery app, get their IP and then enter that in whatever app I want to use at that moment.

To achieve that, I'm providing both DHCP and DNS from my 6.2.2 Synology. DHCP works great, and yes, it tells the clients to use my synology as a DNS server. DNS forwarding to the outside world works too. I don't know why the resolution doesn't work for the LAN nodes.

Bonus question #1: did I understand correctly that the StartDHCP-DNS.sh is only needed if one uses the task manager way of restarting the script after boot?

Bonus question #2: did I understand correctly that, if this works, the short form of the names becomes usable? So, if garyslaptop join my.lan, then mydesktop can connect to it using "garyslaptop" and does not need to use garyslaptop.my.lan? Now I know: yes it does

Many thanks all

status for integration with DSM 7?

Currently two issues are open with a reference to DSM 7 forming either part of all of the title.

What is the status of the project with respect to DSM 7? Does it function completely properly, does it function but with problems, or does it function not at all, when running on DSM 7?

DSM 7 - Zone File Permissions

In DSM 7, the zone files are owned by DNSServer:DNSServer. Once a sync runs, you can no longer add zone records and get the following errors:
create:
image
delete:
image

diskstation_dns_modify.sh sets the ownership to nobody:nobody which seemingly works fine in DSM 6

if ! chown nobody:nobody $BackupPath/$ForwardMasterFile.bumped $BackupPath/$ReverseMasterFile.bumped ; then

image

modifying line 200 to this: "if ! chown DNSServer:DNSServer $BackupPath/$ForwardMasterFile.bumped $BackupPath/$ReverseMasterFile.bumped ; then" resolved the issue

Unfortunately I do not have a DSM 6 system to test on, so not to sure if raising a PR to merge in the change will be useful

What About DNS Entries Whose DHCP Leases Have Expired?

I get what you're doing, and it's a great idea. But, if I'm understanding correctly, as the leases file grows, so will be number of DNS entries in the zone. What about when a DHCP lease expires. At a (very) quick glance I didn't see any way for DNS entries to be removed. Did I miss it?

Typo in documentation

Under the "troubleshooting" section on the project page (README.md) you mention

"A log file of this process is kept at /var/services/homes/logs/dhcp-dns.log"

It should read

A log file of this process is kept at /var/services/homes/admin/logs/dhcp-dns.log

thanks

DSM 6.1 and ";dynamic" alternative

Hello,

So I recently noticed that my diskstation ends up getting very slow from time to time and only a reboot fixes it. "top" as well as "ps" aren't giving any results. Just telling me the "load average" is high even so no process is using a high amount of CPU. I wasn't able to figure out what the reason is yet but once I disabled the polling on boot I haven't had any issues anymore. I also found a note somewhere that someone else had troubles with another script that was set to boot automaticly from "rc.d" caused high load for him even so it wasn't doing anything. It MIGHT have something to do with DSM 6.1 beta (and now release) so I just thought I'd mention it here.
Maybe someone else has noticed the same.

However I also noticed that a recent change (might be a bit older so not sure if it's related to 6.1 but I just noticed) in DNSServer seems to block updating zone records with comments. As I implemented the ";dynamic" workaround a while ago it was able to edit these records in the Frontend and it automaticly removed the comment marking it as static. Now it seems to break saying it can't find the record in /var/logs/message and telling me to relog into DSM.

So I thought about another way to figure out which records are static and which ones are dynamic and came up with this solution:
Instead of checking for ";dynamic" I'd check for the second column to be "86400".
This seems to be the default value for DHCP Client (maybe we can make this dynamic to really check for the value in the dhcp lease file).
Now we remove all records that match the default value while preserving all others.
That means to make a record static set its DNS TTL to 86401 and you're done.
At the same time it makes the DNSServer Frontend happy as it is all valid.
Any thoughts? Maybe better ideas? If not I'll open a merge request (there is still one open from me. So not sure if that got missed.)

As a slightly related question:
Why are the permissions of the zonefile changed to "nobody:nobody" instead of DNSServer:DNSServer?
That prevents editing it in the frontend completly.
Or is that to prevent crosswriting/reading?

Add logrotate to script log

As the log file grew quite large, i added logrotate:

bash-4.4# cat > DHCP_To_DNS
/var/services/homes/admin/dhcp-dns.log
/var/services/homes/admin/startS99pollDHCP.log {
        size 50M
                weekly
        rotate 60
                copytruncate
                dateext
        missingok
                compress
                postrotate
                    /var/services/homes/admin/S99pollDHCP.sh stop
                        /var/services/homes/admin/S99pollDHCP.sh start
}
bash-4.4# pwd
/usr/local/etc/logrotate.d
bash-4.4#

I suggest to include this in your instructions.

Network Interface listed as device name

Currently there is a hardcoded list of Network Interfaces checked to not store the network interface as hostname if the device doesn't provide one. On my Synology I have a bond and the network Interface name is called ovs_bond0 so I got a dns entry called "ovs_bond0" and I think that depends on if ovs is enabled or disabled, too.

As a fix I did the following:
At the top I added ( in my case under ZonePath=):

NetworkInterfaces=",`ip -o link show | awk -F': ' '{printf $2","}'`"

Which results in a list like this depending on the setup:
,lo,sit0,eth0,eth1,ovs-system,ovs_bond0,docker0,docker4452410,

Then I changed the awk call to this:

awk -v YourNetworkName=$YourNetworkName -v RecordType=$1  -v StaticRecords=$2 -v adapters=$NetworkInterfaces

And removed the hardcoded list from the BEGIN block of the awk script.

Now I was thinking about people maybe having multiple machines for docker (for example) or having a system called ovs-system coincidentally.
The main need of blocking that names is the dhcp-leases.log which stores devices like this:
TIMESTAMP MAC IP HOSTNAME INTERFACE
And if there is no hostname set for this device:
TIMESTAMP MAC IP INTERFACE
It would be easy to just add lines which contain more then 4 columns but I'm not sure if the following exists, too in some cases:
TIMESTAMP MAC IP HOSTNAME (Interface is missing)
So what I did is I changed the AWK script to add the line when the following is true:

  1. More then 4 Columns (no matter what the hostname is as with 4 columns we can be pretty sure that the interface would be the last one)
  2. Less then 4 Columns and Column 3 is not an Interfacename
    Also for anything else (static leases via dhcp-host) I removed that check as the interface is not contained in that line anyway.
    However I disallowed any name that is an asterisk (which is a placeholder) or empty in both cases.

The final AWK script I'm using:

    awk -v YourNetworkName=$YourNetworkName -v RecordType=$1  -v StaticRecords=$2 -v adapters=$NetworkInterfaces '
        BEGIN {
           # Set awks field separator
           FS="[\t =,]";
        }
        {IP=""} # clear out variables
        # Leases start with numbers. Don't use if column 4 is an Interface
        $1 ~ /^[0-9]/ {  if(NF>4 || index(adapters, "," $4 "," ) == 0) { IP=$3; NAME=$4; RENEW=86400 } } 
        # Static assignments start with dhcp-host
        $1 == "dhcp-host" {IP=$4; NAME=$3; RENEW=$5}
        # If we have an IP and a NAME (and if name is not a placeholder)
        (IP != "" && NAME!="*" && NAME!="") {
           split(IP,arr,".");
           ReverseIP = arr[4] "." arr[3] "." arr[2] "." arr[1];
           if(RecordType == "PTR" && index(StaticRecords, ReverseIP ".in-addr.arpa.," ) > 0) {IP="";}
           if(RecordType == "A" && index(StaticRecords, NAME "." YourNetworkName ".," ) > 0) {IP="";}
           # Print the last number in the IP address so we can sort the addresses
           # Add a tab character so that "cut" sees two fields... it will print the second
           # field and remove the first which is the last number in the IP address.
           if(IP != "" && NAME!="*" && NAME!="") {
               if (RecordType == "PTR") {print 1000 + arr[4] "\t" ReverseIP ".in-addr.arpa.\t" RENEW "\tPTR\t" NAME "." YourNetworkName ".\t;dynamic"}
               if (RecordType == "A") print 2000 + arr[4] "\t" NAME "." YourNetworkName ".\t" RENEW "\tA\t" IP "\t;dynamic"
           }
        }
    ' $DHCPAssigned| sort | cut -f 2- | uniq

P.S.:
Also the static leases are saved in /etc/dhcpd/dhcpd-ovs_bond0-static.conf for me but as the script reads the dhcpd.conf, too, I didn't need to change anything. Is the call to eth0-static.conf really needed as both files contain the same data (at least in my bond0 case)?

Here is the final script:
https://gist.github.com/WolfspiritM/a47aef594bef4498063f0d4db2140b34

these scripts do not work in synology active directory / DSM7

Thank you all very much for these scripts.

I am running AD in synology DSM7, same issue with DHCP not talking to DNS, and I tried this script but does not work.

I remember when I was using DSM6, I used a script and follow the instruction from following link, but now I am using DSM7, somehow I can not find synology's script dhcpd-script.sh under /usr/share/dhcpd/, this folder is no longer there.

Here was old link talking about a new script for DHCP and DNS at AD condition:
https://stackoverflow.com/questions/53163225/how-can-i-update-a-non-windows-dns-server-a-records-from-dhcp-on-a-synology-nas

Wondering anybody have some good ideas?

Upgrade from version 2016/02

I have installed the scripts from https://blogging.dragon.org.uk/wp-content/uploads/2016/02/dhcp_dns_changes_synology.tgz with the provided install script. This is an old version (2016/02).
The install script does extra the configuration of logrotate. In the current version there is no install script and not a logrotate configuration file (dhcp-dns).

Is it enough to follow the deployment instruction in README.md?
Do I have to remove all old files, inclusive the logrotate configuration file?

SAMBA Intergrated DNS updates

Hi, All,
With Synology implementing SAMBA's excellent Active Directory Domain support into their devices, I believe AD domains are using SAMBA's integrated DNS, rather than the traditional BIND text format (although their may be a BIND_DLZ integration module running there somewhere - I cannot be certain though I've tried to locate/demonstate/detect it.)

To update records in this scenario you need to execute samba-tools with an appropriate login who has DNSUpdateProxy Active Directory Group membership. This is necessary because the DNS is not held in BIND text databases, but are 'Active Directory Integrated' databases, likely trivial database files (.tdb)

The commands of interest are samba-tools dns add, samba-tools dns update , samba-tools dns query, samba-tool dns delete.
From the online help:
Available subcommands: add - Add a DNS record delete - Delete a DNS record query - Query a name. roothints - Query root hints. serverinfo - Query for Server information. synoquery - Query a name. update - Update a DNS record zonecreate - Create a zone. zonedelete - Delete a zone. zoneinfo - Query for zone information. zonelist - Query for zones.
I was wondering if you believe there is an opportunity to expand this functionality to include AD-integrated DNS records. This would be brilliant.

Typically ROOT user cannot be added to the AD group needed, so that would be a problem that would need to be resolved, and the samba-tool implementation on the synology currently does not seem to support the --username= --password= commandline switches that would make this a doddle to get around.

I have not been able to test the functionality of the samba-tool command because of this limitation - it prompts for root credentials.

Just thought I'd throw it out there for comment.
Many thanks,
Peter

/bin/ash: : No such file or directory

First, thanks for writing this!

Each time I run the startup I get the following error in the dhc-dns.log file
dhcp leases changed - reloading DNS
/bin/ash:
: No such file or directory

/bin/ash does exist.

And when I try to manually stop the services I get the following error
/usr/local/etc/rc.d/S99pollDHCP.sh: line 32: [: : integer expression expected

I'm running DSM 6.0-7321 Update 6 on a DS212J

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.