-
Notifications
You must be signed in to change notification settings - Fork 366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a sample code for generic use cases #658
Comments
Somehow I am able execute command remotely but not able to run interactive. I have read this document - Running an interactive command/shell If one needs to parse the command/shell output and then respond by sending the correct input, the code must use separate thread(s) to read the STDOUT/STDERR and provide STDIN input. These threads must be up and running before opening the channel since data may start to pour in even before the await/verify call returns. If this data is not consumed at a reasonable pace, then channel may block and eventually even disconnect. Thus the thread(s) using the streams must be ready beforehand. // The same code can be used when opening a ChannelExec in order to run a single interactive command // The same code can be used when opening a ChannelExec in order to run a single interactive command
} I couldn't understand how to read and write commands in different threads. It would be great If any one could write a sample code. |
I presume you mean First, be aware that Apache MINA sshd has no privilege separation, and is not tied into the Unix user management. The command will run with the user that is running the server. Regarding
|
Thanks for the response. I have tried with getInvertedIn(), but it was returning null. I am still trying, it would be great help if you already have sample code for this use case. That's the reason I have created this issue to provide sample code. btw I fixed my typo from "su" to "sudo". Another use case could be if I run binary calculator command "bc" then it waits for response from user e.g. 2*2 or 3+4 and keeps on going. How can I achieve this scenario? |
If you have a case where the sequence
results in |
Thanks for helping me. stdin is not null this time, I was doing some mistakes and still doing some mistake. Here is the code -
} Process finished with exit code 0 |
Thanks for helping. I found my mistake and it worked now. Only issue is that sometime outputStream doesn't have data because sever takes sometime to process. If I add sleep for 1 sec and reset stream for every command then I get proper output. Is there any way to find when data is available? I have tried - ce.waitFor(EnumSet.of(ClientChannelEvent.STDOUT_DATA), TimeUnit.SECONDS.toMillis(10)); but it doesn't work, it just returns ClientChannelEvent.OPENED. Corrected code -
} OutPut: Sent: 7*23 Sent: 723+323 Sent: 4*2 Final Output list: [161 Process finished with exit code 0 |
That's because you use |
Thanks for helping. Code below is working now. I have few questions though.
3- How do we know that which output of which command from this block of code -
Currently, it is easy to find because each command produces a one-line output, but what will happen when the given command produces multiple lines of output? Sorry if I’m asking any dumb questions, and I appreciate your patience.
Output: Process finished with exit code 0 |
This is not a dumb question at all. It shows that the "protocol" for communicating with "bc" is badly defined since "bc" gives no indication where its output for a command ends. If "bc" gave a prompt whenever it expected input, it would be easier. (Such as writing "> " on stdout when it wants new input.) But it doesn't. I don't have the time to write this code for you. You'll have to figure it out yourself. The main thing is that your code has to keep consuming stdout and stderr, otherwise the command may hang when transport buffers are full. How to synchronize what is read from stdout and stderr with commands you send via stdin is then the application's business. (Same as with any As for providing complete examples: we don't have the resources to produce such examples. If someone provides PRs with high-quality examples, we can include them. As for |
Thanks for your time and responses. I have one final question: I was just using the command bc as an example, but it could be any command. Another use case could be when we are installing a package on a remote machine, and the installer prompts for a response like 'Yes,' 'No,' or 'All,' or other types of questions and answers. Should I use PTY in these scenarios? If so, how should I configure it? |
I have read document with pseudo code but it doesn't help. e.g. how to create serverKeyVerifier as a delegate. How to restrict connection to server when key doesn't exists in known_hosts file etc. It would be great if anyone provides full working example code.
The text was updated successfully, but these errors were encountered: