Connecting to remote unix box from java and running unix script


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Connecting to remote unix box from java and running unix script

  1. #1
    Join Date
    Apr 2008
    Posts
    2

    Connecting to remote unix box from java and running unix script

    Hi all,
    I have requirement like connecting to remote unix box from java and running unix scripts.

    My java code is as follows
    Code:
    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[])
    {
    String user="uname";
    String host="hostname";
    String cmd="sh rdh_count.sh r10040";
    JSch jsch = new JSch();
    try
    {Session session=jsch.getSession(user,host,22);
    session.setPassword("psw"); 
    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)
    { }
    }
    }
    The unix script(rdh_count.sh ) i'm using is as follows
    Code:
    dir=/home/oracle/dba
    if [ $# -ne 1 ]
    then
    echo "Enter retailer ID as parameter"
    else
    retailer=$1
    . $dir/.CBP_LOAD $retailer
    echo $password
    count=`sqlplus <<END
    $retailer/$password@cbpqc
    SELECT COUNT(1) FROM tablename@dblink WHERE MARKETKEY IN (SELECT MKTKEY FROM STG_DIM_MARKET); 
    exit;
    END`
    echo $count | grep -i "ERROR"
    if [ $? -eq 0 ]
    then
    echo "Error in getting the count"
    exit
    else
    echo "There are $count rows for this retailer" 
    fi
    fi
    Here i'm able to connect to unix and run unix commands like (ls) but when i invoke the unix script from java , the script is actually invoked but in the script i need to connect to db and run some select statements. This is not working. The result i'm getting is

    psw
    There are rows for this retailer.

    Iam not able to print the value of count variable in my java code. It is just giving the blank space in the place of printing the count variable.But it is printing the password. so there is a problem in running dml commands in sqlplus using java code.

    plz help me guys. It is very urgent.plz.............

    Thanks in advance.

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    I'm not understanding what isn't working. Are you getting errors or just nothing or what is happening?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Apr 2008
    Posts
    2

    connecting to unix box from java

    Quote Originally Posted by Hack
    I'm not understanding what isn't working. Are you getting errors or just nothing or what is happening?
    Hi Hack,
    Thanks for your response. Actually I have found that when i call the script from java code, the script is not connecting to sqlplus and hence no dml commands are executed. The other parts of the script are working fine except connecting to sqlplus.
    so what is not working is connecting to sqlplus from oracle script ,when the script is invoked from java code. In this case no error is thrown. The java code is working fine but the results are not as expected. The place where i'm trying to print the result fetched from database through sqlplus is appearing just blank and all the other details are printed as expected.But when i run the same script directly in unix box the script is connecting to sqlplus and fetching the results.
    Some people told that we need to set some environmental variables. But i'm not sure of what and where to set those variables. Pz help me regarding this.

    Thanks..
    Last edited by shriamar; 04-18-2008 at 12:44 PM.

Similar Threads

  1. Replies: 3
    Last Post: 03-06-2008, 08:57 AM
  2. Replies: 0
    Last Post: 10-10-2006, 04:37 AM
  3. Replies: 1
    Last Post: 06-12-2006, 12:20 PM
  4. Connecting to java chat server from php
    By prasad in forum Java
    Replies: 0
    Last Post: 05-15-2006, 10:45 AM
  5. .NET vs. Enterprise Java: Who's Got Better Security?
    By Glen Kunene in forum Talk to the Editors
    Replies: 17
    Last Post: 03-23-2002, 01:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center