Java JSch:JSch tty代码用于执行交互命令,我做错了什么?
我正在尝试使用JSch连接到服务器,然后执行交互式Java JSch:JSch tty代码用于执行交互命令,我做错了什么?,java,ssh,jsch,Java,Ssh,Jsch,我正在尝试使用JSch连接到服务器,然后执行交互式su命令 你能告诉我我错过了什么吗?这种东西每次都挂着,我看不出有什么理由这样做。 特别是在发送密码之前,我特别等待密码字符串(实际上是assword而不是:) 这就是我正在做的: 以user1/pass1身份登录 然后执行su-user2-c命令行 在通道输入流中传递密码 无论如何,这里是主要的执行函数,它接受user1/pass1、user2/pass2和命令行 public String execute (String host, Stri
su
命令
你能告诉我我错过了什么吗?这种东西每次都挂着,我看不出有什么理由这样做。
特别是在发送密码之前,我特别等待密码字符串(实际上是assword
而不是:
)
这就是我正在做的:
su-user2-c命令行
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;
}
看来你实施了
UIKeyboardInteractive#promptKeyboardInteractive()
返回包含“null”的字符串数组
至于sudo,我建议参考
在伙计们之间,这里有一条错误消息:com.jcraft.jsch.Buffer.putString(Buffer.java:59)com.jcraft.jsch.UserAuthKeyboardInteractive.start(UserAuthKeyboardInteractive.java:183)com.jcraft.jsch.Session.connect上的线程“main”java.lang.NullPointerException中出现异常(Session.java:442)在com.jcraft.jsch.Session.connect(Session.java:162)在susessionexecution.SUExecute.execute(SUExecute.java:53)在Tester.main(Tester.java:17)处(Tester.java:12)\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuues到1,但是,我尝试了可变长度数组并发送它们。结果是一样的。有趣的是它只运行了一次,没有给出错误。我真的看不到与计时有关的任何东西,但现在看到这种行为,我觉得似乎不得不与计时有关。谢谢,ChandanThanks,我将研究promptKeyboardInteractive黑客再次出现。从代码中可以看出,使其非交互是一种令人不快的黑客行为。在几乎所有情况下,session.setPassword(pstr)都适用于“键盘交互”auth方法。你试过了吗?@paulo,嘿,非常感谢你将问题重新编排得井井有条。看起来更清晰了。我刚刚检查了promptKeyboardInteractive(),使它只传递一个字符串(因此无法发送null),但是,这个错误仍然出现。这是一个糟糕的测试技巧。
public string[]promptKeyboardInteractive(字符串目标、字符串名称、字符串指令、字符串[]提示符、布尔[]回显){System.out.println(“\n”+prompt.length+”\n\n”);字符串[]响应=新字符串[prompt.length];响应[0]=passwd;返回响应;}
我正在尝试其他一些事情,将保持此线程的发布。我猜响应[0]为null,因为“passwd”为null。
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;
}
UIKeyboardInteractive#promptKeyboardInteractive()