You can download the relay's server descriptor and read the platform
line. You can access this at the http://1.2.3.4/tor/server/authority
URL for a given relay's IP address if they have their DirPort
enabled on port 80. You can also download a network consensus document and find a relay's platform string in there. The Stem library can make this easier to script, and would also allow you download the status document over a relay/OR connection if the relay doesn't have its DirPort
enabled.
If you plan on doing this a lot, you should download these documents from the Tor Metrics website to avoid putting extra strain on the relays.