这里执行命令的主要用的是exec函数
弹出计算器出来
import java.io.IOException;
public class WebShell {
public static void main(String[] args)throws IOException {
Runtime.getRuntime().exec("calc");
}
}
执行cmd命令,输出whoami结果
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class Ping {
public static void main(String[] args) throws IOException {
Process pro = Runtime.getRuntime().exec(new String[]{"cmd","/c","whoami"});// calc / other
InputStream in = pro.getInputStream();
Scanner sc = new Scanner(in);
while (sc.hasNextLine()){
System.out.println(sc.nextLine());
}
}
}
需要指定参数一:命令位置;参数二:-c表示先执行第一个参数;参数三:你的命令。