linuxsshganymede

Continuous Execution of commands in Linux using Ganymed SSH




I am using Ganymed SSH to execute commands in my Linux Machine from Java application. I have to execute 100 commands one by one.

For this in my code under for loop I am opening and closing the session for each iteration. But it is throwing some exception after 10 or 15 iterations saying that Unable to establish the connection.

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)
{
session = connection.openSession();
session.execCommand("[ -f /home/"+user+"/.file_name] && echo \"Found\" || echo \"Not Found\"");
session.close();
}

Thanks & Regards,
Gupta Katakam


Solution

  • 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. 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.