java如何实现远程执行dos命令?

popobeelyt 2008-11-18 10:54:09
现在在某机器部署的java程序要在另外一台windows服务器上执行一条cmd命令,请问该如何实现啊?~~~呼唤大侠~
...全文
953 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
popobeelyt 2008-11-20
  • 打赏
  • 举报
回复
谢谢楼上各位了~~
NICKXIACN 2008-11-20
  • 打赏
  • 举报
回复
用一个客户端程序,将指令传过去就可以了啊
NICKXIACN 2008-11-20
  • 打赏
  • 举报
回复
用一个客户端程序,将指令传过去就可以了啊
捡破烂攻城狮 2008-11-20
  • 打赏
  • 举报
回复
thinking in java 第四版551页

//: net/mindview/util/OSExecute.java
// Run an operating system command
// and send the output to the console.
package net.mindview.util;
import java.io.*;

public class OSExecute {
public static void command(String command) {
boolean err = false;
try {
Process process =
new ProcessBuilder(command.split(" ")).start();
BufferedReader results = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String s;
while((s = results.readLine())!= null)
System.out.println(s);
BufferedReader errors = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
// Report errors and return nonzero value
// to calling process if there are problems:
while((s = errors.readLine())!= null) {
System.err.println(s);
err = true;
}
} catch(Exception e) {
// Compensate for Windows 2000, which throws an
// exception for the default command line:
if(!command.startsWith("CMD /C"))
command("CMD /C " + command);
else
throw new RuntimeException(e);
}
if(err)
throw new OSExecuteException("Errors executing " +
command);
}
} ///:~




//: net/mindview/util/OSExecuteException.java
package net.mindview.util;

public class OSExecuteException extends RuntimeException {
public OSExecuteException(String why) { super(why); }
} ///:~
mldxs 2008-11-18
  • 打赏
  • 举报
回复
期待
SOCKAT编程
不会啊 !
jxxx2967 2008-11-18
  • 打赏
  • 举报
回复
使用Runtime类
sagezk 2008-11-18
  • 打赏
  • 举报
回复
楼主看看 Telnet 协议,http://www.cnpaf.net/class/telnet/ 然后使用 Java 自己实现或使用第三方类库。
caizhh 2008-11-18
  • 打赏
  • 举报
回复
我的JDK版本:
java version "1.6.0_04"
Java(TM) SE Runtime Environment (build 1.6.0_04-b12)
Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)
caizhh 2008-11-18
  • 打赏
  • 举报
回复
我这里有个JAVA代码用DOS命令获取MAC地址的,给你看看!

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DOSCommand {
public DOSCommand() {
}

public static String getMACAddress() {

String address = "";
String os = System.getProperty("os.name");
if (os != null && os.startsWith("Windows")) {
try {
String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("Physical Address") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {
e.printStackTrace();
}
}
return address;
}

public static void main(String[] args) {
System.out.println(""+DOSCommand.getMACAddress());
}
}
renmms 2008-11-18
  • 打赏
  • 举报
回复
利用socket通信,把写好的批处理传到服务器上,然后执行,但是好像有难度,只是个想法
gannbatte 2008-11-18
  • 打赏
  • 举报
回复
没看明白啥意思。。。。帮你顶吧~~~
_______-- 2008-11-18
  • 打赏
  • 举报
回复
socket编程啊,不太熟,呵呵,帮楼主顶了。
ZHANGBINFLY 2008-11-18
  • 打赏
  • 举报
回复
可以用socket把执行的Dos命令传到另一台计算机,然后接受到之后执行命令
shixitong 2008-11-18
  • 打赏
  • 举报
回复
学习学习!
fuyou001 2008-11-18
  • 打赏
  • 举报
回复
String command = "cmd.exe /c ipconfig /all";//这里的/c 是什么意思

Process p = Runtime.getRuntime().exec(command);

pauliuyou 2008-11-18
  • 打赏
  • 举报
回复
肯定需要在要执行程序的机器上搞个服务器程序,等待用户远程输入命令,然后再执行,
不过这不就是unix操作系统的原理么?
shenjie1989 2008-11-18
  • 打赏
  • 举报
回复
socket serversocket
cuiran 2008-11-18
  • 打赏
  • 举报
回复
File FileName=new File("D:\\sql2000\\SETUP.BAT");
String str="net stop ServerMgr\n net start ServerMgr>E:\\1.txt";
WriteFile(str);
try {
String command = "cmd.exe /c"+"start D:\\sql2000\\SETUP.BAT";
Process child = Runtime.getRuntime().exec(command);
} catch (IOException e)
{
System.out.println("文件错误");
}
sunnylyy 2008-11-18
  • 打赏
  • 举报
回复
用java模拟telnet。
night__cat 2008-11-18
  • 打赏
  • 举报
回复
socket serversocket,两台电脑都要装java环境

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