java开发中壳,需要从java代码运行内壳命令

While running below mentioned code in windows it work if we pass the date command and then pass parameter. But for FTP it can't put next command as it is going to FTP prompt and process is just waiting there. I need a way to pass another command on same runtime instance.

===================================

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

public class TestCommand {

public static void main(String[] args) {

int exitValue = -1;

String command = "cmd /c ftp";//"cmd /c date";//

String now = "bye"; //"05-01-2018";//

byte[] b = null;

try {

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

BufferedWriter writer = new BufferedWriter(

new OutputStreamWriter(process.getOutputStream()));

writer.write("help");

writer.flush();

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

System.out.println("After BufferedReader");

String errorline = "";

while ((errorline = stdError.readLine()) != null) {

System.out.println(errorline);

}

System.out.println("After checking error");

String line = "";

while ((line = reader.readLine()) != null) {

System.out.println(line);

}

System.out.println("After while of reader");

reader.close();

stdError.close();

System.out.println("Before waitFor");

exitValue = process.waitFor();

System.out.println("After waitFor");

if (exitValue != 0) {

System.out.println("Abnormal process termination");

}

System.out.println("Finished main execution...");

} catch (IOException | InterruptedException e) {

e.printStackTrace();

}

}

}

==================================

I need to login in Kerberos using keytab path and then Kerberos prompt come where need to pass Kerberos command and after successful execution need to get out of the Kerberos prompt.

So 1st will put command in bash shell prompt which will bring Kerberos prompt.

Then execute Kerberos command.

Next command to get out of Kerberos prompt.

The same behavior happen for FTP as well. Could not find any java package to handle this scenario. Already tried ChannelExec and ProcessBuilder but they don't work for second inner prompt command.

What I have tried:

I have tried above mentioned code which worked with simple command and parameter but didn't work where first command goes to inner prompt (like FTP prompt or Kerberos prompt) and then next command or parameter does not work.

解决方案

Put all you commands into a text file and then use the -s option to input them into ftp.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值