日期:2014-05-17  浏览次数:20911 次

java ssh协议连接服务器执行的部分指令无效
我现在有这样的一个问题,我的项目需要通过java程序用ssh协议连接服务器 然后从数据库读取出指令在服务器
上执行,
我用的是ganymed-ssh2-build210.jar这个包,测试连接成功
但是当执行 cd .. cd / 这样的指令时发现执行无效 因为我在这两个指令后 又加了一个 ls指令做测试
发现 还是在本目录 并没有退回到上目录 这个jar包有一个好处 它可以将正确的执行结果 和错误提示信息 分开接收到
我 发现这两条指令执行是 都没有返回执行结果 (当然 这个是指令正确执行的情况) 可是也没有返回错误的执行结果
 所以我说指令执行无效了;我在想是不是因为这个“..” 还有“/“是java里面需要转义的字符 程序将这个指令转义为
一个不会执行报错 也不会有执行效果的指令呢 
有没有哪位大侠碰到这样的情况?
我贴出我的代码:
Java code
 
package com.friendone.broadband.util;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.friendone.broadband.domain.ExecInfo;


import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class SshConnect {
  private  Connection conn;
      /** */ /**  远程机器IP  */
    private  String    ip;
      /** */ /**  用户名  */
    private  String    usr;
      /** */ /**  密码  */
    private  String    psword;
    private  String    charset  =  Charset.defaultCharset().toString();

    private  static  final  int  TIME_OUT  =  1000  *  5  *  60 ;

      /** */ /**
    * 构造函数
    *  @param  param 传入参数Bean 一些属性的getter setter 实现略
      */
      public  SshConnect(ExecInfo param)  {
        this .ip  =  param.getIp();
        this .usr  =  param.getUser();
        this .psword  =  param.getPassword();
    }

      /** */ /**
    * 构造函数
    *  @param  ip
    *  @param  usr
    *  @param  ps
      */
      public  SshConnect(String ip, String usr, String ps)  {
        this .ip  =  ip;
        this .usr  =  usr;
        this .psword  =  ps;
    }

      /** */ /**
    * 登录
    * 
    *  @return
    *  @throws  IOException
      */
      private  boolean  login()  throws  IOException  {
        conn  =  new  Connection(ip);
        conn.connect();
        return  conn.authenticateWithPassword(usr, psword);
    }

      /** */ /**
    * 执行脚本
    * 
    *  @param  cmds
    *  @return
    *  @throws  Exception
      */
      public  List <ExecInfo> exec(String cmds)  throws  Exception  { <