hierynomus / hierynomus/sshj

The shell can’t be used at all. It’s hard to find such an ssh library, but the documentation is so rubbish and there are no cases.

Open
#665 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.7k
Forks
620
Avg merge
3d 23h
Merged PRs (30d)
11

Description

This is all the methods I have tried. I just want to interactively enter a string after I execute the command, and then the script I execute prints out the result of my input, but without exception, all are blocked and cannot be run at all. I looked through the issues madly, and couldn't even find a way to use the shell. There is only exec in the demo. After I execute exec, the executed script needs to read my input. How can I input it again? I can write using outputstream. After writing, I have to read the output of the script again, which he can’t do, or say I can't find any way to read it. As long as I try to read it, it will always be blocked, stuck forever, and never finish running.
```
String host = "localhost";
int port = 23;
String username = "tablo";
String password = "236375";
String cmd = "sh cmd.sh";
DefaultTerminal build = DefaultTerminal.builder()
.host(host)
.port(port)
.password(password)
.username(username)
.build();
// ShellResult shellResult = SSH2ClientUtil.writeExec(cmd,() ->"777",build);
SSHClient client = SSH2ClientUtil.getClient(build);
Session session = client.startSession();
session.allocateDefaultPTY();
Session.Command exec = session.exec(cmd);
OutputStream outputStream = exec.getOutputStream();
outputStream.write("777".getBytes(StandardCharsets.UTF_8));
outputStream.flush();
exec.join();
System.out.println(new String(exec.getInputStream().readAllBytes()));
// OutputStream outputStream = exec.getOutputStream();
// outputStream.write("777".getBytes(StandardCharsets.UTF_8));

// outputStream.flush();

//// Session.Command exec = client.startSession().exec(cmd);
// Session.Shell shell = session.startShell();
// OutputStream output = shell.getOutputStream();
// output.write("ls".getBytes(StandardCharsets.UTF_8));
// output.flush();
// InputStream inputStream = shell.getInputStream();
// System.out.println(new String(inputStream.readAllBytes()));
// inputStream.close();
// OutputStream outputStream = shell.getOutputStream();
// outputStream.write("878".getBytes(StandardCharsets.UTF_8));
// outputStream.flush();
// InputStream stream = shell.getInputStream();
// System.out.println(new String(stream.readAllBytes()));
// shell.close();
// Expect expect = new ExpectBuilder()
// .withOutput(shell.getOutputStream())
// .withInputs(shell.getInputStream(), shell.getErrorStream())
// .withInputFilters(Filters.removeColors(), Filters.removeNonPrintable())
// .withExceptionOnFailure()
// .build();
// try {
// expect.sendLine(cmd);
// expect.sendLine("777");
//
// System.out.println(expect.expect(Matchers.anyString()).getBefore());
// System.out.println(expect.expect(times(2,contains("\n"))).getResults().get(0).getBefore());
//// System.out.println(expect.expect(Matchers.regexp("[\\s\\S]*")).getInput());
//// expect.expect(contains("(Y/N)"));
//// expect.send("N");
//// expect.expect(Matchers.regexp(": $"));
//// expect.send("\b");
//// expect.expect(Matchers.regexp("\\(y\\/n\\)"));
//// expect.sendLine("y");
//// expect.expect(contains("Would you like to sign the guestbook?"));
//// expect.send("n");
//// expect.expect(contains("[RETURN]"));
//// expect.sendLine();
// } finally {
// expect.close();
// session.close();
// }

```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.