本文整理汇总了Java中com.sshtools.j2ssh.SshClient类的典型用法代码示例。如果您正苦于以下问题:Java SshClient类的具体用法?Java SshClient怎么用?Java SshClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SshClient类属于com.sshtools.j2ssh包,在下文中一共展示了SshClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: connectSsh
import com.sshtools.j2ssh.SshClient; //导入依赖的package包/类
/**
* connect to host
*
* @return
* @throws Exception
*/
private static SshClient connectSsh() throws Exception {
SshClient ssh = new SshClient();
HostKeyVerification host = new IgnoreHostKeyVerification();
String hostStr = getBundle().getString("ssh.host");
ssh.connect(hostStr, host);
PasswordAuthenticationClient auth = new PasswordAuthenticationClient();
auth.setUsername(getBundle().getString("ssh.user"));
auth.setPassword(getBundle().getString("ssh.pwd"));
int result = ssh.authenticate(auth);
System.out.println("Status " + result);
if ((result == AuthenticationProtocolState.CANCELLED) || (result == AuthenticationProtocolState.FAILED)) {
throw new Exception("Authentication Error.");
}
return ssh;
}
示例2: connectToLinux
import com.sshtools.j2ssh.SshClient; //导入依赖的package包/类
public boolean connectToLinux(String ipAddress) {
boolean isConnect = false;
try {
SshClient client = new SshClient();
client.connect(ipAddress, 22);// IP�Ͷ˿�
// �����û���������
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername("root");
pwd.setPassword("chenzhao");
int result = client.authenticate(pwd);
if (result == AuthenticationProtocolState.COMPLETE) {// ����������
isConnect = true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return isConnect;
}
示例3: makeConnection
import com.sshtools.j2ssh.SshClient; //导入依赖的package包/类
/***********************************************************************/
public synchronized Connection makeConnection(String database, DatabaseConfiguration originalConfiguration)
{
int port = counter++;
DatabaseConfiguration config = new DatabaseConfiguration(originalConfiguration.getDataSourceName(), originalConfiguration.getDriver(), originalConfiguration.getProtocol(), "localhost", "" + port, database, originalConfiguration.getUserName(), originalConfiguration.getPassword(), originalConfiguration.getType());
try
{
LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
SshClient ssh = new SshClient();
ssh.setSocketTimeout(60000);
ssh.connect(originalConfiguration.getServer(), new IgnoreHostKeyVerification());
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername(originalConfiguration.getUserName());
pwd.setPassword(originalConfiguration.getPassword());
ssh.authenticate(pwd);
ForwardingClient client = ssh.getForwardingClient();
client.addLocalForwarding(config.getProtocol(), "0.0.0.0", config.getPort(), "localhost", originalConfiguration.getPort());
client.startLocalForwarding(config.getProtocol());
return new SshConnection(ssh, config.makeConnection());
}
catch (IOException ie)
{
throw ObjectUtils.throwAsError(ie);
}
}
示例4: connect
import com.sshtools.j2ssh.SshClient; //导入依赖的package包/类
public void connect() throws IOException {
lia.gsi.ssh.GSIAuthenticationClient gsiAuth = null;
try {
gsiAuth = new lia.gsi.ssh.GSIAuthenticationClient();
gsiAuth.setUsername(username);
} catch (GSSException e) {
throw new IOException("Cannot load grid credentials.");
}
conn = new SshClient();
SshToolsConnectionProfile properties = new SshToolsConnectionProfile();
// TODO: add new "port" parameter
properties.setPort(port);
properties.setForwardingAutoStartMode(false);
properties.setHost(hostname);
properties.setUsername(username);
conn.setUseDefaultForwarding(false);
conn.connect(properties);
try {
// Authenticate the user
int result = conn.authenticate(gsiAuth, hostname);
if (result != AuthenticationProtocolState.COMPLETE) {
throw new IOException("GSI authentication failed");
}
// Open a session channel
sess = conn.openSessionChannel();
sess.requestPseudoTerminal("javash", 0, 0, 0, 0, "");
} catch (Throwable t) {
throw new IOException(t.getMessage());
}
}
示例5: executeCommand
import com.sshtools.j2ssh.SshClient; //导入依赖的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;
}
示例6: SshConnection
import com.sshtools.j2ssh.SshClient; //导入依赖的package包/类
private SshConnection(String host, String user, String pass) throws Exception {
ssh = new SshClient();
properties = new SshConnectionProperties();
properties.setHost(host);
pwd = new PasswordAuthenticationClient();
pwd.setUsername(user);
pwd.setPassword(pass);
assureAuthenticatedConnection();
}
示例7: connect
import com.sshtools.j2ssh.SshClient; //导入依赖的package包/类
/**
* Connect. This action simulates all the actions required for a SSH connection
*
* @param host the hostname of the host you want to connect to
* @param port the port of the host you want to connect to
* @param username the username required for authentication
* @param password the password required for authentication
* @return the ssh client you connected to
* @throws IOException Signals that an I/O exception has occurred.
*/
public static SshClient connect(String host, int port,String username,String password) throws IOException {
SSH_LOG.info("Connecting to " + host);
SshClient ssh = new SshClient();
ssh.connect(host, port, new IgnoreHostKeyVerification());
PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient();
passwordAuthenticationClient.setUsername(username);
passwordAuthenticationClient.setPassword(password);
int result = ssh.authenticate(passwordAuthenticationClient);
if (result != AuthenticationProtocolState.COMPLETE) {
throw new IOException("Login to " + host + ":" + port + " "+ username + "/" + password + " failed");
}
SSH_LOG.info("Connected " + host);
return ssh;
}
示例8: executeCommands
import com.sshtools.j2ssh.SshClient; //导入依赖的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;
}
}
示例9: sftpUpload
import com.sshtools.j2ssh.SshClient; //导入依赖的package包/类
/***********************************************************************/
public static void sftpUpload(FTPConfig config, File file, String remoteFileName) throws IOException
{
SshClient ssh = new SshClient();
SftpClient sftp = sshLogin(config, ssh);
sftp.mkdirs(remoteFileName.substring(0, remoteFileName.lastIndexOf("/")));
sftp.put(new FileInputStream(file), remoteFileName);
sftp.quit();
ssh.disconnect();
}
示例10: sshLogin
import com.sshtools.j2ssh.SshClient; //导入依赖的package包/类
/************************************************************************/
private static SftpClient sshLogin(FTPConfig config, SshClient ssh) throws IOException
{
ssh.setSocketTimeout(60000);
ssh.connect(config.host, new IgnoreHostKeyVerification());
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername(config.userName);
pwd.setPassword(config.password);
ssh.authenticate(pwd);
SftpClient sftp = ssh.openSftpClient();
return sftp;
}
示例11: sftpDownload
import com.sshtools.j2ssh.SshClient; //导入依赖的package包/类
/************************************************************************/
public static File sftpDownload(FTPConfig config, File file, String remoteFileName) throws IOException
{
SshClient ssh = new SshClient();
SftpClient sftp = sshLogin(config, ssh);
sftp.get(remoteFileName, new FileOutputStream(file));
sftp.quit();
ssh.disconnect();
return file;
}
示例12: connect
import com.sshtools.j2ssh.SshClient; //导入依赖的package包/类
/**
*
*
* @param ssh
* @param profile
*
* @throws IOException
*/
public void connect(SshClient ssh, SshToolsConnectionProfile profile)
throws IOException {
this.ssh = ssh;
if (!ssh.isAuthenticated()) {
authenticateUser(false);
}
// Set the current connection properties
setCurrentConnectionProfile(profile);
authenticationComplete(false);
}
示例13: executeSudoCommand
import com.sshtools.j2ssh.SshClient; //导入依赖的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;
}
示例14: SSHManager
import com.sshtools.j2ssh.SshClient; //导入依赖的package包/类
public SSHManager() {
ssh = new SshClient();
Logger.getLogger("com.sshtools").setLevel(Level.WARNING);
}
示例15: disConnect
import com.sshtools.j2ssh.SshClient; //导入依赖的package包/类
/** Disconnect
* @param ssh
*/
public static void disConnect(SshClient ssh) {
if(ssh.isConnected()){
ssh.disconnect();
}
}