I am trying a Java program to run multiple commands in unix environment. I would need to pass 'ENTER' after each command. Is there some way to pass enter in the InputStream.
JSch jsch=new JSch();
Session session=jsch.getSession("MYUSERNAME", "SERVER", 22);
session.setPassword("MYPASSWORD");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel= session.openChannel("shell");
channel.setInputStream(getInputStream("ls -l"));
channel.setInputStream(getInputStream("\r\n"));
channel.setInputStream(getInputStream("pwd"));
channel.setInputStream(getInputStream("\r\n"));
channel.connect();
When I do ls -l, I want to add enter here, so that the command is executed.
getInputStream is a method to convert String into InputStream.
Any help will be appreciated.
According to the JSch javadoc, you must call setInputStream() or getOutputStream() before connect(). You can only do one of these, once.
For your purposes, getOutputStream() seems more appropriate. Once you have an OutputStream, you can wrap it in a PrintWriter to make sending commands easier.
Similarly you can use channel.getInputStream() to acquire an InputStream from which you can read results.
OutputStream os = channel.getOutputStream();
PrintWriter writer = new PrintWriter(os);
InputStream is = channel.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
channel.connect();
writer.println("ls -l");
String response = reader.readLine();
while(response != null) {
// do something with response
response = reader.readLine();
}
writer.println("pwd");
If you're determined to use setInputStream() instead of getOutputStream() then you can only do that once, so you'll have to put all your lines into one String:
channel.setInputStream(getInputStream("ls -l\npwd\n"));
(I don't think you need \r, but add it back in if necessary)
If you're not familiar with working with streams, writers and readers, do some study on these before working with JSch.
Related
I tried to use InputStream and Buffer[], also BufferedReader, also PipedInputStream. For all cases i got null:
sessionB = jSch.getSession(username, "localhost", forwardedPort);
sessionB.connect();
if(sessionB.isConnected()) {
System.out.println("Connected host B!");
channel = (ChannelExec) sessionB.openChannel("exec");
br = new BufferedReader(new
InputStreamReader(channel.getInputStream()));
((ChannelExec)channel).setCommand("command");
((ChannelExec)channel).setErrStream(System.err);
channel.connect();
if(channel.isConnected()) {
System.out.println("Channel is connected!");
}
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
And console output:
Connected host A! Connected host B! Channel is connected!
Problem: I got nothing printed (System.out.println(line);)
There is a way to get that stream from exec channel over portforwarding ? Thanks for your help
get/setInputStream and get/setOutputStream only concern the standard input/output of the command, nothing to do with port forwarding.
The port forwarding is done through two functions setPortForwardingL and setPortForwardingR.
You should have something like that in your code.
int assinged_port=session.setPortForwardingL(lport, rhost, rport);
As always, there is very few documentation on JSCH but a lot of detailed examples for L forwarding and R forwarding
The port forwarding is done on the session so I'm not sure you also need an active connection. But if you do, you should consider opening a 'shell' connection instead of an 'exec'. Then you wouldn't have to run a useless command just to maintain the connection.
PS: Some code is missing so it's difficult to say something precise on the example given in the question.
Thanks for your help. I solved it using Pipes, i share what i did:
PipedInputStream en = new PipedInputStream();
pin = new PipedOutputStream((PipedInputStream) en);
BufferedReader br = new BufferedReader(new InputStreamReader((PipedInputStream)
channel.getInputStream()));
channel.connect(5*1000);
String received=null;
while((received=br.readLine())!=null) {
System.out.println(received);
}
I'm trying to manage router via Java application using Jcraft Jsch library.
I'm trying to send Router Config via TFTP server. The problem is in my Java code because this works with PuTTY.
This my Java code:
int port=22;
String name ="R1";
String ip ="192.168.18.100";
String password ="root";
JSch jsch = new JSch();
Session session = jsch.getSession(name, ip, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
session.connect();
System.out.println("Connection established.");
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand("enable");
channelExec.setCommand("copy run tftp : ");
//Setting the ip of TFTP server
channelExec.setCommand("192.168.50.1 : ");
// Setting the name of file
channelExec.setCommand("Config.txt ");
channelExec.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
int index = 0;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
session.disconnect();
I get
Line has an invalid autocommand '192.168.50.1'
The problem is how can I run those successive commands.
Calling ChannelExec.setCommand multiple times has no effect.
And even if it had, I'd guess that the 192.168.50.1 : and Config.txt are not commands, but inputs to the copy run tftp : command, aren't they?
If that's the case, you need to write them to the command input.
Something like this:
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channelExec.setCommand("copy run tftp : ");
OutputStream out = channelExec.getOutputStream();
channelExec.connect();
out.write(("192.168.50.1 : \n").getBytes());
out.write(("Config.txt \n").getBytes());
out.flush();
In general, it's always better to check if the command has better "API" than feeding the commands to input. Commands usually have command-line arguments/switches that serve the desired purpose better.
A related question: Provide inputs to individual prompts separately with JSch.
I was trying to build an small code where I want to create some string and transfer that string to a file (that should be created in runtime) located in remote server. In my case the remote server is Linux.
Can someone help me here? I was using a JSCH and ChannelSftp but unable to do the thing. Below is my code:
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, MachineIP, SFTPPORT);
String str = "Hello";
session.setPassword(SFTPPASS);
System.out.println(SFTPPASS);
java.util.Properties config = new java.util.Properties();
System.out.println("Config done");
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
System.out.println("Config set");
session.connect();
System.out.println("Session connected");
channel = session.openChannel("sftp");
channel.connect();
System.out.println("Connection Opened\n");
channelSftp = (ChannelSftp) channel;
channelSftp.cd(SFTPWORKINGDIR);
File f=new File("Test.txt");
//unable to do anything beyond this.
Sorry if you find this stupid but I am very new to it.
ChannelSftp has versions of the put method which accept a filename on the remote system and which return an OutputStream. Anything written to the OutputStream is written to the file on the remote system. You can write binary data to an OutputStream, or convert it to a Writer if you want to write text to it:
try (OutputStream out = channelSftp.put("/some/remote/file")) {
OutputStreamWriter writer = new OutputStreamWriter(out);
writer.write("some text");
} catch (IOException e) {
....
}
#Kenster answer doesn't work for me (get a 0 bytes file), so I gets another solution:
String content = "some text";
InputStream stream = new ByteArrayInputStream (content.getBytes ());
sftpChannel.put (stream, "/some/remote/file");
Hope it'll help somebody...
I am trying to execute a simple linux command to append some text to a file in remote server through my java code. But it isn't working. When I run the same command in the linux box it works fine.
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session sessionwrite = jsch.getSession(user2, host2, 22);
sessionwrite.setPassword(password2);
sessionwrite.setConfig(config);
sessionwrite.connect();
System.out.println("Connected");
Channel channel = sessionwrite.openChannel("exec");
BufferedReader in = new BufferedReader(new InputStreamReader(
channel.getInputStream()));
String command = "echo \"hello\" >> welcome.txt";
((ChannelExec) channel).setCommand(command);
System.out.println("done");
}
Channel channel = sessionwrite.openChannel("exec");
BufferedReader in = new BufferedReader(new InputStreamReader(
channel.getInputStream()));
String command = "echo \"hello\" >> welcome.txt";
((ChannelExec) channel).setCommand(command);
System.out.println("done");
You're missing the call to channel.connect(). connect() is the method which actually sends the request to the remote server to invoke the command. You should also call channel.disconnect() when finished with the channel to terminate it. Your code might look something like this:
Channel channel = sessionwrite.openChannel("exec");
BufferedReader in = new BufferedReader(new InputStreamReader(
channel.getInputStream()));
String command = "echo \"hello\" >> welcome.txt";
((ChannelExec) channel).setCommand(command);
channel.connect();
channel.disconnect();
System.out.println("done");
I'll add that, in this particular example, there's no reason to open an input stream on the exec channel's standard input, so you could leave that line out.
I've been trying to figure out this problem for hours now and I cant seem to figure it out. I'm trying to use JSch to SSH to a Linux computer from an Android phone. The commands always work fine but the output of the channel is usually empty. Sometimes it displays the output but most of the time it doesn't. Here's the code that I found online.
String userName = "user";
String password = "test123";
String connectionIP = "192.168.1.13";
JSch jsch = new JSch();
Session session;
session = jsch.getSession(userName, connectionIP, 22);
session.setPassword(password);
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
// SSH Channel
ChannelExec channelssh = (ChannelExec) session.openChannel("exec");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
channelssh.setOutputStream(baos);
// Execute command
channelssh.setCommand("ls");
channelssh.connect();
channelssh.disconnect();
RESULT = baos.toString();
RESULT is usually empty. If I change the command to mkdir or something of that nature the files show up on the Linux computer which leads me to believe that the command part is working correctly. The problem seems to lie within the ByteArrayOutputStream. I've also tested the connectionip, username and password on a different computer through Terminal so I know the credentials are correct. I've Googled this problem to death, any input would help me out significantly!
Found the answer I was reading the wrong stream. Heres the proper code for others with this problem.
InputStream inputStream = channelssh.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null)
{
stringBuilder.append(line);
stringBuilder.append('\n');
}
return stringBuilder.toString();
The exec-channel will be run on the other thread, so you need to wait for its termination before invoking Channel#disconnect().