⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 j2ssh-getting-started.htm

📁 j2ssh document for java dev
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<h2><font face="Verdana, Arial, Helvetica, sans-serif">Getting started with J2SSH</font> 
</h2>
<p> <font size="2" face="Verdana, Arial, Helvetica, sans-serif">The first thing 
  you will probably want to do is to connect to an SSH server using J2SSH. This 
  is a fairly<br>
  straightforward procedure using the SshClient class. This class provides access 
  for connecting, authenticating<br>
  and starting a session channel, which enables you to execute commands or start 
  the users shell.</font></p>
<p><code> import com.sshtools.j2ssh.SshClient;
  </code></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">First of all prepare 
  your application, in this section we will guide you through the basics so for 
  now just a simple<br>
  try/catch inside the static main method.</font></p>
<blockquote>
  <pre>
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;



public class SshExample() {
  // A buffered reader so we can request information from the user
  private static BufferedReader reader =
        new BufferedReader(new InputStreamReader(System.in));
  
  public static void main(String args[]) {
    try {
      // Further code will be added here
    
	} catch(Exception e) {
      e.printStackTrace();
    }
  }
}
</pre>
</blockquote>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">The next few sections 
  will guide you through making the initial connection, authenticating the user 
  and executing<br>
  a command or starting the users shell for a simple console based SSH application.</font></p>
  <!--pagebreak-->
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br>
  <em><strong>Making the initial connection</strong></em></font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> To create an SshClient 
  instance import the class into your implementation class file and use the following 
  code<br>
  to connect to an SSH server on the standard port 22.</font></p>
<blockquote>
  <pre>
SshClient ssh 
  = new SshClient();


System.out.print("Host to connect: ");

String hostname = reader.readLine();

ssh.connect(hostname);</pre>
</blockquote>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> When the client 
  connects to the server, the server supplies its public key for the client to 
  verify. You will see<br>
  that calling the <code>connect</code> method prompts the user within the console 
  to verify the key:</font></p>
<p><code>The host firestar 
  is currently unknown to the system<br>
  The host key fingerprint is: 1028: 69 54 9c 49 e5 92 59 40 5 66 c5 2e 9d 86 
  af ed<br>
  Do you want to allow this host key? [Yes|No|Always]:</code></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> In the default 
  implementation of the <code>connect</code> method, J2SSH reads the <em>$HOME/.ssh/known_hosts</em> 
  file to determines to which hosts connections<br>
  may be allowed. This is provided by the class ConsoleKnownHostsKeyVerification 
  and the<br>
  default behavior can be emulated by the following code:</font></p>
<blockquote>
  <pre> 
import com.sshtools.j2ssh.transport.ConsoleKnownHostsKeyVerification;


ssh.connect("firestar", new ConsoleKnownHostsKeyVerification());</pre>
</blockquote>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">When the connect 
  method returns, the protocol has been negotiated and key exchange has taken 
  place, leaving the connection ready for authenticating the user. </font></p>
<!--pagebreak-->
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong><em>Authenticating 
  the user</em></strong></font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Once the connection 
  has been completed the user is required to provide a set of credentials for 
  authentication.<br>
  All client side authentication methods are implemented using the abstract class:</font></p>
<blockquote><code>import com.sshtools.j2ssh.authentication.SshAuthenticationClient.</code></blockquote>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> To perform authentication, 
  the SshClient class provides the following method:</font></p>
<blockquote><code>public int authenticate(SshAuthenticationClient 
  auth);</code></blockquote>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">There are currently 
  five authentication methods implemented by J2SSH, 'password', 'publickey', 'keyboard-interactive' 
  and 'hostbased'. With an extra agent authentication method that performs public 
  key authentication using the J2SSH key agent.</font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> <em><strong>Password 
  Authentication</strong></em></font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> Password authentication 
  is ideal for first time users as it requires no additional configuration within 
  the SSH<br>
  client or server. The user simply supplies his username and password to the 
  client which is then transmitted over<br>
  the encrypted connection to the server. The server then checks that the given 
  password is acceptable to the native<br>
  password-authentication mechanism of the host operating system and returns the 
  result to the client.</font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">J2SSH implements 
  the 'password' authentication method with the following class:</font></p>
<blockquote><code>import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient</code></blockquote>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> Using the password 
  authentication method is straight forward; create an instance of the PasswordAuthentication<br>
  class, set the username and password and pass to the SshClient to complete the 
  authentication.</font></p>
<blockquote>
  <pre>
 /** 
  * Create a PasswordAuthenticationClient instance, set the properties
  * and pass to the SessionClient to authenticate
  */
  PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
  
  System.out.print("Username: ");
  String username = reader.readLine();
  auth.setUsername(username);
  
  System.out.print("Password: ");
  String password = reader.readLine();
  auth.setPassword(password);
  
  int result = ssh.authenticate(pwd);
  </pre>
</blockquote>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong><em>The 
  Authentication Result</em></strong></font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> When the authentication 
  method completes it returns the result of the authentication. This integer value 
  can be any<br>
  of the following three values defined in the class:</font></p>
<blockquote>
  <pre>
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
  ..
  ..
  if(result==AuthenticationProtocolState.FAILED)
     System.out.println("The authentication failed");

  if(result==AuthenticationProtocolState.PARTIAL)
     System.out.println("The authentication succeeded but another"
	                   + "authentication is required");

  if(result==AuthenticationProtocolState.COMPLETE)
     System.out.println("The authentication is complete");</pre>
</blockquote>
<!--pagebreak-->
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong><em>Retrieving 
  the available authentication Methods</em></strong></font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> It is possible 
  at any time after the connection has been established and before authentication 
  has been completed to request a list of authentication methods that can be used. 
  The getAvailableAuthMethods method returns a list of authentication method names.</font></p>
<blockquote><code>public List getAvailableAuthMethods(String 
  username);</code></blockquote>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> <em>It should 
  be noted that the SSH specification allows the server to return authentication 
  methods that are not valid<br>
  for the user.</em></font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong><em>Prompting 
  the User for Authentication Details?</em></strong></font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"> Each SshAuthenticationClient 
  implementation can optionally be set a prompt interface which allows the user 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -