You can use the following:
netstat -rn -f inet | grep default | head -n 1 | awk '{ print $2 }'
To get the default gateway that is the preferred one on macOS, so you don't need to hard code it. scutil --dns
Will display the DNS servers/split DNS configuration as well.