0

I am looking to connect to a remote server using sshj and execute some commands. The commands im looking to do are in order: 1. log on through putty type in username and password 2. then sudo su - oracle 3. then password again 4. then "sqlplus /nolog" 5. then "connect as sysdba" 6. then a couple of sql commands 7. Then exit back out and do a imdmp

Im looking for the best/easiest way to go about this. Here is what I have done so far.

i

mport java.io.IOException;
import java.util.concurrent.TimeUnit;

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.Base64.OutputStream;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;

public class FileCreator {

    public static void main(String[] args) throws IOException {
        final SSHClient client = new SSHClient();
        client.addHostKeyVerifier(new PromiscuousVerifier());
        client.connect("192.168.x.x");

        client.authPassword("username", "password");
        Session session = client.startSession();
        Command cmd = session.exec("sudo su - oracle");
        cmd.join(1, TimeUnit.SECONDS);
        session.close();

        session = client.startSession();
        Command cmd21 = session.exec("password");
        cmd21.join(1, TimeUnit.SECONDS);
        session.close();

        session = client.startSession();
        Command cmd3 = session.exec("1");
        cmd3.join(1, TimeUnit.SECONDS);
        session.close();

        session = client.startSession();
        Command cmd4 = session.exec("sqlplus /nolog");
        cmd4.join(1, TimeUnit.SECONDS);
        session.close();

        session = client.startSession();
        Command cmd5 = session.exec("connect / as sysdba");
        cmd5.join(1, TimeUnit.SECONDS);
        session.close();

        session = client.startSession();
        Command cmd2 = session.exec("drop user STN_x cascade");
        cmd2.join(1, TimeUnit.SECONDS);
        session.close();

        session = client.startSession();
        Command cmd20 = session.exec("drop user STN_x_dba cascade;");
        cmd20.join(1, TimeUnit.SECONDS);
        session.close(); 

        session = client.startSession();
        Command cmd6 = session.exec("");
        cmd6.join(1, TimeUnit.SECONDS);
        session.close();






        client.disconnect();

    }

}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Is this working fine without any issue ? – Chamara Maduranga Mar 29 '18 at 09:47
  • In the console I get results like this " [main] INFO net.schmizz.sshj.connection.channel.direct.SessionChannel - Will request to exec `sudo su - oracle`" As far as I Know the commands do not actually work though (as in no changes are made in the database). Is there any way of checking besides this? – user9558800 Mar 29 '18 at 10:02
  • check this https://stackoverflow.com/questions/22723538/execute-sequense-of-commands-in-sshj – Jose Da Silva Gomes Mar 30 '18 at 03:16

0 Answers0