Friday, February 02, 2007

Dead or Alive !!

Ahh this time its cool. Again a good conversion but this time I guess Java is going pretty cool. Work is to write a script (no script, program :D ) which checks the status(Dead or Alive) of Network machines. Yes an easy job for scripting language. Believe me, much easy in java as well. And here it goes:

import java.io.*;
import java.net.InetAddress;
public class MainClass {
public static void main(String[] args) throws IOException {
try {
int timeout = 2000;
BufferedReader br = new BufferedReader(new FileReader("machine.txt"));
String machineName = null;
while ((machineName = br.readLine()) != null) {
InetAddress address = InetAddress.getByName(machineName.trim());
if (address.isReachable(timeout))
System.out.println(address + " is Alive");
else
System.out.println(address + " is Dead");
}
} catch (Exception e) {
System.out.printf("Unknown host");
}
}
}

Thanks to my team member to add this reading file code. If you remove file reading lines, code is not more than 4-5 lines. machine.txt contains name of the machines like
Machine1
Machine2
Machine3
Dabba4

I have compiled this code on JDK 1.5 but it should run on the lower version of JDK's as well (hopefully).

5 comments:

Prakash Gupta said...

I have a doubt, how do you distinguish whether system is idle or dead. I mean system might just be in a sleep state. How does the function getByName works???

Bipin "3~" Upadhyay said...

@Prakash
getByName() takes the hostname, say www.code-in-my-bug.blogspot.com, and returns the IP adress of the system. [And if an ip address is supplied, be it IPv4 or IPv6, it checks for the validity.]

isReachable() does NOT guarantee the real stats of the host. It basically tries ICMP echo requests. It based on the whether the host entertains (allows) ICMP requests.
Otherwise a TCP coonection is tried with Echo (generally running on port 7).

I hope it helped.
@Bhaiya
I am in a perl mailing list and they are apparently not very happy that Mustang does not support Perl. [If that's true then it's really bad.]
One thing I wanted to ask Bhaiya is; Does Mustang provide any API to configure a scripting language of my choice?
Your code gave me an idea to embed namp's new scripting language.
That would really (I mean really really) be the deadliest thing to happen after java.io.Console :)

Vaibhav Choudhary said...

@ Prakash

I will say the same what Bipin has said but let me put it in different fashion:

System Idle and system dead are two different thing, System Dead means system is not working at all(physically off) or not properly in the LAN. Whereas System Idle means System is not running any application.

Ok, so now a good question comes how to check System Idle status(normally we can see the from Alt-Ctl-Del) from processes. But how from Java code?
Java Doesnot support any direct API for it, but there is a open source project for it : SystemInfo (package org.jdesktop.jdic.systeminfo)

https://jdic.dev.java.net/incubator/systeminfo/index.html


@Bipin:

Sorry I am afarid to say tht Java is not supporting Perl. It is supporting JavaScript as an Script Language. I hope I will make you happy after Dolpin release.

Bipin "3~" Upadhyay said...

Thanks for the "systeminfo" info Bhaiya. I'll check it sometime.
By the way, what are the other scripting languages supported in Mustang? [JS was anyways supported via Rhino, though not officially.]

Vaibhav Choudhary said...

Yeah Java 6 comes with in-build with Rhino implementation of JS. Others which are in pipeline are PHP, Phython(they named it Jython :) )and some more.

One may be of ur interest is BeanShell, which provides you the method clourse to Perl and JS.

Why dont you join the open source project of Script lang. dev. Let me give you the URL:

https://scripting.dev.java.net/

Check out others, may be something of your interest.