Creating an inputstream for use with JSCH API. (Java) - java

Why am I trying to do this?
Right now I'm trying to make a multi tabbed SSH client for use with a few servers. I have 8 at the moment, soon to be 9. As you can imagine, there are a few redundant tasks one has to do when working with Linux. Connecting to each server to make changes one at a time is a terribly tedious process. That's why I'm trying to make an SSH client that can connect to multiple servers at the same time so I can send a command ONCE to have it affect all servers I own.
How far am I right now?
I have a nice UI set up that can connect, log-in, and receive data from the servers. For input, the API requires I specify an inputstream. If I specify System.in as my inputstream, I can then run the program, and have whatever I type into console be broadcast out to the different servers, via the API.
The problem
is that no end user will ever want to work with a separate console to use this program. It will look dinky. So I need some way to take input from the text field to send it through a specified inputstream. That means I'll need an inputstream that never closes unless the program closes. Like System.in. Also, I can't easily redefine the stream once I set it. I searched for an answer yesterday for around 10 hours. Couldn't find anything. If anyone can help, please do. Thank you.
I need
an inputstream that works exactly like an outputstream. It stays open even when nothing is being sent through it, but as soon as it gets data, the data is sent automatically to anything that is using it. This API is very strange, but this last inputstream part is the only thing keeping me from finishing up my program. Thank you for your time.

JSCH sudo su command "tty" error
I was using the API incorrectly. Stupid, yes. I don't want anyone else making the same mistake though. I guess I was following a bad example found somewhere else on the internet.
Essentially, you don't even need to set the input stream. You just need to use the output stream that already exists. Write directly to the output stream. Pretty sure I was trying to do this at 3am last night. It was right in front of me the whole time.

Related

"Smart" scripting via SSH using Java

I want to write a program which controls a tool on a remote server. For that I want to create a SSH-Method to connect to the remote server and execute some commands to get some data. Sadly I don’t find a way to handle the SSH part properly.
Here is a flowchart showing what the method should be able to do:
First I want to connect via SSH to the remote server. I successfully tried that with “JSch”.
After that, the method should enter the command ls -l into the shell and wait for the response. Now it should parse the output, and act differently depending on the output.
If there is a folder with the name “folder123”, the next commands should be to change into this folder (cd folder123) and enter ls -l there again. This list of content then should be returned and the ssh connection can be closed.
If the folder doesn’t exist, the program should create one (mkdir folder123) and then return a code (-1).
In my main program I will use the new data to make some decisions and then going on.
I found ways with JSch to execute one command, like ls -l and even multiple commands and get the response back as a string. Sadly, I have to pass all commands in one block for that, so I have no way to make the decision in the middle of it. When I want to implement decision making, I have to close the connection first. That would lead to a lot of connection building overhead, especially when there will be more than one decision to make and I have multiple navigating steps between them.
So is there a way to make this decision while the connections stays establish, so I can directly enter the next command after it?
Edit:
I am just playing around by now. So my solution with the multiple commands is basically the answer from Mihail in this post
For this part of the task it would be more simple to use the ways DaveyDaveDave and Aaron proposed. But i have to do some more specific tasks.I thought it is the best to provide a simple example first, but here is another one which needs to use a program on the remote server:
Another Example
After the ssh connection is established (the only non-graphical way to access this server), I have to login in the tool (that is a one-liner the command line).
In the cmd, and through this in my ssh connection, i get an output after that. It shows some information about the user and ends with the word "END" and above that i have a return code. The function has to wait for the "END" and then parse the return code. Depending on it, the function should try again or enter the next command.
After the next command it has to do the same validation and when everything worked until now, the last output should be returned to the main program (in this it contains network elements and information about them).
Found an fitting answer for my problem in another Thread where I asked a more specific question.
Look here if you have similar problems:
Change System.in and read System.out programmly on the fly (Jsch)

Display text in console while writing to it

