Search code examples
linuxwindowsterminalcommandshutdown

Shutting Down Windows Network Using Bash


Here's my current code, which works but is slow

for i in {1..255..1}; do
  for j in {1..255..1}; do
    ip="10.8.$i.$j"
    sudo net rpc shutdown -I $ip -U Username%Password -t 1 -f
    echo $ip
  done
done

I would like to be able to go through these IPs and attempt to shut them down. But if there is not a PC at that IP it has to wait for it to timeout before attempting the next one. So how can I find and shutdown all windows PCs on a network? (they all have the same credentials)


Solution

  • A trivial solution is to just run a pile of them in parallel:

    for i in {1..255..1}; do
      for j in {1..255..1}; do
        ip="10.8.$i.$j"
        sudo net rpc shutdown -I $ip -U Username%Password -t 1 -f &
        echo $ip
      done
      wait
    done
    

    This runs 255 at a time and waits for them all to finish. Smarter and more flexible parallelization can be had through xargs, sem or parallel if Windows supports that.