本文整理汇总了Java中com.sshtools.j2ssh.session.SessionChannelClient类的典型用法代码示例。如果您正苦于以下问题:Java SessionChannelClient类的具体用法?Java SessionChannelClient怎么用?Java SessionChannelClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SessionChannelClient类属于com.sshtools.j2ssh.session包,在下文中一共展示了SessionChannelClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getActiveSession
import com.sshtools.j2ssh.session.SessionChannelClient; //导入依赖的package包/类
/**
* <p>
* Returns the active session channel of the given type.
* </p>
*
* @param type The type fo session channel
*
* @return The session channel instance
*
* @exception IOException If the session type does not exist
*
* @since 0.2.0
*/
public SessionChannelClient getActiveSession(String type)
throws IOException {
Iterator it = activeChannels.iterator();
Object obj;
while (it.hasNext()) {
obj = it.next();
if (obj instanceof SessionChannelClient) {
if (((SessionChannelClient) obj).getSessionType().equals(type)) {
return (SessionChannelClient) obj;
}
}
}
throw new IOException("There are no active " + type + " sessions");
}
示例2: openSftpChannel
import com.sshtools.j2ssh.session.SessionChannelClient; //导入依赖的package包/类
/**
* Open an SftpSubsystemChannel. For advanced use only
*
* @param eventListener
*
* @return
*
* @throws IOException
* @throws SshException
*/
public SftpSubsystemClient openSftpChannel(
ChannelEventListener eventListener) throws IOException {
SessionChannelClient session = openSessionChannel(eventListener);
SftpSubsystemClient sftp = new SftpSubsystemClient();
if (!openChannel(sftp)) {
throw new SshException("The SFTP subsystem failed to start");
}
// Initialize SFTP
if (!sftp.initialize()) {
throw new SshException(
"The SFTP Subsystem could not be initialized");
}
return sftp;
}
示例3: executeCommand
import com.sshtools.j2ssh.session.SessionChannelClient; //导入依赖的package包/类
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;
}
示例4: execute
import com.sshtools.j2ssh.session.SessionChannelClient; //导入依赖的package包/类
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());
}
}
示例5: execute
import com.sshtools.j2ssh.session.SessionChannelClient; //导入依赖的package包/类
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);
}
}
示例6: executeCommands
import com.sshtools.j2ssh.session.SessionChannelClient; //导入依赖的package包/类
/**
* 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;
}
}
示例7: setSessionChannel
import com.sshtools.j2ssh.session.SessionChannelClient; //导入依赖的package包/类
/**
*
*
* @param session
*/
public void setSessionChannel(SessionChannelClient session) {
this.session = session;
this.in = session.getInputStream();
this.out = session.getOutputStream();
session.setName(name);
}
示例8: executeSudoCommand
import com.sshtools.j2ssh.session.SessionChannelClient; //导入依赖的package包/类
/**
* 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;
}
示例9: openSessionChannel
import com.sshtools.j2ssh.session.SessionChannelClient; //导入依赖的package包/类
public SessionChannelClient openSessionChannel() throws IOException {
return client.openSessionChannel();
}
示例10: execute
import com.sshtools.j2ssh.session.SessionChannelClient; //导入依赖的package包/类
public void execute(SessionChannelClient session)
throws IllegalSpecException, InvalidSecurityContextException,
InvalidServiceContactException, TaskSubmissionException,
JobException;
示例11: SSHChannel
import com.sshtools.j2ssh.session.SessionChannelClient; //导入依赖的package包/类
public SSHChannel(SSHConnectionBundle bundle, Ssh connection, SessionChannelClient session) {
this.connection = connection;
this.session = session;
this.bundle = bundle;
}
示例12: getSession
import com.sshtools.j2ssh.session.SessionChannelClient; //导入依赖的package包/类
public SessionChannelClient getSession() {
return session;
}
示例13: main
import com.sshtools.j2ssh.session.SessionChannelClient; //导入依赖的package包/类
/**
* The main program for the PasswordConnect class
*
* @param args The command line arguments
*/
public static void main(String args[]) {
try {
// Setup a logfile
/*Handler fh = new FileHandler("example.log");
fh.setFormatter(new SimpleFormatter());
Logger.getLogger("com.sshtools").setUseParentHandlers(false);
Logger.getLogger("com.sshtools").addHandler(fh);
Logger.getLogger("com.sshtools").setLevel(Level.ALL);*/
// Configure J2SSH (This will attempt to install the bouncycastle provider
// under jdk 1.3.1)
ConfigurationLoader.initialize(false);
System.out.print("Connect to host? ");
System.out.print("Connect to host? ");
String hostname = reader.readLine();
// Make a client connection
SshClient ssh = new SshClient();
SshConnectionProperties properties = new SshConnectionProperties();
properties.setHost(hostname);
// Connect to the host
ssh.connect(properties);
// Create a password authentication instance
KBIAuthenticationClient kbi = new KBIAuthenticationClient();
// Get the users name
System.out.print("Username? ");
// Read the password
String username = reader.readLine();
kbi.setUsername(username);
kbi.setKBIRequestHandler(new KBIRequestHandler() {
public void showPrompts(String name, String instructions,
KBIPrompt[] prompts) {
System.out.println(name);
System.out.println(instructions);
String response;
if (prompts != null) {
for (int i = 0; i < prompts.length; i++) {
System.out.print(prompts[i].getPrompt() + ": ");
try {
response = reader.readLine();
prompts[i].setResponse(response);
}
catch (IOException ex) {
prompts[i].setResponse("");
ex.printStackTrace();
}
}
}
}
});
// Try the authentication
int result = ssh.authenticate(kbi);
// Evaluate the result
if (result == AuthenticationProtocolState.COMPLETE) {
// The connection is authenticated we can now do some real work!
SessionChannelClient session = ssh.openSessionChannel();
if(!session.requestPseudoTerminal("vt100", 80, 24, 0, 0, ""))
System.out.println("Failed to allocate a pseudo terminal");
if(session.startShell()) {
IOStreamConnector input =
new IOStreamConnector(System.in, session.getOutputStream());
IOStreamConnector output =
new IOStreamConnector(session.getInputStream(), System.out);
output.getState().waitForState(IOStreamConnectorState.CLOSED);
}else
System.out.println("Failed to start the users shell");
ssh.disconnect();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
示例14: main
import com.sshtools.j2ssh.session.SessionChannelClient; //导入依赖的package包/类
/**
* The main program for the PasswordConnect class
*
* @param args The command line arguments
*/
public static void main(String args[]) {
try {
// JDK > 1.4 ONLY
/*Handler fh = new FileHandler("example.log");
fh.setFormatter(new SimpleFormatter());
Logger.getLogger("com.sshtools").setUseParentHandlers(false);
Logger.getLogger("com.sshtools").addHandler(fh);
Logger.getLogger("com.sshtools").setLevel(Level.ALL);*/
// Configure J2SSH (This will attempt to install the bouncycastle provider
// under jdk 1.3.1)
ConfigurationLoader.initialize(false);
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
System.out.print("Connect to host? ");
String hostname = reader.readLine();
// Make a client connection
SshClient ssh = new SshClient();
ssh.setSocketTimeout(30000);
SshConnectionProperties properties = new SshConnectionProperties();
properties.setHost(hostname);
properties.setPrefPublicKey("ssh-dss");
// Connect to the host
ssh.connect(properties);
// Create a password authentication instance
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
// Get the users name
System.out.print("Username? ");
// Read the password
String username = reader.readLine();
pwd.setUsername(username);
// Get the password
System.out.print("Password? ");
String password = reader.readLine();
pwd.setPassword(password);
// Try the authentication
int result = ssh.authenticate(pwd);
// Evaluate the result
if (result == AuthenticationProtocolState.COMPLETE) {
// The connection is authenticated we can now do some real work!
SessionChannelClient session = ssh.openSessionChannel();
if(!session.requestPseudoTerminal("vt100", 80, 24, 0, 0, ""))
System.out.println("Failed to allocate a pseudo terminal");
if (session.startShell()) {
IOStreamConnector input =
new IOStreamConnector();
IOStreamConnector output =
new IOStreamConnector();
IOStreamConnector error =
new IOStreamConnector();
output.setCloseOutput(false);
input.setCloseInput(false);
error.setCloseOutput(false);
input.connect(System.in, session.getOutputStream());
output.connect(session.getInputStream(), System.out);
error.connect(session.getStderrInputStream(), System.out);
session.getState().waitForState(ChannelState.CHANNEL_CLOSED);
}else
System.out.println("Failed to start the users shell");
ssh.disconnect();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
示例15: hasActiveSession
import com.sshtools.j2ssh.session.SessionChannelClient; //导入依赖的package包/类
/**
* <p>
* Returns true if there is an active session channel of the specified
* type.
* </p>
*
* <p>
* When a session is created, it is assigned a default type. For instance,
* when a session is created it as a type of "uninitialized"; however when
* a shell is started on the session, the type is set to "shell". This
* also occurs for commands where the type is set to the command which is
* executed and subsystems where the type is set to the subsystem name.
* This allows each session to be saved in the active session channel's
* list and recalled later. It is also possible to set the session
* channel's type using the setSessionType method of the
* <code>SessionChannelClient</code> class.
* </p>
* <blockquote><pre>
* if(ssh.hasActiveSession("shell")) {
* SessionChannelClient session =
* ssh.getActiveSession("shell");
* }
* </pre></blockquote>
*
* @param type The string specifying the channel type
*
* @return true if an active session channel exists, otherwise false
*
* @since 0.2.0
*/
public boolean hasActiveSession(String type) {
Iterator it = activeChannels.iterator();
Object obj;
while (it.hasNext()) {
obj = it.next();
if (obj instanceof SessionChannelClient) {
if (((SessionChannelClient) obj).getSessionType().equals(type)) {
return true;
}
}
}
return false;
}