• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3426
  • Last Modified:

excute sh file in unix server using jsch-0.1.42.jar leb

im trying to run unix script for example ABQ_MIS_script.sh it dosent work this an update script but when i try to run unix command like ls -ltr it will work how can run the script
import com.jcraft.jsch.*;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch.*;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
import java.io.*;

public class testrad {

    public static void main(String args[]) throws IOException
    {
        String user = "xxxxx";
        String host = "xxxxxx";
      String cmd = "ls" ;
      
      JSch jsch = new JSch();
        try
        {
            Session session = jsch.getSession(user, host, 22);
            session.setPassword("xxxxx");
            UserInfo usrInfo = new MyUserInfo();
            session.setUserInfo(usrInfo);
            session.connect();
            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(cmd);
          
            channel.setXForwarding(true);
            
            InputStream in = channel.getInputStream();
            channel.connect();
            channel.setInputStream(System.in);
             
            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())
                {
                    in.close();
                    break;
                }
                try
                {
                    Thread.sleep(1000);
                }
                catch (Exception ee)
                {
                }
            }
            channel.disconnect();
            session.disconnect();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("Exception" + e);
        }
    }

    public static class MyUserInfo implements UserInfo {

        public String getPassword()
        {
            return "password";
        }

        public String getPassphrase()
        {
            return "";
        }

        public boolean promptPassword(String arg0)
        {
            return true;
        }

        public boolean promptPassphrase(String arg0)
        {
            return true;
        }

        public boolean promptYesNo(String arg0)
        {
            return true;
        }

        public void showMessage(String arg0)
        {
        }
    }
}

Open in new window

0
osama120
Asked:
osama120
  • 28
  • 23
  • 5
1 Solution
 
CEHJCommented:
>> it dosent work

Meaning ..?
0
 
sumitchejaraCommented:
How are you passing the name of sh file. In the snippet above you have mentioned just the ls command. Have you checked that sh file is having execute permission. Also try by giving a command as this "./myshfilename.sh"
0
 
osama120Author Commented:
the  ABQ_MIS_script.sh  script making update to a table so nothing happen to the table when i run the ls -ltr it will give me -rw-r--r--   1 pwrcard  dba               0 Jan 26 13:56 SPEC_SCRIPT.sql
-rw-r--r--   1 pwrcard  dba               0 Jan 26 13:56 BODY_SCRIPT.sql
-rw-------   1 pwrcard  dba             376 Feb 01 14:39 dead.letter
drwxr-sr-x   9 pwrcard  dba             512 Feb 01 15:02 usr
-rw-r--r--   1 pwrcard  dba               0 Apr 27 15:11 ls
-rw-r--r--   1 pwrcard  dba               0 Apr 27 15:21 ABQ_MIS_script.sh
-rw-r--r--   1 pwrcard  dba               0 Apr 27 15:42 YES
BUILD SUCCESSFUL (total time: 1 second)
but wihn i run the ABQ_MIS_script.sh it will give me
init:
deps-jar:
compile-single:
run-single:
BUILD SUCCESSFUL (total time: 1 second)
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
osama120Author Commented:
im passing the file like this  String cmd = "ABQ_MIS_script.sh "
0
 
CEHJCommented:
>>-rw-r--r--   1 pwrcard  dba               0 Apr 27 15:21 ABQ_MIS_script.sh

Is not executable, so needs to be run by a shell, such as bash
0
 
sumitchejaraCommented:
Your ABQ_MIS_script file is not having execute permission. try giving it execute permission by runninng following command

>chmod 777 ABQ_MIS_script.sh

After this run the ls command and you should see execute permision associated with it.

Try your program now.
0
 
sumitchejaraCommented:
Also check if the path of ABQ_MIS_script.sh is added in your PATH variable of environment.
0
 
CEHJCommented:
You don't actually need to make any changes to the file if you don't want - simply pass it to bash for execution
0
 
osama120Author Commented:
how can pass it  bash
0
 
sumitchejaraCommented:
use following in your code

      String cmd = "sh  ABQ_MIS_script.sh" ;

alternatively

      String cmd = "bash  ABQ_MIS_script.sh" ;

0
 
CEHJCommented:
It's probably safer to use the full path to bash
0
 
osama120Author Commented:
i make chmod 777 and i make the  
String cmd = "bash /pcard16/pwrcard/usr/shl/ABQ_MIS_script.sh" ;
and i i try
String cmd = "sh  ABQ_MIS_script.sh" ;
 String cmd = "bash  ABQ_MIS_script.sh" ;
but still nothing  happen
0
 
CEHJCommented:
Please post that script if you can
0
 
osama120Author Commented:
this the result i get
init:
deps-jar:
compile-single:
run-single:
BUILD SUCCESSFUL (total time: 1 second)

