I
have made a web application that shutdown all PCs in LAN in one
click. I am working in a computer lab and shutting down all PCs daily
is very irritating for me. So, I decided to develop an application
that search all PCs, which are turned on and shut them all.
My
environment is:- (prerequisite)
- I use Ubuntu 14.04 LTS on my PCs.
- All LAN PCs are using windows 7 Operating System.
- Java should be installed on server.
Since
this application is client-server based, so some setting and software will be required on both side.
1.
On client side:-
To
shut down PC from remote location you require some registry edit in
Windows 7 system.
Open
command prompt as administrator and run following command:-
reg
add HKLM\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\POLICIES\SYSTEM /V
LocalAccountTokenFilterPolicy /t REG_DWORD /d 1
OR
HKEY_LOCAL_MACHINE – SOFTWARE – Microsoft – Windows – CurrentVersion – Policies – System
Right-click
on System on
the left hand side and choose New
– DWORD (32-bit) Value.
Now your client machine is ready to shut down remotely.
2.
On server side:-
In my case, I am running
Ubuntu 14.04 and I have installed Java version “1.8.0_74”
of
Oracle.
To shut down windows PC from
Linux machine, we required to install a package samba-common. We can
install this by typing on terminal
sudo
apt-get install samba-common
After
installing this package run following command to check that
everything is working:-
net
rpc shutdown -I ipaddress -U username%password
note:-
If server is windows machine, you can find the windows command to
shutdown a remote PC.
3.
A Java program
If
remote shutdown is successful after above set up, we require a Java
program to make it automatic.
To
shut down a single machine, Java code is given below:-
import
java.io.*;
String
com="net rpc shutdown -I";
String
ip="192.168.25.52";
String
cred="-U admin%logitech";
String
command=com+" " +ip+" "+cred;
try
{
Process
process = Runtime.getRuntime().exec(command);
System.out.println("the
output stream is "+process.getOutputStream());
BufferedReader
reader=new BufferedReader( new
InputStreamReader(process.getInputStream()));
String
s;
while
((s = reader.readLine()) != null){
System.out.println("The
inout stream is " + s);
}
}
catch (IOException e) {
e.printStackTrace();
}
If
you want to shutdown all LAN PCs, code can be:-
try{
String
com="net rpc shutdown -I";
String
cred="-U admin%logitech";
byte[]
ip = {(byte)192, (byte)168, 25, 1}; // for 192.168.0.x addresses and
starting ip address
for
(int i = 1; i <=5; i++) //this will shut 1 to 5 pcs in ip range,
you can limit the loop as many pcs you want to shut
{
ip[3]
= (byte)i;
InetAddress
address = InetAddress.getByAddress(ip);
String
ipad=address.toString().substring(1);
String
command=com+" " +ipad+" "+cred;
System.out.println(command);
Process
process = Runtime.getRuntime().exec(command);
System.out.println("the
output stream is "+process.getOutputStream());
BufferedReader
reader=new BufferedReader( new
InputStreamReader(process.getInputStream()));
String
s;
while
((s = reader.readLine()) != null){
System.out.println("The
inout stream is " + s);
}
}
}
catch(Exception
e)
{
}
Either
you can implement this code on a java program or in JSP/Servlet to
shut LAN PCs.