Four Test Machines access the Internet through routers, and the routers access the Internet through dialing. After automatic dial-up, the IP address changes. You must go to the data center to view the public IP address of the router before you can connect remotely.
Initially, we plan to run the tracert www.sina.com.cn command on the machine behind the vro to find the IP address of the vro and then automatically send an email to relevant personnel. However, when the tracert command is executed on the test machine, the routing IP address cannot be tracked, and the time is short. Therefore, I think of a stupid way to access some pages that can display the requesting public IP address of the machine, then, capture the page content and send an email to everyone. This program has been implemented in Java, but it is slightly complicated. There is nothing to do over the weekend. You can use Ruby to access the webpage to obtain the IP address and then use Java to send emails. The following is a practical process:
The jdk I installed is 1.5. Therefore, you must first download the following package:
1) download the jruby package: Composer
2) download the JSR 223 related package, is: http://www.jcp.org/en/jsr/detail? Id = 1, 223,
Download the latest release, in which there are script-api.jar (JSR 223 implementation package), script-js.jar (Rhino engine implementation) and Js. jar (Rhino) Three jar files must be added with classhpath. Note: If you are using JDK 1.6, then 1.6 has been integrated with JSR 223, so there is no need to introduce additional script-api.jar and script-js.jar, JS. Jar still needs to be introduced. The relationship between the three packages is: script-api.jar is oriented to the final developer, script-js.jar is a proxy, the final script Parsing is forwarded to Js. jar, including support for other scripts, is also in this mode. For example, to support jruby, there must be a standard-based proxy.
3) download the jruby proxy.
4) download an optional auxiliary jar package, because the Code uses the stringutils class, download the commons-lang-2.4.jar package from the Apache website, also add to the classpath
Write Java code:
Import java. util .*;
Import java. Io .*;
Import javax. Script .*;
Import com. Sun. Script. jruby. jrubyscriptenginemanager;
Import org. Apache. commons. Lang. stringutils;
Import java. NiO. charset .*;
Public class main
{
Public static void main (string ARGs [])
{
System. Out. println ("----------" + system. getproperty ("file. encoding "));
Long start = system. currenttimemillis ();
// Obtain the Script Engine manager to obtain the corresponding engine, as shown in the following jruby
// Because jdk1.5 is installed, use jrubyscriptenginemanager instead of scriptengine.
Jrubyscriptenginemanager manager = new jrubyscriptenginemanager ();
// Set context parameters
Hashtable <string, string> Params = new hashtable <string, string> ();
Params. Put ("ID", "testid"); // jruby puts the request response result in the specified ID. the Java code obtains
Params. Put ("url", "http://www.google.cn"); // HTTP request address
Try {
// Obtain the Script Engine Based on the specified name "jruby"
Scriptengine jrubyengine = manager. getenginebyname ("jruby ");
Jrubyengine. Put ("Params", Params); // place parameters in context
// Execute the script in httpservice. Rb
Object value = jrubyengine. eval (New inputstreamreader (
Main. Class. getresourceasstream ("getip. RB "))
);
// Extract response results from the engine based on the specified ID
Object response = jrubyengine. Get (Params. Get ("ID "));
// If no result exists in the ID, it is obtained from the returned result when Eval is executed.
If (response = NULL | stringutils. isempty (response. tostring ())){
Response = (value = NULL? Value: value. tostring ());
}
// Output the request response result to the console
// System. Out. println (new string (response. tostring (). getbytes ("UTF-8"), "GBK "));
System. Out. println (response );
} Catch (exception e ){
E. printstacktrace ();
}
System. Out. println (system. currenttimemillis ()-Start) + "Ms ");
}
}
Note: For jdk1.5, use jrubyscriptenginemanager instead of scriptengine. Otherwise, the compilation fails.
Ruby code (getip. RB ):
Require 'net/http'
Require 'uri'
Eval (
"$ # {$ Params. get ('id')} = net: HTTP. get (URI. parse ('# {$ Params. get ('url ')}'))"
);
After Java is compiled, an error is returned when you run main. Java:
D:/script/javaexec/juby> JAVA main
---------- Gb18030
Java. Lang. illegalargumentexception: No engine registered for: jruby
This is required to run:
Java-CP .; d:/Open/jruby/sjp-1_0-fr-ri/script-api.jar; D:/Open/jruby-1.1.6/lib/BSF. jar; D:/Open/jruby/jruby-complete-1.1.6.jar; D:/Open/jruby/jruby-engine.jar; D:/Open/commons-lang-2.4 main
It can be seen that using Ruby to obtain page content is only three lines of code, which is much simpler than the previously written Java class.