Hi,

Please check this attached code ..

If i run the 120 to 130 commands ,the exec channel will execute.But if i try to 
transfer the files created (result of the commands) to my local machine i am 
not getting the files please check both Command call function and transfer 
files function in the attached file..

Please reply ASAP.

Thanks & Regards

Guru Prasad D S
Assistant System Engineer Trainee
Tata Consultancy Services
Tata Research Development & Design Centre ,
54, Hadapsar Industrial Estate,
Hadapsar,
Pune - 411 013,Maharashtra
India
Mailto: guruprasad...@tcs.com
Website: http://www.tcs.com
________________________________________
From: Guru Prasad  D S
Sent: Saturday, January 07, 2012 12:04 PM
To: Atsuhiko Yamanaka
Subject: RE: [JSch-users] How to run large remote commands in JSCH

Hi,

 Can u please check the code.

If u have any issues let me know.

Thanks in advance..

Regards

Guru Prasad D S
Assistant System Engineer Trainee
Tata Consultancy Services
Tata Research Development & Design Centre ,
54, Hadapsar Industrial Estate,
Hadapsar,
Pune - 411 013,Maharashtra
India
Mailto: guruprasad...@tcs.com
Website: http://www.tcs.com
________________________________________
From: Atsuhiko Yamanaka [y...@jcraft.com]
Sent: Friday, January 06, 2012 7:24 PM
To: Guru Prasad  D S
Cc: JSch-users@lists.sourceforge.net
Subject: Re: [JSch-users] How to run large remote commands in JSCH

Hi,

   +-From: <guruprasad...@tcs.com> ---------
   |_Date: Fri, 6 Jan 2012 18:45:48 +0530 __
   |
   |I have scenario like I need to connect to a remote machine and i
   |need to run 135 commands.
   |Yes, i'm able to open a exec channel and executing the command one
   |by one in loop
   |But after running 58 commands the program quits saying the channel
   |is not opened

Have you invoked Channel#disconnect() method?
If you can show your code, the reason for the problem may be found.


Sincerely,
--
Atsuhiko Yamanaka
JCraft,Inc.
1-14-20 HONCHO AOBA-KU,
SENDAI, MIYAGI 980-0014 Japan.
Tel +81-22-723-2150
Skype callto://jcraft/
Twitter: http://twitter.com/ymnk
Facebook: http://facebook.com/aymnk
=====-----=====-----=====
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


package com.tcs.etransform.datacollection;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class Query {
        
        private DocumentBuilderFactory docBuilderFactory = 
DocumentBuilderFactory.newInstance();
        private DocumentBuilder docBuilder;
        static Logger  log = Logger.getLogger(Query.class);
         private String framework = "embedded";
         private String driver = "org.apache.derby.jdbc.EmbeddedDriver";
         private String protocol = "jdbc:derby:";
        public String runQuery(String query, Properties prop,int queryStatus, 
String currentDirectory, String decPassword, String SheetName, Session 
session,String currDirOfRemote) throws Exception {

                log.debug("runQuery method starts" + "for query :" +query );
                                      
                queryStatus=0;
                
                //Creating path of csv 

              
                String 
local=currentDirectory+"/"+prop.getProperty("GeneratedCSVs")+"/"+SheetName+".csv";

                String queryCommnadPath=prop.getProperty("QueryCommandPath");
                
                // Name of Directory to be created in ADDM machine to store all 
generated csv's
                String nameOfDir = prop.getProperty("nameOfDir");
                String cmd1 = prop.getProperty("cmd") + " " + nameOfDir ;

                //Form a command to to run the query
                
            System.out.println("Connected");
             

             //Creating name of csv to be stored in ADDM mahcine
             String remote = currDirOfRemote.trim() + "/"+nameOfDir+"/" 
+SheetName+".csv" ;

             log.debug("Csv name in ADDM machine is :" + remote);
          
            System.out.println(remote);
              
             
              
              // Creating channel to execute the query command and save csv on 
remote machine
              Channel channel2=session.openChannel("exec");
              String cmd=queryCommnadPath+" --password="+decPassword+" --csv  
--file="+remote+" "+query;

                        System.out.println("formed command is" + cmd);
                        log.debug("Formed command is :" + cmd);
                        
              ((ChannelExec)channel2).setCommand(cmd);
             

              channel2.setInputStream(null);
             

              ((ChannelExec)channel2).setErrStream(System.err);

              InputStream in1=channel2.getInputStream();
              channel2.connect();
          byte[] tmp1=new byte[1024];
              while(true){
                while(in1.available()>0){
                  int i=in1.read(tmp1, 0, 1024);
                  if(i<0)break;
                  
                  System.out.print(new String(tmp1, 0, i));
                }
              queryStatus = channel2.getExitStatus();
                if(channel2.isClosed()){
                  System.out.println("exit-status of execute query: 
"+channel2.getExitStatus());
                  break;
                  
                }
              }
              try
              {
                  Thread.sleep(100);
              }
              catch(Exception e)
              {
                  System.out.println(e.getMessage());
              }
              channel2.disconnect();
              
              
              //Calling transfer method in order to save csv file in local 
machine
              transferFiles(session,remote,local,currentDirectory);
             
              
//            session.disconnect();
              log.debug("Query status for query is :" + queryStatus);
              log.debug("runQuery method ends" + "fo query :" +query);
              return local;
                 
                }
        

        
        public void transferFiles(Session session, String remote, String 
local,String currentDirectory) throws JSchException, SftpException, 
FileNotFoundException, IOException, ParserConfigurationException, SAXException
        {
                 try{
                         
                         // Creating sftp channel in order to transfer files 
                  Channel channel1 = session.openChannel("sftp");
                        channel1.connect();
                        ChannelSftp sftpChannel = (ChannelSftp) channel1;
                     
                        sftpChannel.get(remote, local);
                        sftpChannel.exit();
                    channel1.disconnect();
                }           
                    
                  catch(FileNotFoundException fe)
                        {
                                log.error("File not found exception occured");
                                log.error(getStackTrace(fe));
                                log.error(fe.getMessage());
                        }catch(IOException ie)
                        {
                                log.error("IO exception occured");
                                log.error(getStackTrace(ie));
                                log.error(ie.getMessage());
                        }catch(Exception ex)
                        {
                                log.error("Exception while running 
CompleteData");
                                log.error(getStackTrace(ex));
                                log.error(ex.getMessage());
                        }
                        
                      
                    
                       
                    } catch (JSchException e) {
                        e.printStackTrace(); 
                    } catch (SftpException e) {
                        e.printStackTrace();
                    }
                   
                  //return local;
        }
        
------------------------------------------------------------------------------
Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex
infrastructure or vast IT resources to deliver seamless, secure access to
virtual desktops. With this all-in-one solution, easily deploy virtual 
desktops for less than the cost of PCs and save 60% on VDI infrastructure 
costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox
_______________________________________________
JSch-users mailing list
JSch-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jsch-users

Reply via email to