Java源码示例:com.sshtools.j2ssh.session.SessionChannelClient
示例1
public static String executeCommand(String command) throws Exception {
SessionChannelClient session = null;
String res = "";
try {
SshClient ssh = connectSsh();
// String command = getBundle().getString("ssh.cmd");
session = ssh.openSessionChannel();
OutputStream out = new java.io.ByteArrayOutputStream();
session = ssh.openSessionChannel();
IOStreamConnector output = new IOStreamConnector();
output.connect(session.getInputStream(), out);
if (session.executeCommand(command)) {
session.getState().waitForState(ChannelState.CHANNEL_CLOSED, 15000);
res = out.toString();
} else {
res = "Unable to execute command " + command;
}
} finally {
try {
session.close();
} catch (Exception e) {
}
}
return res;
}
示例2
public void execute(SessionChannelClient session)
throws
IllegalSpecException,
InvalidSecurityContextException,
InvalidServiceContactException,
TaskSubmissionException, JobException {
if ((get != null) && (dest == null)) {
logger.debug("You must supply a destination for the get operation");
}
if ((put != null) && (dest == null)) {
logger.debug(
"You must supply a destination and permissions for the put operation");
}
if ((get != null) && (put != null)) {
logger.debug("You cannot specify a get and put together, use seperate tasks");
}
try {
SftpSubsystemClient sftp = new SftpSubsystemClient();
if (!session.startSubsystem(sftp)) {
throw new TaskSubmissionException("Failed to start the SFTP subsystem");
}
executeSFTP(sftp);
}
catch (IOException sshe) {
logger.debug(sshe);
throw new TaskSubmissionException("SSH Connection failed: " + sshe.getMessage());
}
}
示例3
public void execute(SessionChannelClient session)
throws IllegalSpecException, InvalidSecurityContextException,
InvalidServiceContactException, TaskSubmissionException,
JobException {
try {
executeCommand(session);
session.close();
}
catch (IOException sshe) {
logger.error(sshe);
throw new TaskSubmissionException("SSH Connection failed: "
+ sshe.getMessage(), sshe);
}
}
示例4
/**
* Execute commands.
*
* @param host the host name of the server
* @param port the port number the user want to use for connection
* @param username the username required for authentication
* @param password the password required for authentication
* @param cmds the commands you want to execute remotely
* @return the string with the parameters you entered
* @throws InterruptedException
* @throws IOException, InterruptedException
*/
public static String executeCommands(String host, int port,String username,String password, String[] cmds) throws IOException, InterruptedException {
String commandOutput = "";
SshClient ssh = connect(host, port,username, password);
SessionChannelClient sessionChannel = ssh.openSessionChannel();
// make a script out of all the commands
StringBuilder cmdToExecute = new StringBuilder();
for (String cmd : cmds) {
cmdToExecute.append(cmd).append(";");
}
// execute the whole thing
if (sessionChannel.executeCommand(cmdToExecute.toString())) {
/**
* Reading from the session InputStream
*/
InputStream in = sessionChannel.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in)); // read to buffer from the stream
StringBuffer buffer = new StringBuffer();
String line;
while (((line = br.readLine()) != null)){ // read from the buffer of the stream the line
buffer.append(line); // append the result line to the String buffer.
buffer.append("\n");
}
commandOutput = buffer.toString();
sessionChannel.getState().waitForState(ChannelState.CHANNEL_CLOSED);
br.close();
ssh.disconnect();
return commandOutput;
}
else {
SSH_LOG.error("The command did not execute");
ssh.disconnect();
return commandOutput;
}
}
示例5
/**
* Get shell and execute command.
*
* @param cmd
* @return
* @throws Exception
*/
public String executeSudoCommand(String command) throws Exception {
String res = "";
SessionChannelClient session = null;
try {
SshClient ssh = connectSsh();
session = ssh.openSessionChannel();
if (session.requestPseudoTerminal("isfw", 80, 24, 0, 0, "")) {
if (session.startShell()) {
session.getOutputStream().write((command + "\n").getBytes());
InputStream in = session.getInputStream();
byte buffer[] = new byte[255];
int read;
while ((read = in.read(buffer)) > 0) {
res += new String(buffer, 0, read);
System.out.println("res: " + res);
if (res.contains("password")) {
session.getOutputStream().write((getBundle().getString("ssh.pwd") + "\n").getBytes());
res = command + "\n";
}
if (res.contains(command)) {
if (res.endsWith("]$ ")) {
break;
}
if (command.equalsIgnoreCase("status") && res.endsWith("]$ ")) {
break;
}
}
}
} else {
res = "Unable to start shell.";
}
} else {
res = "Unable to request terminal.";
}
} finally {
try {
session.close();
} catch (Exception e) {
}
}
return res;
}
示例6
public SessionChannelClient openSessionChannel() throws IOException {
return client.openSessionChannel();
}
示例7
public void execute(SessionChannelClient session)
throws IllegalSpecException, InvalidSecurityContextException,
InvalidServiceContactException, TaskSubmissionException,
JobException;
示例8
public SSHChannel(SSHConnectionBundle bundle, Ssh connection, SessionChannelClient session) {
this.connection = connection;
this.session = session;
this.bundle = bundle;
}
示例9
public SessionChannelClient getSession() {
return session;
}