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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器