Java远程获取windows服务器信息

远程获取windows服务器信息,我所使用的ssh连接,获取windows服务器信息,windows不同于Linux,想要建立ssh连接必须使用工具,我用的工具freeSSHd.exe,以下freeSSHd使用。

创建ssh连接,设置ip和端口

创建用户,赋予权限。用户创建可以选择公钥形式,并给予Shell权限。也选择PassWord需要每次登录填写密码。(我这里选择是用密码方式)

 

最后,开启服务。有时候会弹出服务已开启弹窗,需要到window任务管理器那里,先停止FreeSSHDService服务停掉,再点开始服务。如果FreeSSHDService已经停掉还开启不了,再到window任务管理器进程那里找到freeSSHd停掉,再点开始服务。

 Java方面代码,maven包(使用时,记得在windows服务器防火墙开启22端口)

 
         <dependency>
            <groupId>ch.ethz.ganymed</groupId>
            <artifactId>ganymed-ssh2</artifactId>
            <version>build210</version>
         </dependency>
    public static void main(String[] args) {
        String conn = conn(m.getIp(), m.getUserName(), m.getPassword(),
 "typeperf \"\\Network Interface(*)\\Bytes Sent/sec\" -sc 1");//发送网速
        //发送同时多条前面要加cmd /c,并且命令之间用&&分割
        String conn1 = conn(m.getIp(), m.getUserName(), m.getPassword(), 
                        "cmd /c "+"typeperf \"\\Network Interface(*)\\Bytes Sent/sec\" -sc 1&&typeperf \"\\Network Interface(*)\\Bytes Received/sec\" -sc 1");//发送网速,接受网速
        System.out.println(conn);
    }
/**
*hostname ip
*username 用户名
*password 密码
*cmd windows cmd命令
**/
 public  static String conn(String hostname,String username,String password,String cmd){
        // TODO Auto-generated method stub
        StringBuilder stringBuilder=new StringBuilder();
        try{
            //建立连接
            Connection conn= new Connection(hostname);
            //     System.out.println("set up connections");
            conn.connect();
            //利用用户名和密码进行授权
            boolean isAuthenticated = conn.authenticateWithPassword(username, password);
            if(isAuthenticated ==false)
            {
                //     	System.out.println("--------");
                throw new IOException("Authorication failed");
            }
            //打开会话
            Session sess = conn.openSession();
            //    System.out.println("cmd----");
            //执行命令
            sess.execCommand(cmd);

            //     System.out.println("The execute command output is:");
            InputStream stdout = new StreamGobbler(sess.getStdout());
//            BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

            BufferedReader br = new BufferedReader(new InputStreamReader(stdout, "gbk"));
            while(true)
            {
                String line = br.readLine();
                if(line==null) break;
//                System.out.println(line);
//                recover(line);
                stringBuilder.append(line);

            }
            //   System.out.println("Exit code "+sess.getExitStatus());
            sess.close();
            conn.close();
            //     System.out.println("Connection closed");

        }catch(Exception e) {
            System.out.println(hostname+"  ssh连接失效");
        }finally {
            return stringBuilder.toString();
        }

    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值