1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| import java.io.InputStream;
import java.io.OutputStream;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.session.SessionChannelClient;
public class SshExample {
private static String hostname = "monServeur";
private static String username = "user";
private static String password = "password";
public static void main(String args[]) {
try {
// Further code will be added here
SshClient ssh = new SshClient();
ssh.connect(hostname);
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername(username);
pwd.setPassword(password);
int result = ssh.authenticate(pwd);
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");
SessionChannelClient session = ssh.openSessionChannel();
session.executeCommand("jeebop.sh status");
/**
* Reading from the session InputStream
*/
InputStream in = session.getInputStream();
byte buffer[] = new byte[255];
int read;
while((read = in.read(buffer)) > 0) {
String out2 = new String(buffer, 0, read);
System.out.println(out2);
}
} catch(Exception e) {
e.printStackTrace();
}
}
} |
Partager