public static void main(String args[]) throws IOException
    {
        String user = "xxxxxxx";
        String host = "xxx.xx.xx.xx";
      String cmd = "bash /pcard16/pwrcard/ABQ_MIS_script.sh" ;
      
      JSch jsch = new JSch();
        try
        {
            Session session = jsch.getSession(user, host, 22);
            session.setPassword("xxxxxx");
            UserInfo usrInfo = new MyUserInfo();
            session.setUserInfo(usrInfo);
            session.connect();
            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(cmd);
          
            channel.setXForwarding(true);
            
            InputStream in = channel.getInputStream();
            channel.connect();
            channel.setInputStream(System.in);
             
            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())
                {
                    in.close();
                    break;
                }
                try
                {
                    Thread.sleep(1000);
                }
                catch (Exception ee)
                {
                }
            }
            channel.disconnect();
            session.disconnect();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("Exception" + e);
        }
    }

    public static class MyUserInfo implements UserInfo {

        public String getPassword()
        {
            return "password";
        }

        public String getPassphrase()
        {
            return "";
        }

        public boolean promptPassword(String arg0)
        {
            return true;
        }

        public boolean promptPassphrase(String arg0)
        {
            return true;
        }

        public boolean promptYesNo(String arg0)
        {
            return true;
        }

        public void showMessage(String arg0)
        {
        }
    }
}

Open in new window

0
 
CEHJCommented:
Please post that script if you can
0
 
osama120Author Commented:
sorry  this is  the script
sqlplus -S backup_oper/backup006 << EOF
UPDATE powercard.platinum_card  SET flag = 'YES' where flag <> 'YES';
COMMIT;
EOF
0
 
CEHJCommented:
Firstly, open up a normal ssh session to the server and post the output of the following - is the access time consistent with your attempts to run the script?


stat /pcard16/pwrcard/ABQ_MIS_script.sh

Open in new window

0
 
osama120Author Commented:
pwrcard:abqp615a:/pcard16/pwrcard>stat /pcard16/pwrcard/ABQ_MIS_script.sh
ksh: stat:  not found.
0
 
CEHJCommented:
Yuk, no stat. In that case, try


ls -ul /pcard16/pwrcard/ABQ_MIS_script.sh

Open in new window

0
 
osama120Author Commented:
pwrcard:abqp615a:/pcard16/pwrcard>ls -ul /pcard16/pwrcard/ABQ_MIS_script.sh
-rw-r--r--   1 pwrcard  dba               0 Apr 28 10:14 /pcard16/pwrcard/ABQ_MIS_script.sh
0
 
osama120Author Commented:
if i run this coomand on my programe it will work and it give me the same result i post
ls -ul /pcard16/pwrcard/ABQ_MIS_script.sh
-rw-r--r--   1 pwrcard  dba               0 Apr 28 10:14 /pcard16/pwrcard/ABQ_MIS_script.sh
and if i run this command
sh /pcard16/pwrcard/usr/shl/ABQ_MIS_script.sh on unix server it will work but in my programme it will not work
0
 
osama120Author Commented:
if i deby my class it will give this result
init:
deps-jar:
compile-single:
ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):  [../../../src/share/back/util.c:820]
debug-single:
BUILD SUCCESSFUL (total time: 6 seconds)
0
 
CEHJCommented:
>>if i run this coomand on my programe it will work and it give me the same result i post

OK, so the code works for that command.

>>

and if i run this command
sh /pcard16/pwrcard/usr/shl/ABQ_MIS_script.sh on unix server it will work but in my programme it will not work
>>

As i mentioned above, you should probably use the full path to the shell interpreter, perhaps in your case /bin/ksh

>>ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2

If the code is working for some commands, i fail to see how that error is coming about. What command are you executing when you get that error?

0
 
osama120Author Commented:
sh /pcard16/pwrcard/usr/shl/ABQ_MIS_script.sh
0
 
CEHJCommented:
That error looks like a JNI-related bug. I just executed the attached using the principles i described above without error
SSHExec.java
0
 
osama120Author Commented:
sorry maybe i write something wrong when i run this coomand
sh /pcard16/pwrcard/usr/shl/ABQ_MIS_script.sh
it will give me
Listening on 4110
User program running
User program finished
tell me where io have to put the path  /bin/ksh
0
 
CEHJCommented:
If sh is working, then don't worry
0
 
osama120Author Commented:
see im using putty to connect to unix when im if type
sh /pcard16/pwrcard/usr/shl/ABQ_MIS_script.sh
in putty it will work but in my programme it will not work
0
 
CEHJCommented:
Execute the following in putty and post the result
echo ${SHELL}

Open in new window

0
 
