Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

shell - Java library to run multiple unrelated commands on a remote server via ssh

My Java application has to work like this:

  1. User select bash commands in GUI and press "send."
  2. Application return distinct and independent answers for each command (e.g. we could store them in different files).
  3. Commands each run interactively, not in a batch (it can't be something like "ls pwd " etc)
  4. After each command, the application will check if the results are ok. If so, it will send the next command.
  5. We need to execute su <user> on the remote host.

This will be a plugin for a bigger app, so answers like "try something else" (i.e. RPC, web services) will not help me :(

As far as i understand i have to use SHELL or at least keep channel connected.

I have tested jsch , sshj and ethz.ssh2 but with bad results.

I've dug throu stackoverflow answers for questions like: "sending-commands-to-server-via-jsch-shell-channel" etc. But they all focus on sending whole commands in one line. I need an interactive, persistent SSH session.

I've used ExpectJ (with a little hack of output stream). It has resolved points 1,3,4,5.

But there is a problem with point 2. In my app I need to get separated answer. But we will not know their length. Command prompts can be different. Anyone knows how to "hack" ExpectJ so it will be some how more synchronized? I am looking for acting like this : send , wait for full answer, send, wait... I've tried some basic synchronization tricks but this end in timeouts and connection lost usually.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should use ExpectJ, a Java implementation of the Unix expect utility.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...