A number of thoughts
Your script is (presumably) running as root, so that
netstat -Wpetcan run andsudo -u ${user}operation is simplified.Using a reverse connection such as
ssh -R 20202:localhost:22 centralserverI cannot get a port and user combination from thenetstat | grep | grep | cut ...line.netstat -Wpet | grep "ESTABLISHED" | grep sshd: tcp 0 36 centralserver:ssh client:37226 ESTABLISHED root 238622975 15198/sshd: roaimaAs a result I can't usefully test possible changes to your script. What are you expecting to see here?
You have a race condition, such that if a second file is changed after the
inotifywaithas completed it may not get propagated to all your target systems until another file has been changed.A fix for this might be to listen for events from a single instance of
inotifywaitand run the set ofrsynctransfers on each event. However, depending on the frequency of updates this might saturate your clients' network connectionsYou have an omission problem, in that a client connecting after a set of changes will not receive those changes until the next file change. If the updates are this critical you need to consider some way of updating the client copy immediately they have connected
You should consider a timeout for each
ssh/rsyncto the client so that if they disconnect while you're attempting a transfer you don't end up blocking everyone elseGiven a snippet of
bashcode such as this, you may be able to replacecutstatements with variable manipulation (%,#, and/operators)while read -r proto recvq sendq localaddrport foreignaddrport state user inode pidprogram name do localaddr="${localaddrport%:*}" localport="${localaddport#*localaddrport#*:}" foreignaddr="${foreignaddrport%:*}" foreignport="${foreignaddport#*foreignaddrport#*:}" pid="${pidprogram%/*}" program="${pidprogram#*/}"; program="${program%:}" echo "Foreign address = $foreignaddr and port = $foreignport" echo "PID = $pid, program = $program" echo "Name = $name" done < <(netstat -Wpet | grep '\<localhost.localdomain:.*\<ESTABLISHED\>.*/sshd:')If we could see expected output of your
netstatcommand it might be possible to useawkto simplify the line processing