ssh client

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.regex.Pattern;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;

public class SshClient {
	BufferedReader fromServer;
	Pattern alphaNumeric = Pattern.compile("([^a-zA-z0-9])");
	OutputStream toServer;
	String lastCommand = "";
	Channel channel;

	private static final String TERMINATOR = "zDonez";

	public void connect(String username, String password, String host, int port)
			throws JSchException, IOException {
		JSch shell = new JSch();
		Session session = shell.getSession(username, host, port);
		MyUserInfo ui = new MyUserInfo();
		ui.setPassword(password);
		session.setUserInfo(ui);
		session.connect();

		channel = session.openChannel("shell");
		fromServer = new BufferedReader(new InputStreamReader(channel.getInputStream()));
		toServer = channel.getOutputStream();
		channel.connect();
		if(isConnected()){
			send("echo \"\"");
		}
	}

	public boolean isConnected() {
		// TODO Auto-generated method stub
		return (channel != null && channel.isConnected());
	}

	public void disconnect() {
		if (isConnected()) {
			channel.disconnect();
		}

	}

	public void send(String command) throws IOException {
		command += "; echo \"" + TERMINATOR + "\"\n";
		toServer.write(command.getBytes());
		lastCommand = new String(command);
	}

	public String getServerResponse() throws IOException, InterruptedException {
		StringBuilder builder = new StringBuilder();
		int count = 0;
		String line = "";
		for (int i = 0; true; i++) {

			line = fromServer.readLine();
			builder.append(line).append("\n");
			if (line.contains(TERMINATOR) && (++count > 1)) {

				break;
			}

		}
		String result = builder.toString();

		int beginIndex = result.indexOf(TERMINATOR+"\"") + ((TERMINATOR+"\"").length());
		result = result.substring(beginIndex);
		return result.replaceAll(escape(TERMINATOR), "").trim();
	}
	private String escape(String subjectString){
		return alphaNumeric.matcher(subjectString).replaceAll("\\\\$1");
	}
	private static class MyUserInfo implements UserInfo {
		private String password;

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

		}

		public String getPassphrase() {
			// TODO Auto-generated method stub
			return null;
		}

		public String getPassword() {
			// TODO Auto-generated method stub
			return password;
		}

		public boolean promptPassword(String arg0) {
			// TODO Auto-generated method stub
			return true;
		}

		public boolean promptPassphrase(String arg0) {
			// TODO Auto-generated method stub
			return true;
		}

		public boolean promptYesNo(String arg0) {
			// TODO Auto-generated method stub
			return true;
		}

		public void showMessage(String arg0) {
			// TODO Auto-generated method stub
			System.out.println(arg0);
		}

	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值