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 70 71 72 73 74
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.io.UnsignedInteger32;
import com.sshtools.j2ssh.session.SessionChannelClient;
import com.sshtools.j2ssh.sftp.FileAttributes;
import com.sshtools.j2ssh.sftp.SftpFile;
import com.sshtools.j2ssh.sftp.SftpFileOutputStream;
import com.sshtools.j2ssh.SftpClient;
import java.io.*;
import com.sshtools.j2ssh.configuration.ConfigurationLoader;
public class SftpConnect {
public static void main(String args[]) {
try {
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("/home/j2ssh");
}
catch (IOException ex) {
}
// Change directory
sftp.cd("/home/j2ssh");
System.out.println(sftp.pwd());
// Change the mode
sftp.chmod(777, "/home/j2ssh");
sftp.lcd("c:/");
// Upload a file
sftp.put("bilel.txt", "/home/j2ssh/bilel.txt");
// Quit
sftp.quit();
ssh.disconnect();
}}
catch (Exception e) {
e.printStackTrace();
}
}
} |
Partager