简体   繁体   English

使用Ganymed SSH在Linux中连续执行命令

[英]Continuous Execution of commands in Linux using Ganymed SSH



I am using Ganymed SSH to execute commands in my Linux Machine from Java application. 我正在使用Ganymed SSH从Java应用程序在Linux机器中执行命令。 I have to execute 100 commands one by one. 我必须一一执行100条命令。

For this in my code under for loop I am opening and closing the session for each iteration. 为此,我在for循环下的代码中为每次迭代打开和关闭会话。 But it is throwing some exception after 10 or 15 iterations saying that Unable to establish the connection. 但是经过10或15次迭代后,它抛出了一些异常,表明无法建立连接。

I believe this is happening because the sessions are not creating frequently. 我相信这是因为没有频繁创建会话而发生的。

Please check the below code and guide me some good solution for achieve this. 请检查以下代码,并为我提供一些实现此目的的好的解决方案。

for(String user : usersList) for(字符串user:usersList)
{ {
session = connection.openSession(); 会话= connection.openSession();
session.execCommand("[ -f /home/"+user+"/.file_name] && echo \\"Found\\" || echo \\"Not Found\\""); session.execCommand(“ [-f /home/"+user+"/.file_name] && echo \\” Found \\“ || echo \\”未找到\\“”);
session.close(); session.close();
} }

Thanks & Regards, 感谢和问候,
Gupta Katakam 古普塔·卡塔卡姆(Gupta Katakam)

If you try to do this, you will end up in the situation that you are in. SSH has many configuration parameter but that you need to specify in the end machine. 如果尝试执行此操作,则最终会遇到这种情况。SSH具有许多配置参数,但需要在最终计算机中指定。 I am not sure if you have access to the end machine or not, but in case you do not have access to the target machine, please consider adding some delay on you code after doing a disconnect and re-connect. 我不确定您是否可以访问终端计算机,但是如果您无法访问目标计算机,请在断开连接并重新连接后考虑在代码上添加一些延迟。 I was also facing the same problem although I was using a different library. 尽管我使用的是其他库,但我也面临相同的问题。 Let me know if this things works or not, and then we can take it forward from there. 让我知道这件事是否有效,然后我们可以从那里继续进行下去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM