Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send post login string as SSH command when no shell session is requested. #1384

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 36 additions & 1 deletion app/src/main/java/org/connectbot/service/TerminalBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,27 @@ public void run() {
injectStringThread.start();
}

/**
* Send a noninteractive command to the transport. Used for post-login strings
* when no PTY is requested.
*/
public void sendCommandNonInteractive(final String command) {
if (command == null || command.isEmpty())
return;
Thread injectCommandThread = new Thread(new Runnable() {
@Override
public void run() {
try {
transport.sendCommand(command);
} catch (Exception e) {
Log.e(TAG, "Couldn't send command to remote host: ", e);
}
}
});
injectCommandThread.setName("InjectCommand");
injectCommandThread.start();
}

/**
* Internal method to request actual PTY terminal once we've finished
* authentication. If called before authenticated, it will just fail.
Expand Down Expand Up @@ -426,7 +447,21 @@ public void onConnected() {
setFontSize(fontSizeDp);

// finally send any post-login string, if requested
injectString(host.getPostLogin());
if (isSessionOpen()) {
// if a shell session was established, send the string as shell input ...
injectString(host.getPostLogin());
} else {
// ... otherwise inject it as a noninteractive command
sendCommandNonInteractive(host.getPostLogin());
}
}

public void onNoninteractiveSessionEstablished() {
relay = new Relay(this, transport, (vt320) buffer, host.getEncoding());
Thread relayThread = new Thread(relay);
relayThread.setDaemon(true);
relayThread.setName("Relay");
relayThread.start();
}

/**
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/org/connectbot/transport/AbsTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public static Uri getUri(String input) {
*/
public abstract void write(int c) throws IOException;

/**
* Send a command to the transport for noninteractive execution.
* @param command The command to send
* @throws IOException when there is a problem sending the command after connection
*/
public abstract void sendCommand(final String command) throws IOException;

/**
* Flushes the write commands to the transport.
* @throws IOException when there is a problem writing after connection
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/org/connectbot/transport/Local.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ public void write(int c) throws IOException {
os.write(c);
}

@Override
public void sendCommand(String command) throws IOException {
this.write(command.getBytes(host.getEncoding()));
}

public static Uri getUri(String input) {
Uri uri = Uri.parse(DEFAULT_URI);

Expand Down
29 changes: 28 additions & 1 deletion app/src/main/java/org/connectbot/transport/SSH.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public SSH(HostBean host, TerminalBridge bridge, TerminalManager manager) {
private static final int conditions = ChannelCondition.STDOUT_DATA
| ChannelCondition.STDERR_DATA
| ChannelCondition.CLOSED
| ChannelCondition.EXIT_STATUS
| ChannelCondition.EOF;

private final List<PortForwardBean> portForwards = new ArrayList<>();
Expand Down Expand Up @@ -553,7 +554,18 @@ public int read(byte[] buffer, int start, int len) throws IOException {
}
}

if ((newConditions & ChannelCondition.EOF) != 0) {
if ((newConditions & ChannelCondition.EXIT_STATUS) != 0) {
Integer exitStatus = session.getExitStatus();
close();
onDisconnect();
if (exitStatus != null && exitStatus != 0) {
throw new IOException("Remote end exited with non-zero exit code " + exitStatus);
}
// Bypass raising "connection closed" exception from CLOSED condition for exit code 0
return bytesRead;
}

if ((newConditions & ChannelCondition.CLOSED) != 0) {
close();
onDisconnect();
throw new IOException("Remote end closed connection");
Expand All @@ -574,6 +586,21 @@ public void write(int c) throws IOException {
stdin.write(c);
}

@Override
public void sendCommand(final String command) throws IOException {
if (command == null || command.isEmpty())
return;
if (session == null)
session = connection.openSession();
session.execCommand(command);

stdin = session.getStdin();
stdout = session.getStdout();
stderr = session.getStderr();

bridge.onNoninteractiveSessionEstablished();
}

@Override
public Map<String, String> getOptions() {
Map<String, String> options = new HashMap<>();
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/org/connectbot/transport/Telnet.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ public void write(int c) throws IOException {
}
}

@Override
public void sendCommand(String command) throws IOException {
this.write(command.getBytes(host.getEncoding()));
}

@Override
public void setDimensions(int columns, int rows, int width, int height) {
try {
Expand Down