I'm testing short ping test program. If I set simple ping command, "ping y.y.y.y -c 5 -s 500 "
into setCommand()
function, it works as designed. But if I add addition ping options, "ping source x.x.x.x host y.y.y.y -c 5 -s 500"
, I got
ping: unknown host source
message back. If I manually execute both commands from the x-terminal, the both commands works fine.
I need to make the program to ping from different source interface IPs. What is the difference between two commands using JSch setCommand
?
ping y.y.y.y -c 5 -s 500
(working)ping source x.x.x.x host y.y.y.y -c 5 -s 500
(not working)
Code:
public static void main(String[] arg){
try{
JSch jsch=new JSch();
String host=null;
if(arg.length>0){
host=arg[0];
}
else{
host=JOptionPane.showInputDialog("Enter username@hostname",
System.getProperty("user.name")+
"@localhost");
}
String user=host.substring(0, host.indexOf('@'));
host=host.substring(host.indexOf('@')+1);
Session session=jsch.getSession(user, host, 22);
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);
session.connect();
// this command works
// String command = "ping 20.5.1.15 -c " + count + " -s " + size;
// this command not working
String command = "ping source 20.5.1.10 host 20.5.1.15 -c " + count + " -s
" + size;
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();
byte[] tmp=new byte[1024];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
System.out.print(new String(tmp, 0, i));
}
if(channel.isClosed()){
if(in.available()>0) continue;
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
session.disconnect();
}
catch(Exception e){
System.out.println(e);
}