本文整理汇总了Java中com.trilead.ssh2.Connection.openSession方法的典型用法代码示例。如果您正苦于以下问题:Java Connection.openSession方法的具体用法?Java Connection.openSession怎么用?Java Connection.openSession使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.trilead.ssh2.Connection
的用法示例。
在下文中一共展示了Connection.openSession方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: authenticateSshSessionWithPublicKey
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
/**
* This method will create and authenticate a session using a public key method. The
* certificate that will be used is the Autonomiccs default certificated that is installed by
* default in the Autonomiccs system VM template
*/
protected Session authenticateSshSessionWithPublicKey(Connection sshConnection) throws IOException {
sshConnection.connect(null, CONNECT_TIMEOUT, CONNECT_TIMEOUT);
if (!sshConnection.authenticateWithPublicKey("root", certificate, null)) {
throw new CloudRuntimeException(String.format("Unable to authenticate to (%s)", sshConnection.getHostname()));
}
return sshConnection.openSession();
}
示例2: communicate
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
public static CommunicateResult communicate(Connection connection, Duration timeout, String... command) throws IOException {
checkNotNull(connection);
checkNotNull(timeout);
checkNotNull(command);
String commandString = quoteCommand(asList(command));
Session session = connection.openSession();
try {
session.execCommand(commandString);
StreamGobbler stderr = new StreamGobbler(session.getStderr());
StreamGobbler stdout = new StreamGobbler(session.getStdout());
try {
session.waitForCondition(ChannelCondition.EXIT_STATUS, timeout.getMillis());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return new CommunicateResult(
commandString,
readAll(stdout, Charsets.UTF_8),
readAll(stderr, Charsets.UTF_8),
firstNonNull(session.getExitStatus(), -1000)
);
} finally {
session.close();
}
}
示例3: execCommand
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
/**
* Log in the remote host by ssh2 and execute the specified command.
*
* @param cmd
* the command will be executed list the result information
* printed in the terminal after executing the "cmd" if there
* are. errorList the error information printed in the terminal
* after executing the "cmd" if there are.
* @throws RemoteException
*
* */
public void execCommand(Connection conn, String cmd,
ArrayList<String> list, ArrayList<String> errorList) throws RemoteException {
try {
/* Create a session */
Session sess = conn.openSession();
sess.execCommand(cmd);
/*
* get the printed information of stdout and stderr.
*/
InputStream stdout = new StreamGobbler(sess.getStdout());
br = new BufferedReader(new InputStreamReader(stdout));
InputStream stderr = new StreamGobbler(sess.getStderr());
errorbr = new BufferedReader(new InputStreamReader(stderr));
while (true) {
line = br.readLine();
if (line == null)
break;
list.add(line);
}
while (true) {
errorline = errorbr.readLine();
if (errorline == null)
break;
errorList.add(errorline);
}
/* Close this session */
sess.close();
} catch (IOException e) {
throw new RemoteException(e.getMessage());
}
}
示例4: openConnectionSession
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
protected static Session openConnectionSession(final Connection conn) throws IOException {
return conn.openSession();
}
示例5: main
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
public static void main(String[] args)
{
String hostname = "127.0.0.1";
String username = "joe";
String password = "joespass";
try
{
/* Create a connection instance */
Connection conn = new Connection(hostname);
/* Now connect */
conn.connect();
/* Authenticate.
* If you get an IOException saying something like
* "Authentication method password not supported by the server at this stage."
* then please check the FAQ.
*/
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
/* Create a session */
Session sess = conn.openSession();
sess.execCommand("uname -a && date && uptime && who");
System.out.println("Here is some information about the remote host:");
/*
* This basic example does not handle stderr, which is sometimes dangerous
* (please read the FAQ).
*/
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
/* Show exit status, if available (otherwise "null") */
System.out.println("ExitCode: " + sess.getExitStatus());
/* Close this session */
sess.close();
/* Close the connection */
conn.close();
}
catch (IOException e)
{
e.printStackTrace(System.err);
System.exit(2);
}
}
示例6: main
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
public static void main(String[] args)
{
String hostname = "my-ssh-server";
String username = "joe";
String password = "joespass";
String proxyHost = "192.168.1.1";
int proxyPort = 3128; // default port used by squid
try
{
/* Create a connection instance */
Connection conn = new Connection(hostname);
/* We want to connect through a HTTP proxy */
conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort));
// if the proxy requires basic authentication:
// conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort, "username", "secret"));
/* Now connect (through the proxy) */
conn.connect();
/* Authenticate.
* If you get an IOException saying something like
* "Authentication method password not supported by the server at this stage."
* then please check the FAQ.
*/
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
/* Create a session */
Session sess = conn.openSession();
sess.execCommand("uname -a && date && uptime && who");
System.out.println("Here is some information about the remote host:");
/*
* This basic example does not handle stderr, which is sometimes dangerous
* (please read the FAQ).
*/
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
/* Show exit status, if available (otherwise "null") */
System.out.println("ExitCode: " + sess.getExitStatus());
/* Close this session */
sess.close();
/* Close the connection */
conn.close();
}
catch (IOException e)
{
e.printStackTrace(System.err);
System.exit(2);
}
}
示例7: openConnectionSession
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
protected static Session openConnectionSession(Connection conn) throws IOException, InterruptedException {
Session sess = conn.openSession();
return sess;
}
示例8: main
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
public static void main(String[] args) {
// Parameters
List<String> argsList = Arrays.asList(args);
Iterator<String> iter = argsList.iterator();
while (iter.hasNext()) {
String arg = iter.next();
if (arg.equals("-h")) {
host = iter.next();
}
if (arg.equals("-p")) {
password = iter.next();
}
if (arg.equals("-u")) {
url = iter.next();
}
}
if (host == null || host.equals("")) {
s_logger.info("Did not receive a host back from test, ignoring ssh test");
System.exit(2);
}
if (password == null) {
s_logger.info("Did not receive a password back from test, ignoring ssh test");
System.exit(2);
}
try {
s_logger.info("Attempting to SSH into host " + host);
Connection conn = new Connection(host);
conn.connect(null, 60000, 60000);
s_logger.info("User + ssHed successfully into host " + host);
boolean isAuthenticated = conn.authenticateWithPassword("root", password);
if (isAuthenticated == false) {
s_logger.info("Authentication failed for root with password" + password);
System.exit(2);
}
String linuxCommand = "wget " + url;
Session sess = conn.openSession();
sess.execCommand(linuxCommand);
sess.close();
conn.close();
} catch (Exception e) {
s_logger.error("SSH test fail with error", e);
System.exit(2);
}
}
示例9: run
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
@Override
public void run() {
NDC.push("Following thread has started" + Thread.currentThread().getName());
int retry = 0;
//Start copying files between machines in the network
s_logger.info("The size of the array is " + this.virtualMachines.size());
while (true) {
try {
if (retry > 0) {
s_logger.info("Retry attempt : " + retry + " ...sleeping 120 seconds before next attempt");
Thread.sleep(120000);
}
for (VirtualMachine vm : this.virtualMachines) {
s_logger.info("Attempting to SSH into linux host " + this.publicIp + " with retry attempt: " + retry);
Connection conn = new Connection(this.publicIp);
conn.connect(null, 600000, 600000);
s_logger.info("SSHed successfully into linux host " + this.publicIp);
boolean isAuthenticated = conn.authenticateWithPassword("root", "password");
if (isAuthenticated == false) {
s_logger.info("Authentication failed");
}
//execute copy command
Session sess = conn.openSession();
String fileName;
Random ran = new Random();
fileName = Math.abs(ran.nextInt()) + "-file";
String copyCommand = new String("./scpScript " + vm.getPrivateIp() + " " + fileName);
s_logger.info("Executing " + copyCommand);
sess.execCommand(copyCommand);
Thread.sleep(120000);
sess.close();
//execute wget command
sess = conn.openSession();
String downloadCommand =
new String("wget http://172.16.0.220/scripts/checkDiskSpace.sh; chmod +x *sh; ./checkDiskSpace.sh; rm -rf checkDiskSpace.sh");
s_logger.info("Executing " + downloadCommand);
sess.execCommand(downloadCommand);
Thread.sleep(120000);
sess.close();
//close the connection
conn.close();
}
} catch (Exception ex) {
s_logger.error(ex);
retry++;
if (retry == retryNum) {
s_logger.info("Performance Guest Network test failed with error " + ex.getMessage());
}
}
}
}
示例10: main
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
public static void main(String[] args)
{
String hostname = "127.0.0.1";
String username = "joe";
File keyfile = new File("~/.ssh/id_rsa"); // or "~/.ssh/id_dsa"
String keyfilePass = "joespass"; // will be ignored if not needed
try
{
/* Create a connection instance */
Connection conn = new Connection(hostname);
/* Now connect */
conn.connect();
/* Authenticate */
boolean isAuthenticated = conn.authenticateWithPublicKey(username, keyfile, keyfilePass);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
/* Create a session */
Session sess = conn.openSession();
sess.execCommand("uname -a && date && uptime && who");
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
System.out.println("Here is some information about the remote host:");
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
/* Close this session */
sess.close();
/* Close the connection */
conn.close();
}
catch (IOException e)
{
e.printStackTrace(System.err);
System.exit(2);
}
}
示例11: main
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException
{
String hostname = "somehost";
String username = "joe";
String password = "joespass";
File knownHosts = new File("~/.ssh/known_hosts");
try
{
/* Load known_hosts file into in-memory database */
if (knownHosts.exists())
database.addHostkeys(knownHosts);
/* Create a connection instance */
Connection conn = new Connection(hostname);
/* Now connect and use the SimpleVerifier */
conn.connect(new SimpleVerifier(database));
/* Authenticate */
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
/* Create a session */
Session sess = conn.openSession();
sess.execCommand("uname -a && date && uptime && who");
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
System.out.println("Here is some information about the remote host:");
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
/* Close this session */
sess.close();
/* Close the connection */
conn.close();
}
catch (IOException e)
{
e.printStackTrace(System.err);
System.exit(2);
}
}