永发信息网

如何用java远程登录windows

答案:1  悬赏:10  手机版
解决时间 2021-02-06 17:42
  • 提问者网友:無理詩人
  • 2021-02-05 17:00
如何用java远程登录windows
最佳答案
  • 五星知识达人网友:廢物販賣機
  • 2021-02-05 17:18
Java使用SSH远程访问Windows并执行命令
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class SSHWindows {

public static void main(String[] args) {
// TODO Auto-generated method stub
String hostname ="192.168.30.10";
String username="administrator";
String password="Talent123";
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("ruby C:\\WhatWeb-master\\whatweb --output-xml http://216.139.147.75:443/");
// System.out.println("The execute command output is:");
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while(true)
{
String line = br.readLine();
if(line==null) break;
System.out.println(line);
}
// System.out.println("Exit code "+sess.getExitStatus());
sess.close();
conn.close();
// System.out.println("Connection closed");

}catch(IOException e)
{
System.out.println("can not access the remote machine");
}
}

}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