use of com.jn.agileway.ssh.client.supports.command.SshCommandResponse in project agileway by fangjinuo.
the class SshClients method exec.
public static SshCommandResponse exec(@NonNull SshConnection connection, @Nullable Map<String, String> environmentVariables, @Nullable Supplier<Map<String, String>, String> environmentSettingsSupplier, @Nullable String workingDirectory, @NonNull String command, @Nullable String encoding) throws SshException {
Preconditions.checkState(connection != null && connection.isConnected() && !connection.isClosed(), "connection status invalid");
Preconditions.checkNotEmpty(command, "the command is not supplied");
Charset charset = Charsets.UTF_8;
if (Strings.isNotEmpty(encoding)) {
try {
charset = Charsets.getCharset(encoding);
} catch (Throwable ex) {
logger.warn("The encoding is invalid : {}", encoding);
}
}
final SessionedChannel channel = connection.openSession();
if (Strings.isNotEmpty(workingDirectory)) {
workingDirectory = workingDirectory.replace("\\", "/");
command = "cd " + workingDirectory + ";" + command;
}
if (Emptys.isNotEmpty(environmentVariables)) {
String envs = null;
if (environmentSettingsSupplier != null) {
envs = environmentSettingsSupplier.get(environmentVariables);
}
if (Strings.isNotEmpty(envs)) {
command = envs + command;
} else {
Collects.forEach(environmentVariables, new Consumer2<String, String>() {
@Override
public void accept(String variable, String value) {
channel.env(variable, value);
}
});
}
}
channel.exec(command);
int exitStatus = channel.getExitStatus();
SshCommandResponse response = new SshCommandResponse();
response.setExitStatus(exitStatus);
try {
if (exitStatus != 0) {
InputStream errorInputStream = channel.getErrorInputStream();
byte[] errorContent = IOs.toByteArray(errorInputStream);
String error = new String(errorContent, charset);
response.setExitErrorMessage(error);
} else {
InputStream inputStream = channel.getInputStream();
// if (inputStream.available() > 0) { // 这行代码可以避免阻塞,但又容易导致某些ssh 库获取不到任何内容
byte[] bytes = IOs.toByteArray(inputStream);
String content = new String(bytes, charset);
response.setResult(content);
// }
}
} catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
}
return response;
}
use of com.jn.agileway.ssh.client.supports.command.SshCommandResponse in project agileway by fangjinuo.
the class SshClients method getGroupIds.
/**
* 获取当前用户所属的Group的id集
*
* @param connection
* @return
*/
public static int[] getGroupIds(SshConnection connection) {
SshCommandResponse response = SshClients.exec(connection, "id -G");
if (!response.hasError()) {
final String[] groups = response.getResult().trim().split("\\s+");
final int[] groupsIds = new int[groups.length];
for (int i = 0; i < groups.length; i++) {
groupsIds[i] = Integer.parseInt(groups[i]);
}
return groupsIds;
}
return null;
}
use of com.jn.agileway.ssh.client.supports.command.SshCommandResponse in project agileway by fangjinuo.
the class SshClients_CommandLineTests method testExec.
private void testExec(SshConnectionFactory connectionFactory, AbstractSshConnectionConfig connectionConfig) throws SshException, IOException {
// pc
connectionConfig.setHost("192.168.234.128");
// connectionConfig.setHost("192.168.1.70"); //work station
connectionConfig.setPort(22);
connectionConfig.setUser("fangjinuo");
connectionConfig.setPassword("fjn13570");
SshConnection connection = connectionFactory.get(connectionConfig);
SshCommandResponse response = null;
response = SshClients.exec(connection, "route");
showResult(response);
response = SshClients.exec(connection, "ifconfig");
showResult(response);
response = SshClients.exec(connection, "ls -al");
showResult(response);
// 测试错误命令
response = SshClients.exec(connection, "ls2 -al");
showResult(response);
connection.close();
}
Aggregations