gpt4 book ai didi

java - JSch:使用 JSCH tty 代码执行交互式命令我做错了什么?

转载 作者:行者123 更新时间:2023-12-01 05:16:48 27 4
gpt4 key购买 nike

我正在尝试使用 JSch 连接到服务器,然后执行交互式 su 命令。

你能告诉我我错过了什么吗?这些东西每次都会挂起,我认为这种行为没有任何理由。特别是在发送密码之前,我专门等待密码字符串(实际上是 assword ,没有 : )。

这就是我正在做的事情:

  1. 以 user1/pass1 身份登录
  2. 然后执行su - user2 -c 命令行
  3. 在 channel 输入流中传递密码。

无论如何,这是主要的执行函数,它需要 user1/pass1、user2/pass2 和命令行。

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.ja‌va: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)

这是提示键盘交互方法

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()

返回一个字符串数组,其中包含“null”。

关于sudo,我建议引用 http://www.jcraft.com/jsch/examples/Sudo.java.html

关于java - JSch:使用 JSCH tty 代码执行交互式命令我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11055115/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com