java - Avoiding password pop ups in JSch -
i want bypass password step/password pop-up. trying assign password internally upon pop-up. don't want user type again , again.
right can pass internally clicking on ok without in textfield of password , want rid of prompt
import com.jcraft.jsch.*; import java.awt.*; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import javax.swing.*; public class jumphosts { textpanel textpanel = new textpanel(); public static string my_password = "password"; public void jumphosts(final string[] arg,final string command,final textpanel textpanel) { stringbuffer resultdisplaybuffer = new stringbuffer(); swingworker sw = new swingworker(){ @override protected object doinbackground() throws exception { try{ jsch jsch = new jsch(); if(arg.length <= 1){ system.out.println("this program expects more arguments."); system.exit(-1); } session session = null; session[] sessions = new session[arg.length]; string host = arg[0]; string user = host.substring(0, host.indexof('@')); host = host.substring(host.indexof('@')+1); sessions[0] = session = jsch.getsession(user, host, 22); session.setuserinfo(new myuserinfo()); session.connect(); //resultdisplaybuffer.append("the session has been established "+user+"@"+host+"\n"); textpanel.appendtext("the session has been established "+user+"@"+host+"\n",textpanel); for(int = 1; < arg.length; i++){ host = arg[i]; user = host.substring(0, host.indexof('@')); host = host.substring(host.indexof('@')+1); int assinged_port = session.setportforwardingl(0, host, 22); //resultdisplaybuffer.append("portforwarding: "+ // "localhost:"+assinged_port+" -> "+host+":"+22+"\n"); textpanel.appendtext("portforwarding: "+ "localhost:"+assinged_port+" -> "+host+":"+22+"\n",textpanel); sessions[i] = session = jsch.getsession(user, "localhost", assinged_port); session.setuserinfo(new myuserinfo()); session.sethostkeyalias(host); session.connect(); //resultdisplaybuffer.append("the session has been established "+ // user+"@"+host+"\n"); textpanel.appendtext("the session has been established "+ user+"@"+host+"\n",textpanel); } //string command=joptionpane.showinputdialog("enter command, execed sudo", // "printenv sudo_user"); // string sudo_pass=null; // { // jtextfield passwordfield=(jtextfield)new jpasswordfield(8); // object[] ob={passwordfield}; // int result= // joptionpane.showconfirmdialog(null, // ob, // "enter password sudo", // joptionpane.ok_cancel_option); // if(result!=joptionpane.ok_option){ // system.exit(-1); // } // sudo_pass=passwordfield.gettext(); // } channel channel=session.openchannel("exec"); // man sudo // -s -s (stdin) option causes sudo read password // standard input instead of terminal device. // -p -p (prompt) option allows override default // password prompt , use custom one. ((channelexec)channel).setcommand("sudo -s -p '' "+command); inputstream in = channel.getinputstream(); outputstream out = channel.getoutputstream(); ((channelexec) channel).seterrstream(system.err); channel.connect(); out.write((my_password + "\n").getbytes()); out.flush(); byte[] tmp = new byte[1024]; while (true) { while (in.available() > 0) { int = in.read(tmp, 0, 1024); if (i < 0) break; //resultdisplaybuffer.append(new string(tmp,0,i)); textpanel.appendtext(new string(tmp,0,i),textpanel); } if (channel.isclosed()) { //resultdisplaybuffer.append(new string("exit-status: " + channel.getexitstatus())+ "\n"); textpanel.appendtext(new string("exit-status: " + channel.getexitstatus())+ "\n",textpanel); break; } try { thread.sleep(1000); } catch (exception ee) { system.out.println(ee); } } channel.disconnect(); //resultdisplaybuffer.append("disconnect\n\n"); textpanel.appendtext("disconnect\n\n",textpanel); for(int = sessions.length-1; >= 0; i--){ sessions[i].disconnect(); } } catch(exception e){ system.out.println(e); } return null; } //return !("".equals(resultdisplaybuffer.tostring())) ? resultdisplaybuffer.tostring() : "something wrong here, check code errors!"; public void done(){ try { system.out.println(get()); } catch (exception ex) { ex.printstacktrace(); } } }; sw.execute(); } public static class myuserinfo implements userinfo, uikeyboardinteractive{ public string getpassword(){ return passwd; } public boolean promptyesno(string str){ object[] options={ "yes", "no" }; int foo=joptionpane.showoptiondialog(null, str, "warning", joptionpane.default_option, joptionpane.warning_message, null, options, options[0]); return foo==0; } string passwd; //jtextfield passwordfield=(jtextfield)new jpasswordfield(20); public string getpassphrase(){ return null; } public boolean promptpassphrase(string message){ return true; } public boolean promptpassword(string message){ //object[] ob={passwordfield}; //int result= //joptionpane.showconfirmdialog(null, ob, message, // joptionpane.ok_cancel_option); // if(result==joptionpane.ok_option){ passwd= my_password;//passwordfield.gettext(); return true; //} //else{ return false; } } public void showmessage(string message){ joptionpane.showmessagedialog(null, message); } final gridbagconstraints gbc = new gridbagconstraints(0,0,1,1,1,1, gridbagconstraints.northwest, gridbagconstraints.none, new insets(0,0,0,0),0,0); private container panel; public string[] promptkeyboardinteractive(string destination, string name, string instruction, string[] prompt, boolean[] echo){ panel = new jpanel(); panel.setlayout(new gridbaglayout()); gbc.weightx = 1.0; gbc.gridwidth = gridbagconstraints.remainder; gbc.gridx = 0; panel.add(new jlabel(instruction), gbc); gbc.gridy++; gbc.gridwidth = gridbagconstraints.relative; jtextfield[] texts=new jtextfield[prompt.length]; for(int i=0; i<prompt.length; i++){ gbc.fill = gridbagconstraints.none; gbc.gridx = 0; gbc.weightx = 1; panel.add(new jlabel(prompt[i]),gbc); gbc.gridx = 1; gbc.fill = gridbagconstraints.horizontal; gbc.weighty = 1; if(echo[i]){ texts[i]=new jtextfield(20); } else{ texts[i]=new jpasswordfield(20); } panel.add(texts[i], gbc); gbc.gridy++; } if(joptionpane.showconfirmdialog(null, panel, destination+": "+name, joptionpane.ok_cancel_option, joptionpane.question_message) ==joptionpane.ok_option) { string[] response = new string[] {my_password};//=new string[prompt.length]; // for(int i=0; i<prompt.length; i++){ // response[i]=texts[i].gettext(); return response; } //string[] response = null; //return response; //} //else{ //return null; // cancel //} return null; } } }
Comments
Post a Comment