Sqlite3: How to URLEncode query results? - java

I have a system where it connects to a SMS Gateway.I use sqlite3 to connect to the database of the Gateway. The system connects to the Gateway and retrieves messages then saves it to an Oracle database. My problem is that the format of the messages disappears once I set the values of the message to an object.
Is there any way to encode the message in the query?
I've tried doing this:
SELECT URLENCODE(*) FROM SMSRECV;
I know that this is not a proper function, but I still tried because I really can't find a way
Connecting to the gateway via ssh
private String getSmsDump() {
StringBuilder sb = new StringBuilder();
try {
String command = "sqlite3 /persistent/var/lib/asterisk/db/MyPBX.sqlite \".read /persistent/script/cwms/smsrecv_html.sqlc\"";
String host = this.sshHost;
String user = this.sshUser;
String password = this.sshPassword;
int port = this.sshPort;
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream input = channel.getInputStream();
channel.connect();
try {
InputStreamReader inputReader = new InputStreamReader(input);
BufferedReader bufferedReader = new BufferedReader(inputReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
bufferedReader.close();
inputReader.close();
} catch (Exception ex) {
LOGGER.error("BUFFERED READER ERROR: "+ex);
}
channel.disconnect();
session.disconnect();
} catch (JSchException | IOException ex) {
LOGGER.error("SSH CONNECTION ERROR: "+ex);
}
return sb.toString();
} // end getSmsHtmlDump()
Setting the values to an object:
public List<SmsMessage> getSmsMessages() {
List<SmsMessage> result = new ArrayList();
String smsDump = getSmsDump();
String received = "RECEIVED";
String status = "1";
if (null != smsDump) {
Document messageDump = Jsoup.parse("<html><head></head><body><table>" + smsDump + "</table></body></html>");
Elements rows = messageDump.getElementsByTag("tr");
String hasRead = "0"; //no
// Lambda expression for iterating row elements
rows.stream().map((Element row) -> {
SmsMessage message = new SmsMessage();
Elements cols = row.getElementsByTag("td");
message.setMessageId(cols.get(0).text()); // Message ID
message.setSender(cols.get(1).text()); // Sender
message.setSmsc(cols.get(2).text()); // SMSC
message.setPortId(cols.get(3).text()); // Port ID
message.setContent(cols.get(4).text());
LOGGER.debug(cols.get(4).text());// Content
message.setReceiveTime(cols.get(5).text()); // Receive Time
message.setHasRead(hasRead); // Has Read
message.setTypeOfMessage(received);
message.setMessageStatus(status);
return message;
}).forEachOrdered((message) -> {
result.add(message);
//LOGGER.debug(message.toString());
});
}
return result;
} // end getSmsMessages()
Content of the smsrecv_html.sqlc file:
.mode html
SELECT * FROM SMSRECV;
I just want to set the values to the object without removing the format
Hope you could help me.
Thank you!

Related

Execution of SSH command from Java sends status code 255, but in the terminal if it works [duplicate]

This question already has answers here:
Certain Unix commands fail with "... not found", when executed through Java using JSch
(3 answers)
Closed 3 years ago.
I am trying to develop a small application that allows me to send certain commands by SSH to a remote server. If I try it from the Linux terminal or from the Windows Command Prompt it works without problems, but when I do it from my Java application it always responds with a status code of 255.
I have disabled the firewall and I have changed the port where I have listening SSH on the server to 22, because I use another, but nothing works. It does not throw me exceptions or anything and if it connects without problems. Any ideas?
I have tried with the sshj and JSch libraries and with both I have the same problem.
ForwardAgent is turn off
sshj example
private void sshj() throws Exception {
SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier((s, i, publicKey) -> true);
ssh.connect("host", 22);
Session session = null;
try {
ssh.authPassword("username", "password");
session = ssh.startSession();
Session.Command cmd = session.exec("command");
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join(5, TimeUnit.SECONDS);
System.out.println("Exit status: " + cmd.getExitStatus());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
ssh.disconnect();
}
}
JSch example
private static void jsch() throws Exception {
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("command");
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 status: " + ce.getExitStatus());
}
change to code so that you get the inputStream before doing connect
InputStream in = ce.getInputStream();
ce.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
Also, the getSession is wrong as it should be
public Session getSession(String username,
String host,
int port)
edit
The below code works for me
JSch js = new JSch();
Session s = js.getSession("username", "127.0.0.1", 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("uptime");
InputStream in = ce.getInputStream();
ce.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
ce.disconnect();
s.disconnect();
System.out.println("Exit status: " + ce.getExitStatus());
output
10:26:08 up 149 days, 58 min, 3 users, load average: 0.61, 0.68, 0.68
Exit status: 0

How i can read file from unix server after doing some grep operation using java(Jsch)

I have a text file on server with a size of 200 mb.I want reduce the file content using command (grep keyword1 filename.txt|grep -v keyword2) and then read that file in java using Jsch connection.
I solved the issue by below code:-
JSch jsch = new JSch();
Session session = null;
try {
String command="grep 'keyword1' filename.txt|grep -v 'keyword2'";
session = jsch.getSession(userid, servername, serverport);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
channel.connect();
InputStream commandOutput = channel.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(commandOutput));
String line="";
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
br.close();
channel.disconnect();
session.disconnect();
} catch (Exception e) {
e.printStackTrace();
}

Java code to run command on remote Linux with sesu access

I need java code that logs into linux box with my credentials, then do a sesu , and then execute a shell script. Permission to execute the shell script if for only sesu user, hence sesu-ing after login is critical. I used te following code which can help me execute a command with my credential scope, however, I need sesu login following my login. Please suggest a way.
I tried adding the sesu command in teh command list, but it prompts for a password. I want a way to pass the password as well and completly automate it.
import java.io.InputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class SSHCommandExecutor {
/** * #param args */
public static void main(String[] args) {
String host = "xxxxxxx";
String user = "xxxxxxx";
String password = "xxxxxxx";
String command1 = "cd /test; ./test.sh";
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command1);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {}
}
channel.disconnect();
session.disconnect();
System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Variant A: Using JSch all the way
This minimal script (test.sh) requests an input from stdin before putting out one line of data:
#!/bin/bash
echo -n "Is this a good question (y/n)? "
read answer
if echo "$answer" | grep -iq "^y" ;then
echo Yes
else
echo No
fi
So it should be equivalent to your call requesting a password to be given. Now have a look at this code on how to send data into that process
String command1 = "cd /home/jan; ./test.sh";
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
ChannelExec channel = (ChannelExec)session.openChannel("exec");
OutputStream o = channel.getOutputStream();
PrintWriter pw = new PrintWriter(o);
InputStream in = channel.getInputStream();
((ChannelExec) channel).setCommand(command1);
channel.connect();
// 1 - Reading the prompt to input password
byte[] buf = new byte[255];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
// 2 - Send (password) data and flush stream
pw.println("y");
pw.flush();
// 3 - Read result
BufferedReader br = new BufferedReader(new InputStreamReader(in));
System.out.println(br.readLine());
// 4 - Clean up
channel.disconnect();
session.disconnect();
Variant B: Shell magic
String command1 = "cd /test; echo 'password' | ./test.sh";
or
String command1 = "cd /test; ./test.sh <<< 'password'";
(of cause you'd need to specify correct password there)
To run remote script with root privileges even if login user does not have them see here:
https://askubuntu.com/questions/155791/how-do-i-sudo-a-command-in-a-script-without-being-asked-for-a-password

Executing remote shell commands in Java

I need to execute some remote commands on unix box using java. For that I have used jsch but the issue is that I need to collect the output of last command.
i wrote the below piece of code:
public static String run(String hostname, String hostPort, String username, String password, String[] commands) throws JSchException, IOException
{
JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, 22);
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
// System.out.println("Connected");
ChannelExec channel = (ChannelExec) session.openChannel("exec");
BufferedReader in = new BufferedReader(new InputStreamReader(channel.getInputStream()));
StringBuffer sb = new StringBuffer();
for (String s : commands)
{
sb.append(s + ';');
}
String command = sb.toString();
channel.setCommand(command);
channel.connect();
String msg = null;
//String output = "";
StringBuffer output = new StringBuffer();
while ((msg = in.readLine()) != null)
{
//output = output + msg;
output.append(msg);
}
channel.disconnect();
session.disconnect();
// System.out.println("DONE");
return output.toString();
}
In the above code, I am collecting all the command result but i need to collect only result of last command fired.
Please provide your inputs.

What is the reason to get exit status value -1 in JSch

I am trying to run a command on a remote Linux box from Java using JSch (SSH) API. The value of exitStatus is -1 i.e.
int exitStatus = channelExec.getExitStatus()
What is the possible reason to get a negative value?
Note the documentation on getExitStatus().
The exit status will be -1 until the channel is closed.
API Description
Here is my code. It works fine.
public static Result sendCommand(String ip, Integer port, String userName, String password, String cmd) {
Session session = null;
ChannelExec channel = null;
Result result = null;
try {
JSch jsch = new JSch();
JSch.setConfig("StrictHostKeyChecking", "no");
session = jsch.getSession(userName, ip, port);
session.setPassword(password);
session.connect();
channel = (ChannelExec)session.openChannel("exec");
InputStream in = channel.getInputStream();
channel.setErrStream(System.err);
channel.setCommand(cmd);
channel.connect();
StringBuilder message = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null)
{
message.append(line).append("\n");
}
channel.disconnect();
while (!channel.isClosed()) {
}
result = new Result(channel.getExitStatus(),message.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
try {
session.disconnect();
} catch (Exception e) {
}
}
}
return result;
}
'-1' means that the exit status code has not been received yet from the remote sshd.
channelName.getExitStatus()
it will retrieve the exit status of the remote command corresponding to your channel. If the command is not yet terminated (or this channel type has no command), it will return the status as -1.
For more got to jsch documentation.

Categories