SSH into multiple servers
Hi I am trying to s开发者_开发问答sh into 4 servers but I am only getting in the 1st one and not the rest. if anyone can let me know where I am going wrong in this code.
try {
File f = new File("/usr/site/html/Output.txt");
BufferedWriter output = new BufferedWriter(new FileWriter(f));
out.println(f.getPath());
String Servers[] = {"root@a1.xyz.com","root@a2.xyz.com","root@a3.xyz.com","root@a4.xyz.com"};
for(int i =0;i<Servers.length;i++){
Process p = Runtime.getRuntime().exec("ssh "+Servers[i]);
output.write("\nI'm In"+Servers[i]);
String s = "exit";
byte[] byteS = s.getBytes();
p.getOutputStream().write(byteS);
output.write("\nI'm logged out ");
output.close();
}
So far I just can login into the 1st one . Any suggestions??
Thanks
This may not solve your problem but from the first look at your codes, you close the output in the loop. Any atttemp to write to an output that is closed should give a run time error. Move output.close()
out of the for loop.
Check the exit value of the process and make sure its 0. Also, it might be a good idea to use the waitFor method of the Process object as it makes the thread waits untill the current process is terminated.
精彩评论