本文整理汇总了Java中com.trilead.ssh2.Connection.authenticateWithPublicKey方法的典型用法代码示例。如果您正苦于以下问题:Java Connection.authenticateWithPublicKey方法的具体用法?Java Connection.authenticateWithPublicKey怎么用?Java Connection.authenticateWithPublicKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.trilead.ssh2.Connection
的用法示例。
在下文中一共展示了Connection.authenticateWithPublicKey方法的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: authenticate
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
public static boolean authenticate( Connection connection, AuthenticationData auth ) throws IOException
{
boolean isAuthenticated = false;
if (auth.getMode().equals(AuthenticationData.Mode.KEY))
{
Logger.info("Authenticating with key file: '" + auth.getKeyFile() + "'", Level.COMM, Authenticator.class);
File rsa = new File(auth.getKeyFile());
if (rsa.exists() && rsa.canRead())
{
isAuthenticated = connection.authenticateWithPublicKey(auth.getUsername(), rsa , "");
}
else
{
Logger.warning("Cannot read RSA/DSA key: '" + auth.getKeyFile() + "'", Level.COMM, Authenticator.class);
}
if (!isAuthenticated && auth.getPassword()!=null)
{
Logger.warning("Falling back to password authentication", Level.COMM, Authenticator.class);
isAuthenticated = connection.authenticateWithPassword( auth.getUsername(), auth.getPassword() );
}
}
else
{
Logger.warning("Authenticating with password", Level.COMM, Authenticator.class);
isAuthenticated = connection.authenticateWithPassword( auth.getUsername(), auth.getPassword() );
}
if (isAuthenticated == false) throw new IOException("Authentication failed.");
return isAuthenticated;
}
示例3: bootstrap
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
private int bootstrap(Connection bootstrapConn, EC2Computer computer, PrintStream logger) throws IOException, InterruptedException, AmazonClientException {
logger.println("bootstrap()" );
boolean closeBootstrap = true;
try {
int tries = 20;
boolean isAuthenticated = false;
logger.println("Getting keypair..." );
KeyPair key = computer.getCloud().getKeyPair();
logger.println("Using key: " + key.getKeyName() + "\n" + key.getKeyFingerprint() + "\n" + key.getKeyMaterial().substring(0, 160) );
while (tries-- > 0) {
logger.println("Authenticating as " + computer.getRemoteAdmin());
isAuthenticated = bootstrapConn.authenticateWithPublicKey(computer.getRemoteAdmin(), key.getKeyMaterial().toCharArray(), "");
if (isAuthenticated) {
break;
}
logger.println("Authentication failed. Trying again...");
Thread.sleep(10000);
}
if (!isAuthenticated) {
logger.println("Authentication failed");
return FAILED;
}
closeBootstrap = false;
return SAMEUSER;
} finally {
if (closeBootstrap)
bootstrapConn.close();
}
}
示例4: connect
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
public static Connection connect(HostAndPort host, StandardUsernameCredentials credentials) throws IOException {
Connection connection = new Connection(host.getHostText(), host.getPortOrDefault(22));
connection.setTCPNoDelay(true);
connection.connect();
try {
if (credentials instanceof StandardUsernamePasswordCredentials) {
StandardUsernamePasswordCredentials passwordCredentials = (StandardUsernamePasswordCredentials) credentials;
connection.authenticateWithPassword(passwordCredentials.getUsername(), Secret.toString(passwordCredentials.getPassword()));
} else if (credentials instanceof SSHUserPrivateKey) {
SSHUserPrivateKey sshCredentials = (SSHUserPrivateKey) credentials;
checkState(sshCredentials.getPrivateKeys().size() > 0, "no private keys defined");
String username = credentials.getUsername();
String password = Secret.toString(sshCredentials.getPassphrase());
for (String privateKey : sshCredentials.getPrivateKeys()) {
if (connection.authenticateWithPublicKey(username, privateKey.toCharArray(), password)) {
break;
}
}
} else {
connection.authenticateWithNone(credentials.getUsername());
}
checkState(connection.isAuthenticationComplete(), "Authentication failed");
} catch (Throwable ex) {
connection.close();
throw Throwables.propagate(ex);
}
return connection;
}
示例5: bootstrap
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
private int bootstrap(Connection bootstrapConn, Computer computer, PrintStream logger) throws IOException, InterruptedException {
logger.println("bootstrap()" );
boolean closeBootstrap = true;
if (bootstrapConn.isAuthenticationComplete()) {
return SAMEUSER;
}
try {
int tries = 20;
boolean isAuthenticated = false;
while (tries-- > 0) {
logger.println("Authenticating as " + computer.getRemoteAdmin());
isAuthenticated = bootstrapConn.authenticateWithPublicKey(computer.getRemoteAdmin(), computer.getNode().getPrivateKey().toCharArray(), "");
if (isAuthenticated) {
break;
}
logger.println("Authentication failed. Trying again...");
Thread.sleep(10000);
}
if (!isAuthenticated) {
logger.println("Authentication failed");
return FAILED;
}
closeBootstrap = false;
return SAMEUSER;
} catch (Exception e) {
e.printStackTrace(logger);
return FAILED;
} finally {
if (closeBootstrap)
bootstrapConn.close();
}
}
示例6: SSHConnect
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
private void SSHConnect(LogWriter log,String realservername, String realserverpassword, int realserverport,
String realUsername, String realPassword,
String realproxyhost,String realproxyusername, String realproxypassword, int realproxyport,
String realkeyFilename, String realkeyPass) throws Exception
{
/* Create a connection instance */
Connection conn = new Connection(realservername,realserverport);
/* We want to connect through a HTTP proxy */
if(useproxy)
{
conn.setProxyData(new HTTPProxyData(realproxyhost, realproxyport));
/* Now connect */
// if the proxy requires basic authentication:
if(!Const.isEmpty(realproxyusername) || !Const.isEmpty(realproxypassword))
{
conn.setProxyData(new HTTPProxyData(realproxyhost, realproxyport, realproxyusername, realproxypassword));
}
}
if(timeout>0)
{
// Use timeout
conn.connect(null,0,timeout*1000);
}else
{
// Cache Host Key
conn.connect();
}
// Authenticate
boolean isAuthenticated = false;
if(publicpublickey)
{
isAuthenticated=conn.authenticateWithPublicKey(realUsername, new File(keyFilename), realkeyPass);
}else
{
isAuthenticated=conn.authenticateWithPassword(realUsername, realserverpassword);
}
if(!isAuthenticated) throw new Exception("Can not connect to ");
sshclient = new SFTPv3Client(conn);
}
示例7: SSHConnect
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
private void SSHConnect(String realservername, String realserverpassword, int realserverport,
String realUsername, String realPassword,
String realproxyhost,String realproxyusername, String realproxypassword, int realproxyport,
String realkeyFilename, String realkeyPass) throws Exception
{
/* Create a connection instance */
Connection conn = new Connection(realservername,realserverport);
/* We want to connect through a HTTP proxy */
if(useproxy)
{
conn.setProxyData(new HTTPProxyData(realproxyhost, realproxyport));
/* Now connect */
// if the proxy requires basic authentication:
if(!Const.isEmpty(realproxyusername) || !Const.isEmpty(realproxypassword))
{
conn.setProxyData(new HTTPProxyData(realproxyhost, realproxyport, realproxyusername, realproxypassword));
}
}
if(timeout>0)
{
// Use timeout
conn.connect(null,0,timeout*1000);
}else
{
// Cache Host Key
conn.connect();
}
// Authenticate
boolean isAuthenticated = false;
if(publicpublickey)
{
isAuthenticated=conn.authenticateWithPublicKey(realUsername, new File(keyFilename), realkeyPass);
}else
{
isAuthenticated=conn.authenticateWithPassword(realUsername, realserverpassword);
}
if(!isAuthenticated) throw new Exception("Can not connect to ");
sshclient = new SFTPv3Client(conn);
}
示例8: SSHConnect
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
private void SSHConnect(String realservername, String realserverpassword, int realserverport,
String realUsername, String realPassword,
String realproxyhost,String realproxyusername, String realproxypassword, int realproxyport,
String realkeyFilename, String realkeyPass) throws Exception
{
/* Create a connection instance */
Connection conn = new Connection(realservername,realserverport);
/* We want to connect through a HTTP proxy */
if(useproxy)
{
conn.setProxyData(new HTTPProxyData(realproxyhost, realproxyport));
/* Now connect */
// if the proxy requires basic authentication:
if(!Const.isEmpty(realproxyusername) || !Const.isEmpty(realproxypassword))
{
conn.setProxyData(new HTTPProxyData(realproxyhost, realproxyport, realproxyusername, realproxypassword));
}
}
if(timeout>0)
{
// Use timeout
conn.connect(null,0,timeout*1000);
}else
{
// Cache Host Key
conn.connect();
}
// Authenticate
boolean isAuthenticated = false;
if(publicpublickey)
{
isAuthenticated=conn.authenticateWithPublicKey(realUsername, new File(realkeyFilename), realkeyPass);
}else
{
isAuthenticated=conn.authenticateWithPassword(realUsername, realserverpassword);
}
if(!isAuthenticated) throw new Exception("Can not connect to ");
sshclient = new SFTPv3Client(conn);
}
示例9: SSHConnect
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
private void SSHConnect( String realservername, String realserverpassword, int realserverport,
String realUsername, String realPassword, String realproxyhost, String realproxyusername,
String realproxypassword, int realproxyport, String realkeyFilename, String realkeyPass ) throws Exception {
/* Create a connection instance */
Connection conn = new Connection( realservername, realserverport );
/* We want to connect through a HTTP proxy */
if ( useproxy ) {
conn.setProxyData( new HTTPProxyData( realproxyhost, realproxyport ) );
/* Now connect */
// if the proxy requires basic authentication:
if ( !Utils.isEmpty( realproxyusername ) || !Utils.isEmpty( realproxypassword ) ) {
conn
.setProxyData( new HTTPProxyData( realproxyhost, realproxyport, realproxyusername, realproxypassword ) );
}
}
if ( timeout > 0 ) {
// Use timeout
conn.connect( null, 0, timeout * 1000 );
} else {
// Cache Host Key
conn.connect();
}
// Authenticate
boolean isAuthenticated = false;
if ( publicpublickey ) {
isAuthenticated = conn.authenticateWithPublicKey( realUsername, new File( realkeyFilename ), realkeyPass );
} else {
isAuthenticated = conn.authenticateWithPassword( realUsername, realserverpassword );
}
if ( !isAuthenticated ) {
throw new Exception( "Can not connect to " );
}
sshclient = new SFTPv3Client( conn );
}
示例10: OpenConnection
import com.trilead.ssh2.Connection; //导入方法依赖的package包/类
public static Connection OpenConnection( String serveur, int port, String username, String password,
boolean useKey, String keyFilename, String passPhrase, int timeOut, VariableSpace space, String proxyhost,
int proxyport, String proxyusername, String proxypassword ) throws KettleException {
Connection conn = null;
char[] content = null;
boolean isAuthenticated = false;
try {
// perform some checks
if ( useKey ) {
if ( Utils.isEmpty( keyFilename ) ) {
throw new KettleException( BaseMessages.getString( SSHMeta.PKG, "SSH.Error.PrivateKeyFileMissing" ) );
}
FileObject keyFileObject = KettleVFS.getFileObject( keyFilename );
if ( !keyFileObject.exists() ) {
throw new KettleException( BaseMessages.getString( SSHMeta.PKG, "SSH.Error.PrivateKeyNotExist", keyFilename ) );
}
FileContent keyFileContent = keyFileObject.getContent();
CharArrayWriter charArrayWriter = new CharArrayWriter( (int) keyFileContent.getSize() );
try ( InputStream in = keyFileContent.getInputStream() ) {
IOUtils.copy( in, charArrayWriter );
}
content = charArrayWriter.toCharArray();
}
// Create a new connection
conn = createConnection( serveur, port );
/* We want to connect through a HTTP proxy */
if ( !Utils.isEmpty( proxyhost ) ) {
/* Now connect */
// if the proxy requires basic authentication:
if ( !Utils.isEmpty( proxyusername ) ) {
conn.setProxyData( new HTTPProxyData( proxyhost, proxyport, proxyusername, proxypassword ) );
} else {
conn.setProxyData( new HTTPProxyData( proxyhost, proxyport ) );
}
}
// and connect
if ( timeOut == 0 ) {
conn.connect();
} else {
conn.connect( null, 0, timeOut * 1000 );
}
// authenticate
if ( useKey ) {
isAuthenticated =
conn.authenticateWithPublicKey( username, content, space.environmentSubstitute( passPhrase ) );
} else {
isAuthenticated = conn.authenticateWithPassword( username, password );
}
if ( isAuthenticated == false ) {
throw new KettleException( BaseMessages.getString( SSHMeta.PKG, "SSH.Error.AuthenticationFailed", username ) );
}
} catch ( Exception e ) {
// Something wrong happened
// do not forget to disconnect if connected
if ( conn != null ) {
conn.close();
}
throw new KettleException( BaseMessages.getString( SSHMeta.PKG, "SSH.Error.ErrorConnecting", serveur, username ), e );
}
return conn;
}
示例11: 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);
}
}