package com.unicss.ssh2;

import java.io.IOException;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;

public class SSH2Conn {
	private String hostname;
	private String username;
	private String password;
	
	public SSH2Conn(String hostname, String username, String password) {
		super();
		this.hostname = hostname;
		this.username = username;
		this.password = password;
	}
	
	public SSH2Conn(String username, String password) {
		super();
		this.hostname = "localhost";
		this.username = username;
		this.password = password;
	}

	public String getHostname() {
		return hostname;
	}

	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean executeCmd(String cmd) {
		boolean isSuc = false;
		try{
			Connection conn = new Connection(this.hostname);
			conn.connect();
			boolean isAuthenticated = conn.authenticateWithPassword(this.username, this.password);
			if (isAuthenticated == false)
				throw new IOException("Authentication failed.");
			Session sess = conn.openSession();
			sess.execCommand(cmd);
			/*InputStream stdout = new StreamGobbler(sess.getStdout());
			BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
			while (true) {
				String line = br.readLine();
				if (line == null)
					break;
				System.out.println(line);
			}*/
			//得到脚本运行成功与否的标志 ：0－成功 非0－失败
			System.out.println("ExitCode: " + sess.getExitStatus());
			if (sess.getExitStatus() == 0) {
				isSuc = true;
			}
			sess.close();
			conn.close();
		}
		catch (IOException e) {
			e.printStackTrace(System.err);
			System.exit(2);
		}
		return isSuc;
	}
}

@@ -34,27 +34,120 @@
 java.io.
-IOException
+BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader
 ;%0A%0Aimpor
@@ -201,16 +201,51 @@
 Session;
+%0Aimport ch.ethz.ssh2.StreamGobbler;
 %0A%0Apublic
@@ -1738,42 +1738,12 @@
 %0A%09%09%09
-System.out.println(%22ExitCode: %22 + 
+if (
 sess
@@ -1758,26 +1758,28 @@
 Status()
-);%0A%09%09%09if (
+ == null %7C%7C 
 sess.get
@@ -1943,16 +1943,16 @@
 2);%0A%09%09%7D%0A
-
 %09%09return
@@ -1962,10 +1962,255 @@
 Suc;%0A%09%7D%0A
+%09public static void main(String%5B%5D args) %7B%0A%09%09SSH2Conn ssh2 = new SSH2Conn(%22192.168.1.231%22, %22root%22, %22bjiamcall%22);%0A%09%09String cmd = %22scp root@192.168.1.229:/tmp/test.csv /tmp/%22;%0A%09%09boolean isSuc = ssh2.executeCmd(cmd);%0A%09%09System.out.println(isSuc);%0A%09%7D%0A
 %7D%0A

modify SSH2Conn.java