I am making a chat program in Java. When a user sends a message to the server, the server sends that message to every other user. The problem is, when a user is typing, he can receive a message, that will be written in the middle of the text he is typing.
A user must always be able to receive messages, while at the same time being able to write without having text appearing in the message he is typing.
Is there any way to do this in Java? A good solution would be to have a part of the console where the messages appear, and another one solely for typing.
It seems that there are libraries similar to ncurses, but are there any solution that doesn't rely on external libraries?
Cleaning the console using ANSI escape codes doesn't help since the text an user is typing might simply disappear because of it.
You can create 2 seperate console applications (one which reads, and one which only sends). That way your console windows would be separate and text would not conflict. Bear in mind that if you are using sessions, you would need to bind both sessions to the same user.
Another solution could be reading and storing each individual key without pressing enter. When you receive the response from the other client, you manually clear everything, print the text received, then place whatever the user was typing directly after.
Both of the above solutions seem hacky, and I feel Swing or JavaFX would be the way to go.

Most effective way to read/write large .ser file for online applet

I am writing an applet that I eventually want to put online so that my friends/family can use it. I have the applet running now locally, but in order to work properly it needs to read a .ser file in when the applet opens, and update that same file when the applet closes. The file is quite large (~180 MB), though I am working on paring it down.
What would be the fastest/most effective way to read/write this file in java? There is a lot of information out there on this and I have never done anything like it before, so it's a bit overwhelming. The class HTTPURLConnection seems like an option to read it, but not write it. Any free web hosting that I have seen will not allow a file that big to be uploaded.
The size of the file should hopefully go down substantially, it is a list of 2.8 million musical artists, many of which I'm sure nobody using the program will ever encounter, but if this program is to be effective, many artists will have to be stored, so the problem most likely remains the same.
Thanks in advance for any help
It sounds like it would be wise to keep this large data and the processing of it on your server instead of making the applet operate on it. That's because you would avoid each user downloading a large file and processing it. If you had a server side piece that the applet could call to get useful information from, then only your server would have to load it, write it, and process it. You could implement a Java servlet, or a PHP program to respond to http requests from your applet in a format that suits the data. I'm assuming that your server can handle either servlets or custom PHP (most can).

Running server-program from a .jar file

I have created and am working on a server-application that monitors for specific folders and takes appropriate actions whenever files are being added.
Now I come to the point where I want to be able to shutdown the program, for example for applying a patch.
The server runs simply in a command prompt, how can I signal that I want to perform maintenance on it? I do not think reading System.in is feasible as I am also outputting text in the prompt.
Regards.
You could try reading System.in as System.in and System.out are different file descriptors. What this means is that by writing things in console you are not writing in the same place than when you are typing, so console output should not matter for reading commands in the prompt.
A second application can be used to communicate with the server application. You can use Java Management Extensions or implement your own client/server communication using sockets.
Another way to achieve this is that server periodically checks for existence of an specific file somewhere on hard disk. If server finds that specific file, it will shut down.

Problems with PHP connecting to a Java program using a Port

Hoping someone can shed some light on this.
I have a PHP program which opens a port and sends some text to a java program listening on that port. Basically, there could be many instances of this PHP program connecting over this port to this one java program. The java program contacts an API, retrieves the answer, and then sends the information back over the port to the PHP program.
This solution seems to work but sometimes I get a empty response in my PHP program. The java program runs run successfully but the answer string is not transmitted back through the port to the php program.
Does this solution sound valid? Will I ever have an instance where the java program will send back the wrong information to the PHP program? Can anyone see any problems here? Thanks a bunch!
"Opens a port" is ambiguous, I'm going to assume you mean "opens a TCP connection".
There's no reason that the scheme you outlined cannot work, but you haven't provided enough information to narrow down the cause of the problem you're having.
When your PHP script gets an empty response from your Java code, it could be because the connection was broken, but it's more likely that the code on one end or the other has a bug. Make sure you're testing all the result values for errors.
If you need help finding the problem, you will need to post more specifics, ideally the smallest example of real code (for both ends of the connection) you can get to exhibit the problem.
Does the Java program close or at least flush the socket's stream after writing?
Consult the basic Sun tutorial "All About Sockets" for details and proper code examples.

Categories

Resources