java连接ssh服务

来源:互联网 发布:指南针软件怎么注销 编辑:程序博客网 时间:2024/05/16 17:53
public class Shell {private static Logger logger = Logger.getLogger(Shell.class);private String ip;//远程主机的IP地址private String username;//远程主机的用户名private String password;//远程主机的密码private int port;//远程主机端口public static final int DEFAULT_SSH_PORT = 22;private ArrayList<String> stdout;//保存输出的内容public String getIp() {return ip;}public void setIp(String ip) {this.ip = ip;}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 int getPort() {return port;}public void setPort(int port) {this.port = port;}public ArrayList<String> getStdout() {return stdout;}public Shell() {stdout = new ArrayList<String>();}public Shell(final String ip, final String username, final String password) {this.ip = ip;this.username = username;this.password = password;stdout = new ArrayList<String>();}public int execute(final String command) {int returnCode = 0;JSch jsch = new JSch();Session session = null;try {//创建session并且打开连接,因为创建session之后要主动打开连接if (port == 0)session = jsch.getSession(username, ip, DEFAULT_SSH_PORT);elsesession = jsch.getSession(username, ip, port);session.setPassword(password);//关闭主机密钥检查,否则会导致连接失败,重要!!!Properties config = new Properties();config.put("StrictHostKeyChecking", "no");session.setConfig(config);logger.info("连接服务器" + session.getHost());session.connect();//打开通道,设置通道类型,和执行的命令Channel channel = session.openChannel("exec");ChannelExec channelExec = (ChannelExec) channel;channelExec.setCommand(command);channelExec.setInputStream(null);BufferedReader input = new BufferedReader(new InputStreamReader((channelExec.getInputStream())));channelExec.connect();logger.info("The remote command is:" + command);//接受远程服务器执行命令的结果String line = null;logger.info("stdout信息开始打印");while ((line = input.readLine()) != null) {stdout.add(line);logger.info(line);}logger.info("stdout信息打印结束");input.close();//得到returnCodeif (channelExec.isClosed())returnCode = channelExec.getExitStatus();//关闭通道channelExec.disconnect();//关闭sessionsession.disconnect();} catch (JSchException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return returnCode;}@Testpublic void run() {Shell shell = new Shell();shell.setIp("10.59.81.11");shell.setUsername("www");shell.setPassword("www");shell.setPort(22);logger.info(shell.execute("ls -l"));}}