Child_Zed 2018-07-05 02:19 采纳率: 28.6%
浏览 1442
已结题

ssh连接windows 执行shell命令 在线等 急!!!!!!!!!!!!

我输入的字符串执行命令 为什么控制台出现的是 netstat /?的结果
import com.jcraft.jsch.*;

import java.io.*;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.io.IOUtils;

public class WindowProgram {

public final static int DEFAULT_SSH_PORT=22;


public Set<String> getPost(String host, String user, String password, int DEFAULT_SSH_PORT,String programPost) {

    Set<String> postList = new HashSet<String>();


    try {
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, DEFAULT_SSH_PORT);
        session.setPassword(password);
        session.setUserInfo(new MyUserInfo());
        session.connect();
        String command = "NETSTAT -aon | findstr \"1521\" ";
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        channel.setInputStream(null);
        InputStream in = channel.getInputStream();
        channel.connect();

        byte[] tmp = new byte[2048];
        while (true) {
            while (in.available() > 0) {
                String out = IOUtils.toString(in, "GBK");
                System.out.println(out);

            int i = in.read(tmp, 0, 1024);
            System.out.print(new String(tmp, 0, i));
            //System.out.println("\n");
            //System.out.println(i+"=i");
                for (int j = 0; j < tmp.length; j++) {
                    System.out.println(tmp[j]+"="+(char)tmp[j]);
    /*              if ((tmp[j] == 58) && (tmp[j + 1] != 58)&&tmp[j+1]!=48
                            && (tmp[j + 1] != 42) && (tmp[j + 2] != 58)
                            && (tmp[j + 3] != 58) && (tmp[j + 4] != 58) ) {
                        String post = (char)tmp[j + 1]+""+(char)tmp[j + 2]+""+(char)tmp[j + 3] +""+(char)tmp[j + 4]+""+(char)tmp[j + 5];
                        if(tmp[j+5]==32&&tmp[j+1]!=48){
                            post = (char)tmp[j + 1]+""+(char)tmp[j + 2]+""+(char)tmp[j + 3] +""+(char)tmp[j + 4]+"";
                        }   if(tmp[j+5]==32&&tmp[j+4]==32&&tmp[j+1]!=48){
                            post = (char)tmp[j + 1]+""+(char)tmp[j + 2]+""+(char)tmp[j + 3] +"";
                        }   if(tmp[j+5]==32 &&tmp[j+4]==32&&tmp[j+3]==32&&tmp[j+1]!=48){
                            post = (char)tmp[j + 1]+""+(char)tmp[j + 2]+"";
                        }
                        postList.add(post);
                    */  //System.out.println(post+"=post");
                    }

                //}
                //System.out.println(postList.toString());
                return postList;
            }
        }
    } catch (Exception e) {

    }
    return null;
}

private static class MyUserInfo implements UserInfo {
    @Override
    public String getPassphrase() {
        return null;
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public boolean promptPassword(String s) {
        return false;
    }

    @Override
    public boolean promptPassphrase(String s) {
        return false;
    }

    @Override
    public boolean promptYesNo(String s) {
        return true;// notice here!
    }

    @Override
    public void showMessage(String s) {
    }
}

}

  • 写回答

5条回答 默认 最新

  • ws229730 2018-07-05 03:05
    关注

    in.available() > 0 改为 >-1试试

    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波