• Post Reply Bookmark Topic Watch Topic
  • New Topic

connecting to a remote machine with ssh, executing a program, and reading the output from that prog.  RSS feed

 
Grant Dragonson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to use the Runtime.getRuntime().exec to connect to a remote machine, using the commande "ssh <remote machine name>"
This is working.

p = Runtime.getRuntime().exec("ssh <remote machine name>");

I know this is working because I do a
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
System.out.println("Loading ports, please wait while reading.");
Thread.sleep(1000);
while (in.ready()){
s = in.readLine();
System.out.println(s);
}

which spits out the login info on the remote machine. Since I'm logged into the first machine (AIX) with the same user as the second machine it never asks for a user or password so I'm saved from that....

But then I try to use an

out.write("remote command");
out.flush();
Thread.sleep(1000); // wait for the output to buffer up a bit so I don't read when there's nothing to read.
int j = 0;
while (in.ready()){
System.out.println(s);
}

at this point it just shoots on by the in.ready() loop and goes on, never reading anything. I've tried upping my sleep, but that doesn't do anything. Is my out.write not working? How can I tell?
Am I making sense? :O
I am actually using the same command on the local machine and reading in the data with an exec command, so I'm thinking the ssh should work the same way. It just doesn't seem to be getting my out.write commands at all. Too bad I can't do an exec on the remote machine instead and then capture the output....

Any help would be very appreciated! Thank you!
 
Grant Dragonson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, a co-worker had a good idea. I can create a script that will run the command remotely and copy the file locally, so I can then just read in the file. I suppose this will work, but it just isn't as cool....
 
Grant Dragonson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured out how I can do this. I can run a script that does redirection and puts all the information into a local file. For example the exec command would be a local script that has:
ssh "remote site" < commands.txt > "local file"
The < and > are redirection signs, so it gets the commands from commands.txt and then puts all the output into the local file >.
I know, this has nothing to do with Java, but it might help someone in the future. Thanks!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a quick and dirty approach which I myself have used when the task is fairly straightforward. However, for more complicated tasks that require multiple steps, with each step having the potential to go wrong somehow and you need to give feedback to the calling program about the status of the remote job, etc. then this approach quickly becomes unwieldy and prone to patching and duct taping until it takes on a life of its own and starts consuming more hours devoted to maintenance and debugging from your own life. In short, it can get ugly pretty quick. I wouldn't wait for it to get even close to getting ugly. Once I find that I have to tweak it a few times, I would start thinking about writing a simple web service. If you want to get fancy and hipster, maybe look at microservices. At any rate, I try to limit my use of the approach you describe to the truly quick and dirty and I'm careful not to let it devolve into a sticky, stinky, marsh of shell scripting despair.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!