本文整理汇总了Java中com.sshtools.j2ssh.authentication.AuthenticationProtocolState.COMPLETE属性的典型用法代码示例。如果您正苦于以下问题:Java AuthenticationProtocolState.COMPLETE属性的具体用法?Java AuthenticationProtocolState.COMPLETE怎么用?Java AuthenticationProtocolState.COMPLETE使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类com.sshtools.j2ssh.authentication.AuthenticationProtocolState
的用法示例。
在下文中一共展示了AuthenticationProtocolState.COMPLETE属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: connect
public boolean connect(String host, String user, String password) {
try {
// Connect to host ignoring host key verification
ssh.connect(host, new IgnoreHostKeyVerification());
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername(user);
pwd.setPassword(password);
status = ssh.authenticate(pwd);
if (status == AuthenticationProtocolState.COMPLETE)
return true;
else
return false;
} catch (IOException ioe) {
System.out.println("Error in connecting to "+host+" with user "+user);
return false;
}
}
示例2: assureAuthenticatedConnection
private void assureAuthenticatedConnection() throws Exception {
boolean reEstablished = false;
if (!ssh.isConnected()) {
connect();
reEstablished = true;
}
if (reEstablished || !ssh.isAuthenticated()) {
if (ssh.authenticate(pwd)!=AuthenticationProtocolState.COMPLETE) {
throw new RuntimeException("SSH authentication error");
}
reEstablished = true;
}
if (reEstablished) {
session = null;
scp = null;
}
}
示例3: connectToLinux
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;
}
示例4: connect
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: connectKey
public boolean connectKey(String host, String user, String keyFile) {
try {
// Connect to host ignoring host key verification
ssh.connect(host, new IgnoreHostKeyVerification());
PublicKeyAuthenticationClient sshClient = new PublicKeyAuthenticationClient();
SshPrivateKeyFile sshPrivKeyFile = SshPrivateKeyFile.parse(new File(keyFile));
SshPrivateKey sshPrivKey = null;
try {
sshPrivKey = sshPrivKeyFile.toPrivateKey("");
} catch (InvalidSshKeyException iske) {
System.out.println("Wrong key or unsupported usage of passphrase.");
return false;
}
sshClient.setKey(sshPrivKey);
sshClient.setUsername(user);
status = ssh.authenticate(sshClient);
if (status == AuthenticationProtocolState.COMPLETE)
return true;
else
return false;
} catch (IOException ioe) {
System.out.println("Error in connecting to "+host+" with keyfile "+keyFile);
System.out.println(ioe);
return false;
}
}
示例6: connect
/**
* 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;
}
示例7: authenticateWithKBI
private boolean authenticateWithKBI(boolean force)
throws InvalidSecurityContextException, IOException {
final KBIAuthenticationClient kbi = new KBIAuthenticationClient();
kbi.setKBIRequestHandler(new KBIRequestHandler() {
public void showPrompts(String name, String instruction,
KBIPrompt[] prompts) {
if (prompts == null) {
return;
}
Prompt[] p = new Prompt[prompts.length];
for (int i = 0; i < prompts.length; i++) {
p[i] = new Prompt(prompts[i].getPrompt(),
Prompt.TYPE_HIDDEN_TEXT);
}
if (logger.isDebugEnabled()) {
logger.debug("Displaying credential dialog");
}
char[][] results = CredentialsDialog.showCredentialsDialog(host
+ (empty(name) ? "" : " - " + name)
+ (empty(instruction) ? "" : " - " + instruction), p);
if (results != null) {
for (int i = 0; i < prompts.length; i++) {
prompts[i].setResponse(new String(results[i]));
}
}
}
});
if (logger.isDebugEnabled()) {
logger.debug("Performing keyboard-interactive authentication");
}
if (username == null) {
promptForUsername();
}
kbi.setUsername(username);
int result = client.authenticate(kbi);
if (result == AuthenticationProtocolState.COMPLETE) {
if (logger.isDebugEnabled()) {
logger.debug("Authentication complete");
}
return true;
}
else if (result == AuthenticationProtocolState.PARTIAL) {
if (logger.isDebugEnabled()) {
logger.error("Keyboard Interactive Authentication succeeded "
+ "but further authentication required!");
}
if (force) {
throw new InvalidSecurityContextException(
"The server requested additional authentication, but none is possible.");
}
else {
return false;
}
}
else {
if (force) {
throw new InvalidSecurityContextException(
"Keyboard Interactive Authentication failed");
}
else {
return false;
}
}
}
示例8: main
/**
* 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();
}
}
示例9: main
/**
* 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();
}
}
示例10: main
/**
* 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);
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();
// Connect to the host
ssh.connect(hostname);
// Create a password authentication instance
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
// Get the users name
System.out.print("Username? ");
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!
SftpClient sftp = ssh.openSftpClient();
// Make a directory
try {
sftp.mkdir("j2ssh");
}
catch (IOException ex) {
}
// Change directory
sftp.cd("j2ssh");
System.out.println(sftp.pwd());
// Change the mode
sftp.chmod(0777, "j2ssh");
sftp.lcd("c:/");
// Upload a file
sftp.put("system.gif");
// Change the local directory
sftp.lcd("localdir");
// Download a file
sftp.get("somefile.txt", "anotherfile.txt");
// Remove a directory or file
sftp.rm("j2ssh");
// Quit
sftp.quit();
ssh.disconnect();
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
System.exit(0);
}
}
示例11: main
/**
* The main program for the PortForwarding 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);
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();
// Connect to the hos
ssh.connect(hostname);
// Create a password authentication instance
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
// Get the users name
System.out.print("Username? ");
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) {
ForwardingClient forwarding = ssh.getForwardingClient();
forwarding.addLocalForwarding("Test Local", "0.0.0.0", 8081,
"127.0.0.1", 80);
forwarding.startLocalForwarding("Test Local");
forwarding.addRemoteForwarding("Test Remote", "0.0.0.0", 8081,
"127.0.0.1", 8080);
forwarding.startRemoteForwarding("Test Remote");
}
ssh.getConnectionState().waitForState(TransportProtocolState.DISCONNECTED);
}
catch (Exception e) {
e.printStackTrace();
}
}
示例12: onMsgUserAuthRequest
private void onMsgUserAuthRequest(final SshMsgUserAuthRequest msg)
throws IOException {
if (msg.getMethodName().equals("none")) {
sendUserAuthFailure(false);
} else {
if (attempts >= (ConfigurationLoader.getConfiguration(
ServerConfiguration.class)).getMaxAuthentications()) {
// Too many authentication attempts
transport.disconnect("Too many failed authentication attempts");
} else {
// If the service is supported then perfrom the authentication
if (acceptServices.containsKey(msg.getServiceName())) {
final String method = msg.getMethodName();
if (availableAuths.contains(method)) {
final SshAuthenticationServer auth = SshAuthenticationServerFactory.newInstance(method);
serviceToStart = msg.getServiceName();
final int result = auth.authenticate(this, msg);
if (result == AuthenticationProtocolState.FAILED) {
sendUserAuthFailure(false);
} else if (result == AuthenticationProtocolState.COMPLETE) {
completedAuthentications.add(auth.getMethodName());
final ServerConfiguration sc = ConfigurationLoader.getConfiguration(ServerConfiguration.class);
for (final String required : sc.getRequiredAuthentications()) {
if (!completedAuthentications.contains(required)) {
sendUserAuthFailure(true);
return;
}
}
thread.setUsername(msg.getUsername());
sendUserAuthSuccess();
} else {
// Authentication probably returned READY as no completion
// evaluation was needed
}
} else {
sendUserAuthFailure(false);
}
} else {
sendUserAuthFailure(false);
}
attempts++;
}
}
}
示例13: openSessionChannel
/**
*
* <p>
* Open's a session channel on the remote server.
* </p>
*
* <p>
* A session channel may be used to start the user's shell, execute a
* command or start a subsystem such as SFTP.
* </p>
*
* @param eventListener an event listner interface to add to the channel
*
* @return
*
* @throws IOException
* @throws SshException
*/
public SessionChannelClient openSessionChannel(
ChannelEventListener eventListener) throws IOException {
if (authenticationState != AuthenticationProtocolState.COMPLETE) {
throw new SshException("Authentication has not been completed!");
}
SessionChannelClient session = new SessionChannelClient();
session.addEventListener(activeChannelListener);
if (!connection.openChannel(session, eventListener)) {
throw new SshException("The server refused to open a session");
}
return session;
}
示例14: authenticate
/**
* <p>
* Authenticate the user on the remote host.
* </p>
*
* <p>
* To authenticate the user, create an <code>SshAuthenticationClient</code>
* instance and configure it with the authentication details.
* </p>
* <code> PasswordAuthenticationClient pwd = new
* PasswordAuthenticationClient(); pwd.setUsername("root");
* pwd.setPassword("xxxxxxxxx"); int result = ssh.authenticate(pwd);
* </code>
*
* <p>
* The method returns a result value will one of the public static values
* defined in <code>AuthenticationProtocolState</code>. These are<br>
* <br>
* COMPLETED - The authentication succeeded.<br>
* PARTIAL - The authentication succeeded but a further authentication
* method is required.<br>
* FAILED - The authentication failed.<br>
* CANCELLED - The user cancelled authentication (can only be returned
* when the user is prompted for information.<br>
* </p>
*
* @param auth A configured SshAuthenticationClient instance ready for
* authentication
*
* @return The authentication result
*
* @exception IOException If an IO error occurs during authentication
*
* @since 0.2.0
*/
public int authenticate(SshAuthenticationClient auth)
throws IOException {
// Do the authentication
authenticationState = authentication.authenticate(auth, connection);
if ((authenticationState == AuthenticationProtocolState.COMPLETE) &&
useDefaultForwarding) {
// Use some method to synchronize forwardings on the ForwardingClient
forwarding.synchronizeConfiguration(transport.getProperties());
}
return authenticationState;
}
示例15: acceptsKey
/**
* <p>
* Determine whether a private/public key pair will be accepted for public
* key authentication.
* </p>
*
* <p>
* When using public key authentication, the signing of data could take
* some time depending upon the available machine resources. By calling
* this method, you can determine whether the server will accept a key for
* authentication by providing the public key. The server will verify the
* key against the user's authorized keys and return true should the
* public key be authorized. The caller can then proceed with the private
* key operation.
* </p>
*
* @param username The username for authentication
* @param key The public key for which authentication will be attempted
*
* @return true if the server will accept the key, otherwise false
*
* @exception IOException If an IO error occurs during the operation
* @throws SshException
*
* @since 0.2.0
*/
public boolean acceptsKey(String username, SshPublicKey key)
throws IOException {
if (authenticationState != AuthenticationProtocolState.COMPLETE) {
PublicKeyAuthenticationClient pk = new PublicKeyAuthenticationClient();
return pk.acceptsKey(authentication, username,
connection.getServiceName(), key);
} else {
throw new SshException("Authentication has been completed!");
}
}