Start script as root if Syncthing establish a new connection.

How to start a script as root if Syncthing establish a new connection.

System: Ubuntu 16.04 Syncthing: V0.14.25

Thanks to the GitHub crew I was able to get Information as I needed.

and use at moment a Systemd Script to periodical start the following script.

With the following script I plan to write the hosts file to allow other Programs connecting the same devices. Syncthing act as a DNS for me.

Ruby script by canton7

API_KEY = ’ ' HOST = ‘http://localhost:8384

require 'uri’ require 'net/http’ require ‘json’

uri = URI(HOST) http = Net::HTTP.new(uri.host, uri.port) headers = { ‘X-API-Key’ => API_KEY } time = Time.new

config = JSON::parse(http.get(’/rest/system/config’, headers).body) connections = JSON::parse(http.get(’/rest/system/connections’, headers).body)

config[‘devices’].each do |device| address = connections[‘connections’][device[‘deviceID’]][‘address’] puts “+#{device[‘name’]},+#{address},+” + time.strftime("%Y-%m-%d-%H")

end


And I developed a Script to modify /etc/hosts

hosts=$1 netname=$2 data=data.rb

bearbeiten=$(ruby $data|sed ‘/,+,/d’|sed ‘/172.16/d’|sed ‘/192.168/d’)

function Hinzu { for i in $bearbeiten do var=(${i//,/ })

IP_ROH=${var[1]}
HOSTNAME_SY=${var[0]/+/}
DATE_TIME_H=${var[2]}

if [ "$IP_ROH" != "+" ];
then

CUT1=${IP_ROH%:*}       # 'entferne alles bis und mit : -> Port'
CUT2=${CUT1/[/}         # 'entferne öffnende Klammer'
CUT3=${CUT2/]/}         # 'entferne schliessende klammer'
CUT4=${CUT3/%/_}        # 'ersetze sonderzeichen durch Zeichen _'
CUT5=${CUT4%_*}         # 'entferne alles bis und mit _ (Netzwerk gerät)'
CUT6=${CUT5/+/}         # 'entferne das + Zeichen'
IP=$CUT6

sed -i "/ $HOSTNAME_SY /d" "$hosts"
sed -i "/#$netname#/a $IP    $HOSTNAME_SY    # $DATE_TIME_H" "$hosts"
echo "$IP      $HOSTNAME_SY    # $DATE_TIME_H"
fi

done } Hinzu


[edit] kleiner Fehler im Script korrigiert.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.