1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
Dismiss Notice

Welcome To SNBForums

SNBForums is a community for anyone who wants to learn about or discuss the latest in wireless routers, network storage and the ins and outs of building and maintaining a small network.

If you'd like to post a question, simply register and have at it!

While you're at it, please check out SmallNetBuilder for product reviews and our famous Router Charts, Ranker and plenty more!

Adding OpenVPN Client to DNS

Discussion in 'Asuswrt-Merlin' started by TechnoSwiss, Aug 9, 2018.

  1. TechnoSwiss

    TechnoSwiss New Around Here

    Joined:
    Sep 12, 2014
    Messages:
    5
    I've been searching and trying to get this working and I'm coming up blank, most of the posts I'm finding are about pushing DNS to the OpenVPN clients (that's working great) or adding hosts to the configuration file (that's also working great). What I want to do however is when my OpenVPN client connects to the router, I want the DNS updated with that clients OpenVPN address, so that other hosts on the network can resolve the client to an IP address. I've got a script running on client connect, I've tried 'dnsmasq --host-record=$common_name,$ifconfig_pool_remote_ip' but all that manages to do is create another instance of dnsmasq, not actually add the host-record into the running instance. I also tried using the addhosts option in dnsmasq config since it looks like it's supposed to read in new hosts files as they're added to the directory, but I just get an error that the option isn't supported. Is there a way to setup OpenVPN to add it's clients to dnsmasq just like the dhcp-clients on the local network (or setup the dhcp server in OpenVPN to do it?) or, is there a way to add new hosts to the running instance of dnsmasq from the script? Thanks.
     
  2. Please support SNBForums! Just click on this link before you buy something from Amazon and we'll get a small commission on anything you buy. Thanks!
  3. eibgrad

    eibgrad Occasional Visitor

    Joined:
    Feb 20, 2017
    Messages:
    34
    The correct directive which defines additional hosts is ...

    Code:
    addn-hosts=/jffs/addn-hosts
    If you add that directive to the DNSMasq configuration, then update the associated file w/ the appropriate address or host-record directive (preferred) for each OpenVPN client as they connect, you can send the following signal to the running dnsmasq instance to force it to reread that file and thus DNS resolution will work.

    Code:
    killall -HUP dnsmasq
     
  4. TechnoSwiss

    TechnoSwiss New Around Here

    Joined:
    Sep 12, 2014
    Messages:
    5
    Thanks, that's what I ended up doing, addn-hosts=/jffs/hosts.openvpn

    Then in my script that runs on OpenVPN client I did:

    Code:
    #!/bin/sh
    
    matches_in_hosts="$(grep -n "$ifconfig_pool_remote_ip\|$common_name" /jffs/hosts.openvpn | cut -f1 -d: | sort -r)"
    host_entry="$ifconfig_pool_remote_ip $common_name"
    
    if [ ! -z "$matches_in_hosts" ]
    then
        # iterate over the line numbers on which matches were found
        for line_number in ${matches_in_hosts}
        do
           printf "%d\n" "$line_number"
            # delete each line where match was found
            sed -i -e "${line_number}d" /jffs/hosts.openvpn
        done
    fi
    echo "$host_entry" | tee -a /jffs/hosts.openvpn > /dev/null
    
    kill -HUP $(pidof dnsmasq)
     
Please support SNBForums! Just click on this link before you buy something from Amazon and we'll get a small commission on anything you buy. Thanks!