本文整理汇总了Java中ch.ethz.ssh2.Connection.authenticateWithPublicKey方法的典型用法代码示例。如果您正苦于以下问题:Java Connection.authenticateWithPublicKey方法的具体用法?Java Connection.authenticateWithPublicKey怎么用?Java Connection.authenticateWithPublicKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ch.ethz.ssh2.Connection
的用法示例。
在下文中一共展示了Connection.authenticateWithPublicKey方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: SSHConnection
import ch.ethz.ssh2.Connection; //导入方法依赖的package包/类
/**
* SSHConnection authenticate using a user and and a public key
* @param hostname The host to connect to
* @param username The user name
* @param keyfile The certificate public key file
* @throws IOException
*/
public SSHConnection(String hostname, String username, File keyfile) throws IOException {
String keyfilePass = ""; // will be ignored if not needed
/* Create a connection instance */
connection = new Connection(hostname);
/* Now connect */
connection.connect();
/* Authenticate */
boolean isAuthenticated = connection.authenticateWithPublicKey(username, keyfile, keyfilePass);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
}
示例2: login
import ch.ethz.ssh2.Connection; //导入方法依赖的package包/类
/**
* Attempt to login to host
* @param host host name/ip
* @param user user name
* @param aFile key file
* @param pwd password for the key file ("" if none)
* @throws IOException
*/
public void login(String host, String user, File aFile, String pwd ) throws IOException {
setParams(host, user, pwd, aFile);
if( !aFile.exists()){
throw new IOException("Cant read key file " + aFile.getAbsolutePath());
}
conn = new Connection(host);
info = conn.connect();
if(!conn.authenticateWithPublicKey(user, aFile, pwd)){
throw new IOException("Cant authenticate with user " + user + " and password " + pwd);
}
}
示例3: main
import ch.ethz.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);
}
}