1, the project needs to detect several remote server parameters, poor a lot of information, decided to use this
2. Jar Package: Ganymed-ssh2-build210.jar
3, principle: Send a script command to the remote Linux server, get the information of this server
4, the code is as follows:
Public classBasic { Public Static voidMain (string[] args) {String hostname1= ""; String username1= ""; String Password1= ""; String hostname2= ""; String username2= ""; String Password2= ""; String Hostname3= ""; String Username3= ""; String Password3= ""; /*** Server 1*/Montor Montor1=Getmontor (hostname1, username1, Password1); /*** Server 2*/Montor Montor2=Getmontor (hostname2, UserName2, Password2); /*** Server 3*/Montor Montor3=Getmontor (Hostname3, Username3, Password3); System.out.println (Montor1.tostring ()); System.out.println (Montor2.tostring ()); System.out.println (Montor3.tostring ()); } Private Staticmontor getmontor (string hostname, string Username, string password) {Montor Montor=NULL; Try{Connection conn=NewConnection (hostname); Conn.connect (); Booleanisauthenticated; IsAuthenticated=Conn.authenticatewithpassword (username, password); if(IsAuthenticated = =false) Throw NewIOException ("Authentication failed."); Montor=NewMontor (); Montor.sethostname (EXEC (conn,"Hostname")); Montor.setdiskspace (EXEC (conn,"Df-h | awk ' nr==2 {print $} ' ")); Montor.setuserspace (EXEC (conn,"Df-h | awk ' nr==2 {print $} ' ")); Montor.setremainingspace (EXEC (conn,"Df-h | awk ' nr==2 {print $4} ' ")); Montor.setmemory (EXEC (conn,"Free-m | awk ' nr==2 {print $} ' ")); Montor.setusememory (EXEC (conn,"Free-m | awk ' nr==2 {print $} ' ")); Montor.setremainingmemory (EXEC (conn,"Free-m | awk ' nr==2 {print $4} ' ")); BigDecimal B1=NewBigDecimal (EXEC (conn,"Free-m | awk ' nr==2 {print $} ' ")); BigDecimal B2=NewBigDecimal (EXEC (conn,"Free-m | awk ' nr==2 {print $} ' ")); Montor.setusagememory (B1.divide (B2,2, BIGDECIMAL.ROUND_HALF_UP)) . Doublevalue ()* 100 + "%"); Conn.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } returnMontor; }; /** * * @paramConn Connection *@paramcommand Execution of SHEEL commands *@return */ Private Staticstring EXEC (Connection conn, string command) {string Data= ""; Try{Session Sess=conn.opensession (); //Execute Commandsess.execcommand (command); InputStream stdout=NewStreamgobbler (Sess.getstdout ()); BufferedReader BR=NewBufferedReader (NewInputStreamReader (stdout)); while(true) {String line=Br.readline (); if(line = =NULL) Break; Data=Line ; } br.close (); Sess.close (); } Catch(IOException e) {e.printstacktrace (system.err); System.exit (2); } returndata; }}
5, at present also need the server in the services of the connectivity, such as TOMCAT,ACTIVE-MQ and other services are hanging machine, if the lady knows, hope to inform
6, if there are other better ways to look at the well-known.
Using Ganymed-ssh2-build to get remote server parameters via SSH