2009年6月10日

SSHTools

SSHTools
Docjar Implements an SSH client

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.sshtools.j2ssh.ScpClient;
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.connection.ChannelOutputStream;
import com.sshtools.j2ssh.session.SessionChannelClient;
import com.sshtools.j2ssh.transport.IgnoreHostKeyVerification;

public class Main {
public static void main(String args[]) throws IOException {
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername("user");
pwd.setPassword("password");

com.sshtools.j2ssh.SshClient ssh = new SshClient();
ssh.connect("hostname", new IgnoreHostKeyVerification());
try {
int result = ssh.authenticate(pwd);
if (result == AuthenticationProtocolState.COMPLETE) {
SessionChannelClient session = ssh.openSessionChannel();
ScpClient scpc = ssh.openScpClient();
// get file
scpc.get("local.bashrc.backup", ".bashrc", false);
if (session.requestPseudoTerminal("utf-8", 80, 24, 0, 0, "")) {
if (session.startShell()) {
InputStreamReader is = new InputStreamReader(session.getInputStream());
BufferedReader br = new BufferedReader(is);
ChannelOutputStream os = session.getOutputStream();
os.write("ls -al;exit\n".getBytes());
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
os.close();
br.close();
}
}
session.close();
}
} finally {
ssh.disconnect();
}
}
}

沒有留言:

網誌存檔