Public Static voidTest_connect_host () throws Exception {SimpleDateFormat Date_format=NewSimpleDateFormat ("Yyyymmdd_hhmmss"); String[] Host_ip_list=NewString[] {"192.168.2.179","192.168.17.5", }; String[] Host_password_list=NewString[] {"","", }; String[] Identity_password_list=NewString[] {"","", }; int[] Host_port_list =New int[] { A,51255, }; String[] Connect_type_list=NewString[] {"Password","PublicKey", }; String Host_user="Root"; // for(inti =0; i < host_ip_list.length; i++) {String host_ip=Host_ip_list[i]; intHost_port =Host_port_list[i]; String Host_password=Host_password_list[i]; String Identity_password=Identity_password_list[i]; String Connect_type=Connect_type_list[i]; Jsch Jsch=NewJsch (); Session Session=NULL; //connect the machine to SSH and specify some kind of way /* if (Connect_type.equalsignorecase ("password")) {session = J Sch.getsession (Host_user, Mysql_host, Host_port); Session.setpassword (Host_password); } else if (Connect_type.equalsignorecase ("PublicKey")) {jsch.addidentity ("d:\\tmp\\admin_id_dsa_p Rivate.private "," "); Session = Jsch.getsession (Host_user, Mysql_host, Host_port); } else {continue; } UserInfo UI = new Localuserinfo (); Session.setuserinfo (UI); hashtable<string, string> config = new hashtable<string, string> (); Config.put ("stricthostkeychecking", "no"); Session.setconfig (config); Session.connect (); */ //Traverse all the way, success canBoolean Connectflag=false; for(intj =0; J < Connect_type_list.length; J + +) {String V_connect_type=Connect_type_list[i]; Try { if(V_connect_type.equalsignorecase ("Password") ) {Session=jsch.getsession (Host_user, Host_ip, Host_port); Session.setpassword (Host_password); } Else if(V_connect_type.equalsignorecase ("PublicKey") {jsch.addidentity ("d:\\tmp\\admin_id_dsa_private.private", Identity_password); Session=jsch.getsession (Host_user, Host_ip, Host_port); } Else { Continue; } UserInfo vui=NewLocaluserinfo (); Session.setuserinfo (VUI); Hashtable<string, string> vconfig =NewHashtable<string, string>(); Vconfig.put ("stricthostkeychecking","No"); Session.setconfig (Vconfig); Session.connect (); Connectflag=true; Break; } Catch(Exception e) {//Todo:handle Exception } } if(Connectflag = =false) { Continue; } //a unified approach /*jsch.addidentity ("d:\\tmp\\admin_id_dsa_private.private"); Session = Jsch.getsession (Host_user, Host_ip, Host_port); Session.setpassword (Host_password); Properties Config = new properties (); Config.put ("stricthostkeychecking", "no"); Session.setconfig (config); Session.settimeout (60); Session.connect ();*/ //Execute CommandString Execstr ="ls-lu--full-time--time-style= ' +%f_%t ' ~"; System. out. println (Date_format.format (NewDate ()) +"\ t"+execstr); Channel Channel= Session.openchannel ("exec"); ((channelexec) channel). SetCommand (EXECSTR); InputStreaminch=Channel.getinputstream (); Channel.connect (); intNextchar; StringBuffer SB=NewStringBuffer (); while(true) { while((Nextchar =inch. read ())! =-1) {sb.append (Char) Nextchar); } if(channel.isclosed ()) {//System.out.println ("exit-status:" +//channel.getexitstatus ()); Break; } Try{Thread.Sleep ( +); } Catch(Exception ee) {ee.printstacktrace (); }} String ret=sb.tostring (); System. out. println (ret); if(Channel! =NULL) {channel.disconnect (); } if(Session! =NULL) {session.disconnect (); }} System. out. println ("======================== successfully ========================"); }
Java connects the test code of a Linux server via SSH