Hi I am trying to ssh 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.