我正在测试短ping测试程序。如果我将简单的ping命令"ping y.y.y.y -c 5 -s 500 "
设置为setCommand()
函数,则按设计工作。但如果我添加额外的ping选项,"ping source x.x.x.x host y.y.y.y -c 5 -s 500"
,我得到了
ping:未知主机来源
回复消息。如果我从x-terminal手动执行这两个命令,则两个命令都可以正常工作。
我需要让程序从不同的源接口IP ping。使用JSch setCommand
的两个命令有什么区别?
ping y.y.y.y -c 5 -s 500
(工作)ping source x.x.x.x host y.y.y.y -c 5 -s 500
(不工作)代码:
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);
}
答案 0 :(得分:0)
您的ping source x.x.x.x host y.y.y.y
语法对我来说很奇怪。但我会相信你在终端上有效。
ping
命令可能取决于某些环境变量或其他配置来解析源地址。
&#34; exec&#34; JSch中的信道(正确地)不为会话分配伪终端(PTY)。因此,可能(可能)采购了一组不同的启动脚本。和/或脚本中的不同分支基于TERM
环境变量的缺失/存在而被采用。因此,环境可能与您与SSH客户端使用的交互式会话不同。
如果这打破了ping
命令,它在服务器端明显配置错误,而不是JSch错误。 ping
不是交互式命令,因此它甚至可以在非交互式会话上工作。您应该找出打破ping
的内容并相应地修复您的启动脚本。
要验证这是根本原因,请在SSH客户端中禁用伪终端分配。例如,在PuTTY中,它的 Connection&gt; SSH&gt; TTY&gt;不要分配伪终端。然后转到 Connection&gt; SSH&gt;远程命令并使用ping source ...
命令。检查会话&gt;关闭退出窗口&gt;从不并打开会话。
另一种(不推荐的)方法是强制执行&#34; exec&#34;频道使用.setPty
方法:
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setPty(true);
使用伪终端自动执行命令可能会带来令人讨厌的副作用。请参阅示例Is there a simple way to get rid of junk values that come when you SSH using Python's Paramiko library and fetch output from CLI of a remote machine?
这是这个常见问题的特例:
Certain Unix commands fail with "... not found", when executed through Java using JSch