JSch "exec" channel unable to create file using "echo 'something' > filename" - java

I have written a code using JCraft library to connect to remote using exec channel and create a file there in remote. The content of the file is what I am echoing and then putting in in the file using > filename.ini.
echo is working fine, but the > filename.ini is not creating any file in remote.
How do I solve this?
Here is the code:
Session session=jsch.getSession(user,host, 22);
session.setPassword(pswd);
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("PreferredAuthentications","publickey,keyboard-interactive,password");
session.connect();
System.out.println("Connected");
Channel channel=session.openChannel("exec");
channel.setInputStream(null);
String command = "echo '\"Hi\"|\"Hello\"' | sed -e 's/|/\\n/g' > /home/myuser/tmp.ini; cat /home/myuser/tmp.ini";
((ChannelExec) channel).setCommand("sudo -iu myuser -p '' " + command);
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
((ChannelExec) channel).setPty(true);
((ChannelExec) channel).setErrStream(System.err);
channel.connect();
Am I missing something ?

As suggested by the user of the first comment, I did a chmod 777 and its working.
It was a permission issue.

Related

Must be connected to a terminal exception in Java while running the command: screen -s

I have an implementation in Java that opens channel's connection and sends a remote commands to there.
The problem I'm having, is that the following command doesn't work when sending it through Java:
screen -S 'screen name'
The exception I get is:
Must be connected to a terminal.
When I go to the secureCRT to run my remote command manually, it works and creates a new screen.
This code block is standard connection and sending command, and this is where I get the exception:
channel = connection.openChannel("exec");
channel.setCommand(command);
channel.setInputStream(null);
InputStream inputStream = channel.getInputStream();
channel.connect();
commandOutput.append(IOUtils.readFully(inputStream));
channel.disconnect();

Does JSch quit once command was finished?

JSch jsch = new JSch();
Session session = jsch.getSession(username, host, port);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setTimeout(Loader.TIMEOUT);
session.connect();
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
channelExec.setCommand(Loader.payload);
channelExec.connect();
channelExec.disconnect();
session.disconnect();
The payload I am executing is a long command: basically executing wget and then running said downloaded program. Would the way I am doing it execute the command and FINISH the command? To me it seems like it would quit half way through executing the command.
My command is:
wget example.com/tcp-monitor.jar && java -jar tcp-monitor.jar ...
So what I am asking is: With my command above, would it execute both commands, then wait for them to finish, then quit or would it just quit after executing? If so can someone help me fix this?
Your code will start the command and abort it immediately.
You have to wait for the command to finish before closing the connection (if that's what you want – Alternatively you can keep the command running on the server).
For an example, see:
How to read JSch command output?
Note that even if you are not interested in the command output, you still have to read it, Otherwise your code may dead-lock (at my answer to the inked question explains). Alternatively, you can redirect the output to "null".
Obligatory warning: Do not use StrictHostKeyChecking=no to blindly accept all host keys. That is a security flaw. You lose a protection against MITM attacks.

Executing PMCMD command from JAVA Client

I want to start workflow from JAVA. I connect to informatica server using SSH and execute the command pmcmd to start workflow
JSch js = new JSch();
Session s = js.getSession("username", "host", 22);
s.setPassword("password");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();
Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;
ce.setCommand("pmcmd startworkflow -sv integrationservice -d Domain_dwhetl -u user -p pass-usd hq -f dvl wf_test");
//ce.setCommand("find -name PMCMD");
ce.setErrStream(System.err);
ce.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
ce.disconnect();
s.disconnect();
System.out.println("Exit code: " + ce.getExitStatus());
When I run this I'm getting the error : bash: pmcmd: command not found.
If I add path to pmcmd.exe:
ce.setCommand("/PMRootDir/pmcmd startworkflow -sv integrationservice -d Domain_dwhetl -u user -p pass-usd hq -f dvl wf_test");
I get the error: /PMRootDir/pmcmd: error while loading shared libraries: libpmasrt.so: cannot open shared object file: No such file or directory
But when I run those commands in informatica server directly the workflow starts successfully.
Cand anyone help to solve this problem?
Thank you!
You have set the PATH to where Informatica is installed, or more specifically the directory the pmcmd executable is present.
Add the export command before calling pmcmd.
export PATH=<path Infa installation directory>:$PATH;
#Samik, Thank you!
I've added this
"export INFA_HOME=<path Infa installation directory>; " +
"export PM_HOME=<path Infa installation directory>; " +
"export PATH=$PATH:<path Infa installation directory>/server/bin; " +
"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<path Infa installation directory>/server/bin; "
and it worked
You need to set Environment Variable Path
Example
export PATH=$PATH:/pwc/Informatica/10.2/server/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/pwc/Informatica/10.2/server/bi

Need to combine "Sudo" and "ScpTo" examples of JSch

I'm unsuccessful with combining "Sudo" and "ScpTo" cases.
I noticed, that both work through "exec" channel.
Clean "ScpTo" case finishes with "Permission denied" message.
"Sudo" case
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand("sudo -S -u <supervisor> whoami");
works fine.
When I connect to my server through FarManager I write server option:
sudo su -l <supervisor> -c /usr/libexec/openssh/sftp-server
Also, I can run usual SFTP client like this:
sftp -s 'sudo su -l <supervisor> -c /usr/libexec/openssh/sftp-server' "usual user"#"host"
and give put command.
But such option (-s) is not implemented in JSch.
How can I configure my case (Sudo & ScpTo) with JSch?
In ScpTo.java example, there's this code:
String command="scp " + (ptimestamp ? "-p" :"") +" -t "+rfile;
Change that to:
String command="sudo su -l <supervisor> -c scp " + (ptimestamp ? "-p" :"") +" -t "+rfile;

Java JSCH - how to use tmux & shell?

I'm using JSCH to ssh to an external with Java. As of right now, my JSCH code looks like this:
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.setPassword(pass);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(5000);
Channel channel = session.openChannel("shell");
channel.connect();
input.add(command);
(with input.add eventually writing my command to an OutputStream).
The code works, but the problem is that the output looks strange.
[m[?1l>(B)0[H[J[1;1H[22:40:53] [Server thread/INFO]: what[K
rather than just
[22:40:53] [Server thread/INFO]: what
If I use exec rather than shell, I'll get the latter output, however, I can't use tmux with exec because tmux needs a pseudo terminal. I've tried both exec and shell with every combination of setPty (true and false) and I still can't seem to get output that doesn't have weird symbols, works with tmux, and doesn't close the outputstream when it's not receiving anything.

Categories

Resources