我正在尝试将 JSch 连接到服务器,然后执行交互式su
命令。
你能告诉我我错过了什么吗?这些东西每次都挂起,我看不出这种行为的原因。特别是我在发送密码之前专门等待密码字符串(实际上assword
没有:
)。
这就是我正在做的事情:
- 以 user1/pass1 身份登录
- 然后执行
su - user2 -c commandline
. - 在通道输入流中传递密码。
无论如何,这是主要的执行功能,它需要 user1/pass1、user2/pass2 和 commandLine。
public String execute (String host, String nuser, String npass,
String puser, String ppass, String commandLine)
{
try{
synchronized(this)
{
session = jsch.getSession(nuser,host,22);
session.setConfig("StrictHostKeyChecking", "no");
pstr=npass;
//session.setPassword(npass);
String authmethods= session.getConfig("PreferredAuthentications");
System.out.println(authmethods);
UserInfo ui=new SUSessionExecution.UInfo();
Thread.sleep(150);
if(authmethods.contains("keyboard-interactive"))
{ System.out.print("keyboard-interactive"); session.setUserInfo(ui); }
else if ( authmethods.contains("password") )
{ System.out.print("password"); session.setPassword(pstr); }
session.connect();
channel = session.openChannel("exec");
nuser=null;
npass=null;
((ChannelExec)channel).setPty(true);
((ChannelExec)channel).setPtyType("vt100");
String command= "su - " + puser + " -c " + commandLine + "\n";
//((ChannelExec)channel).setCommand(commandLine);
byte[] cmdBuffer=command.getBytes();
ByteArrayInputStream bi = new ByteArrayInputStream(cmdBuffer);
channel.setInputStream(bi);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
((ChannelExec)channel).setErrStream(bo);
sessionOutput = channel.getInputStream();
//sessionError = channel.getExtInputStream();
channel.connect();
session_open=true;
// it is only here our session is fully functional.
boolean sustatus;//=establishSU(commandIO, channel,puser,ppass);
// NEEDS REPLACE
//commandIO.write(command.getBytes());
//commandIO.flush();
String standardOutBuffer="";
String standardErrBuffer="";
int counter;
byte[] byteBuffer = new byte[1024];
while(sessionOutput.available() > 0)
{ counter=0; //byteBuffer=null;
counter=sessionOutput.read(byteBuffer, 0, byteBuffer.length);
if(counter < 0) { throw new java.io.IOException(); }
standardOutBuffer += new String(byteBuffer,0,counter);
if(standardOutBuffer.contains("assword")){break;}
}
/*if(sessionError.available() > 0)
{ counter=0; //byteBuffer=null;
counter=sessionError.read(byteBuffer, 0, byteBuffer.length);
if(counter < 0) { throw new java.io.IOException(); }
standardErrBuffer += new String(byteBuffer,0,counter);
if(standardErrBuffer.contains("assword")){break;}}*/
commandIO = new PipedOutputStream();
sessionInput = new PipedInputStream(commandIO);
channel.setInputStream(sessionInput);
commandIO.write(new String(ppass+"\n").getBytes());
commandIO.flush();
counter=0; standardOutBuffer="";
while((counter = sessionOutput.read(byteBuffer,0,byteBuffer.length)) != -1)
{ standardOutBuffer += new String(byteBuffer,0,counter); }
closeComponents();
return standardOutBuffer;
}
}
catch(com.jcraft.jsch.JSchException jse)
{ session_open=false;su_space_open=false;jse.printStackTrace();
closeComponents(); return null; }
catch(java.io.IOException ioe)
{ session_open=false;su_space_open=false;ioe.printStackTrace();
closeComponents(); return null; }
catch(InterruptedException ie)
{ ie.printStackTrace(); }
return null;
}
这是我的错误信息:
Exception in thread "main" java.lang.NullPointerException
at com.jcraft.jsch.Buffer.putString(Buffer.java:59)
at com.jcraft.jsch.UserAuthKeyboardInteractive.start(UserAuthKeyboardInteractive.java:183)
at com.jcraft.jsch.Session.connect(Session.java:442)
at com.jcraft.jsch.Session.connect(Session.java:162)
at susessionexecution.SUExecute.execute(SUExecute.java:53)
at Tester.<init>(Tester.java:12)
at Tester.main(Tester.java:17)
这是 promptKeyboardInteractive 方法
public String[] promptKeyboardInteractive (String destination, String name,
String instruction, String[] prompt,
boolean[] echo)
{
System.out.println("\n"+prompt.length+"\n\n");
String[] response=new String[prompt.length];
response[0] = passwd;
return response;
}