I need to be able to remotely shut down 2-or-more PCs from one Laptop on a LAN network. How should I go about it? (meanwhile, the network is pinging fine ... )
I need to be able to remotely shut down 2-or-more PCs from one Laptop on a LAN network. How should I go about it? (meanwhile, the network is pinging fine ... )
Hello twohot
The way I do it here is to use ssh.
In a terminal window:
That demands the password of the user you are using and logging in as. It assumes you have the same user identity/password on both machines.Code:ssh <network identity of remote machine>
Then:
to gain root control of the remote machine and finally:Code:su -
will shut the remote machine down.Code:shutdown -h now
will reboot it.Code:shutdown -r now
Hope that helps.
Well, since the PCs are not physically far from each other (less than 2 metres) and are few in number, it would take less time to reach out and press the power button (followed by "enter"). I was thinking of something more automated ... unless there is a way to issue the ssh & shutdown commands to all PCs, simultaneously.
Hello,
You can, but it would require ssh with root ID And that is not a good idea
Could setup a cron job to shutdown the servers/PC
Setting up an RSA public key you can avoid typing the password (beware that this has security implications in case someone get access to your private key).
Then on the remote machines you can set sudo to allow your non-root user tu run shutdown without password, with something like this in /etc/sudoers
To shutdown all the machines you will just runCode:user ALL=NOPASSWD: /sbin/shutdown
Code:for ip in 192.168.1.1 192.168.1.2 192.168.1.3; do ssh user@$ip -i ~/.ssh/id_rsa "sudo shutdown -h now" done
Interesting. I'll check that out.
Wondering what the first line of the "for" statement will look like for a larger network.
---------- Post added at 10:57 AM CDT ---------- Previous post was at 10:52 AM CDT ----------
... that is, if there is a way to execute a particular job at request. In that case, one should be able to access cron jobs remotely from a server. How is that done? It will be interesting to know how to schedule a shutdown at idle time ... this is one task no one has addressed to my satisfaction.
to run on the whole 192.168.0.0/16 block
if you have a hard-to-generate pattern you can put the list of ip in a text file and read from itCode:for c in `seq 0 255`; do for d in `seq 0 255`; do ip="192.168.$c.$d"; ... done done
Code:while read ip; do ... done < iplistI wonder why you would remotely activate a cron job, which as the name suggest is supposed to automatically run on scheduled times; and it would be harder to implement than just running the command with ssh.Originally Posted by twohot
To shutdown at idle time you can create a cron job with a script that runs "shutdow -h now" when some condition is true, for example
Code:avgload=$(echo "scale=0; `cat /proc/loadavg | cut -d " " -f 3`/0.01" | bc) [ $avgload -le 30 ] && shutdown -h now
Very Coool
It is a good time to learn bash tricks. Thanks ... for helping a scripting novice. Much appreciated! I'll decode at my pace ... and report back when I hit the wall
Hi People!
I finally gave your suggestions a critical look (I had little or no bash experience before). I was able to extract bits of code to construct what I needed. Here are a few of my observations:
Aesir's code ...
... Is lacking a "-t" switch for the ssh which removes the need for tty. Sudo requires tty over ssh by default, forcing the client to type passwords -- which is what we don't want. Again, for a proper RSA (keys) authentication setup the "-i" switch is not necessary after using ssh-agent. Apart from those, this is cool. Thanks Aesir!Code:for ip in 192.168.1.1 192.168.1.2 192.168.1.3; do ssh user@$ip -i ~/.ssh/id_rsa "sudo shutdown -h now" done
I found that iterating through a large network with "for-do-done" loop makes for a slow execution especially if some hosts are unavailable (negotiation time lapses).
SO, .... I'm using:
Code:while read ip; do ... done < iplist
Its fast for a network of 10-30 PCs
Hi,
A different approach: install apcupsd and run it in slave mode on the desired servers. Set up an apcupsd in master mode on your workstation. Then use the apcupsd to shut down the remote servers.
Or, more broadly speaking, any RPC client-server should do your job.
WWell,