0

I have a python application where I need to be able to dynamically add an NTP server to Chrony. From the command line I can do:

sudo chronyc add server time.google.com

My understanding is that chronyc interacts with /var/run/chrony/chronyd.sock to dynamically change chronyd. Looking at the source code I think I should be doing something like:

import socket
client = socket.socket( socket.AF_UNIX, socket.SOCK_STREAM )
client.bind('/tmp/my_chrony_sock.sock')
client.connect('/var/run/chrony/chronyd.sock')
client.send(b'add server time.google.com\n')
data = client.recv(4096)

But that just hangs never receiving a response

proximous
  • 617
  • 1
  • 10
  • 28

1 Answers1

0

The Unix domain socket should be datagram, not stream. A bigger problem is that the protocol is binary. AFAIK there is no python library implementing it.

You can capture the request with tcpdump on port 323 if you call chronyc -h 127.0.0.1 add server time.google.com. You can replay the request to the Unix domain socket. The hostname is at a fixed offset in a 256-byte field. Easy to modify.

Removing a source would be more difficult as it needs to be specified by IP address. You would need to implement the chronyc sources and chronyc sourcename parts of the protocol in order to find the address corresponding to the hostname.

mlichvar
  • 21
  • 1