osama120Author Commented:
/usr/bin/ksh
0
 
CEHJCommented:
That's what you should use then
0
 
osama120Author Commented:
where i should use it
0
 
CEHJCommented:

String cmd = "/usr/bin/ksh /pcard16/pwrcard/ABQ_MIS_script.sh" ;

Open in new window

0
 
osama120Author Commented:
String cmd = "/usr/bin/ksh /pcard16/pwrcard/ABQ_MIS_script.sh" ;
i try this one it give the same result

init:
deps-jar:
compile-single:
run-single:
BUILD SUCCESSFUL (total time: 2 seconds)
0
 
CEHJCommented:
Try it with the code i posted
0
 
CEHJCommented:
You could do that with the following (choose correct host, user, private key file (if applicable))
public class SSH {

    public static void main(String[] args) throws Exception {
	SSHExec ssh = new SSHExec("x.y.com", 22, "username");
	ssh.useIdentity("/home/someone/.ssh/id_rsa", "yourpphrase");
	ssh.executeCommand("/usr/bin/ksh /pcard16/pwrcard/ABQ_MIS_script.sh");
	System.out.println(ssh.getStandardOutput());
	System.out.println(ssh.getStandardError());
    }
}

Open in new window

0
 
osama120Author Commented:
i try it like this
 */
public class SSHExecTest {
    public static void main(String[]  args) throws JSchException
    {
         String user = "pwrcard";
        String host = "123.123.123.123";
         String cmd = "usr/bin/ksh /pcard16/pwrcard/ABQ_MIS_script.sh" ;

       SSHExec osa=new SSHExec(host,22,user);
       osa.usePassword("XXXX");
        osa.executeCommand(cmd);
    }

}
is  this right
0
 
CEHJCommented:
>>String cmd = "/usr/bin/ksh /pcard16/pwrcard/ABQ_MIS_script.sh" ;
0
 
osama120Author Commented:
i try your class i didn't get what is the mean of this
ssh.useIdentity("/home/someone/.ssh/id_rsa", "yourpphrase");
0
 
CEHJCommented:
That's for when you're using private key authentication (not password)
0
 
osama120Author Commented:
so i can commite this line and complete but still the problem the same result i get not hapen
0
 
osama120Author Commented:
i mean comment
0
 
CEHJCommented:
What does the getStandardError produce?
0
 
osama120Author Commented:
there no error but when i try to run this code in putty it will not work
pwrcard:abqp615a:/pcard16/pwrcard/usr/shl>/usr/bin/ksh /pcard16/pwrcard/ABQ_MIS_script.sh
pwrcard:abqp615a:/pcard16/pwrcard/usr/shl>
0
 
osama120Author Commented:
where i try this code in your programme
ssh.executeCommand("sh /pcard16/pwrcard/usr/shl/update_pt.sh" );
it will give me
init:
deps-jar:
compile-single:
run-single:

pcard16/pwrcard/usr/shl/update_pt.sh: sqlplus:  not found.
BUILD SUCCESSFUL (total time: 1 second)
0
 
CEHJCommented:
>>there no error but when i try to run this code in putty it will not work

OK. If it doesn't work in Putty, it's probably not going to work from Java. Please define 'work' - are you saying all flags are NOT set to 'YES'?
0
 
osama120Author Commented:
yes the table dose,t get updated
0
 
osama120Author Commented:
dosen't get updated
0
 
osama120Author Commented:
is there any solution for this problem
0
 
CEHJCommented:
Have you got rights to commit the update? It might be a good idea to do a select after the update for debugging purposes
0
 
osama120Author Commented:
if i run the file  update_pt.sh  on putty the table will updated successfully
0
 
sumitchejaraCommented:
You have this error in your output.

       >>>pcard16/pwrcard/usr/shl/update_pt.sh: sqlplus:  not found.

please add path of sqlplus in your PATH variable of env. Alternatively give a full path of sqlplus in your sh file.

Also make sure your env variables required for ORACLE are all set up and correct.

You said it runs from the putty. Are you executing java programm from same shell?
0
 
osama120Author Commented:
i  try this command
ssh.executeCommand("/oracle/10g/product/oracle/bin/sqlplus /pcard16/pwrcard/usr/shl/update_pt.sh" );and give me this error
rror 6 initializing SQL*Plusessage file sp1<lang>.msb not foundP2-0750: You may need to set ORACLE_HOME to your Oracle software directory
BUILD SUCCESSFUL (total time: 1 second)
 howcan set the path
0
 
CEHJCommented:
In update_pt.sh, set the following as its first line
export ORACLE_HOME=<the full path to whatever Oracle defines as home without these angle brackets>

Open in new window

0
 
osama120Author Commented:
Thanks alot
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 28
  • 23
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now