Discussion:
[JSch-users] Oracle apps through JSch
Ulises Vazquez
2015-02-26 02:08:55 UTC
Permalink
I cannot enter to Oracle SQL*Plus nor Oracle RMAN through a JSch ssh session. The session hangs when the sqlplus or rman prompt is about to appear in the output.

Is there a way to deal with this issue?

Ulises Vázquez Rocha
Leonardo K. Shikida
2015-02-26 11:11:58 UTC
Permalink
Hi Ulises

Here's how I do it.

Use https://github.com/ronniedong/Expect-for-Java (it's just a single class)

import org.apache.log4j.Level;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SQLPlusAutomation {

public static void main(String[] args) throws Exception {
String rootPassword = "...";
JSch jsch = null;
Session session = null;
jsch = new JSch();
session = jsch.getSession("leoks", "localhost");
session.setPassword(rootPassword);
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("PreferredAuthentications",
"publickey,keyboard-interactive,password");
session.connect(10 * 1000);
Channel channel = session.openChannel("shell");
Expect expect = new Expect(channel.getInputStream(),
channel.getOutputStream());
expect.setDefault_timeout(2);
expect.turnOffLogging();

expect.forwardInputStreamTo(System.out);

channel.connect();

expect.expect("password for leoks:");
expect.send("sudo su - oracle\n");

expect.expect("\\$");
expect.send(rootPassword+"\n");

expect.expect("\\$");
expect.send("source ./product/11.2.0/xe/bin/oracle_env.sh\n");

expect.expect("SQL>");
expect.send("sqlplus / as sysdba\n");

expect.expect("SQL>");
expect.send("select sysdate from dual;\n");

expect.expect("\\$");
expect.send("quit;\n");

expect.expect("#");
expect.send("exit\n");

expect.expect("\\$");
expect.send("exit\n");

expect.expectEOF();
expect.close();

if (session != null) {
session.disconnect();
}
}

}


here's my output

Last login: Thu Feb 26 08:07:52 2015 from localhost

[***@myhost ~]$ sudo su - oracle
[sudo] password for leoks:
-bash-4.1$ source ./product/11.2.0/xe/bin/oracle_env.sh
-bash-4.1$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.2.0 Production on Thu Feb 26 08:08:35 2015

Copyright (c) 1982, 2011, Oracle. All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> select sysdate from dual;

SYSDATE
------------------
26-FEB-15

SQL> quit;
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 -
64bit Production
-bash-4.1$ exit
logout
[***@myhost ~]$ exit
logout

[]

Leo
Post by Ulises Vazquez
I cannot enter to Oracle SQL*Plus nor Oracle RMAN through a JSch ssh
session. The session hangs when the sqlplus or rman prompt is about to
appear in the output.
Is there a way to deal with this issue?
Ulises Vázquez Rocha
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website,
sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for
all
things parallel software development, from weekly thought leadership blogs
to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
JSch-users mailing list
https://lists.sourceforge.net/lists/listinfo/jsch-users
Leonardo K. Shikida
2015-02-26 11:18:35 UTC
Permalink
actually, you can remove this

expect.expect("#");
expect.send("exit\n");

[]

Leo
Post by Leonardo K. Shikida
Hi Ulises
Here's how I do it.
Use https://github.com/ronniedong/Expect-for-Java (it's just a single class)
import org.apache.log4j.Level;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class SQLPlusAutomation {
public static void main(String[] args) throws Exception {
String rootPassword = "...";
JSch jsch = null;
Session session = null;
jsch = new JSch();
session = jsch.getSession("leoks", "localhost");
session.setPassword(rootPassword);
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("PreferredAuthentications",
"publickey,keyboard-interactive,password");
session.connect(10 * 1000);
Channel channel = session.openChannel("shell");
Expect expect = new Expect(channel.getInputStream(),
channel.getOutputStream());
expect.setDefault_timeout(2);
expect.turnOffLogging();
expect.forwardInputStreamTo(System.out);
channel.connect();
expect.expect("password for leoks:");
expect.send("sudo su - oracle\n");
expect.expect("\\$");
expect.send(rootPassword+"\n");
expect.expect("\\$");
expect.send("source ./product/11.2.0/xe/bin/oracle_env.sh\n");
expect.expect("SQL>");
expect.send("sqlplus / as sysdba\n");
expect.expect("SQL>");
expect.send("select sysdate from dual;\n");
expect.expect("\\$");
expect.send("quit;\n");
expect.expect("#");
expect.send("exit\n");
expect.expect("\\$");
expect.send("exit\n");
expect.expectEOF();
expect.close();
if (session != null) {
session.disconnect();
}
}
}
here's my output
Last login: Thu Feb 26 08:07:52 2015 from localhost
-bash-4.1$ source ./product/11.2.0/xe/bin/oracle_env.sh
-bash-4.1$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.2.0 Production on Thu Feb 26 08:08:35 2015
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> select sysdate from dual;
SYSDATE
------------------
26-FEB-15
SQL> quit;
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 -
64bit Production
-bash-4.1$ exit
logout
logout
[]
Leo
Post by Ulises Vazquez
I cannot enter to Oracle SQL*Plus nor Oracle RMAN through a JSch ssh
session. The session hangs when the sqlplus or rman prompt is about to
appear in the output.
Is there a way to deal with this issue?
Ulises Vázquez Rocha
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website,
sponsored
by Intel and developed in partnership with Slashdot Media, is your hub
for all
things parallel software development, from weekly thought leadership
blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
JSch-users mailing list
https://lists.sourceforge.net/lists/listinfo/jsch-users
Loading...