简体   繁体   中英

Cannot read an output file created by a script executed using JSch

public String getMacAndServerIp(Dataset ds){
    TestLogger.info(logger,"*************   Started test method for getMacAndServerIp *****************");
    String macaddserverip="";
    String user=admin;
    String host=10.121.172.9;
    String pswd=admin;
    int port=22;
    String path= "/opt/conf/files/";

    TestLogger.info(logger,"values "+"User value::"+user+ ", "+ "Host value:"+host + "Password value:"+pswd + "Port Num'" +port);
    String command= "cd "+path+ "; ./gettingMacAndIPadd.sh &";
    TestLogger.info(logger,"Command ::"+command);

    try{
        JSch jsch = new JSch();
        com.jcraft.jsch.Session session = jsch.getSession(user, host, port);
            session.setPassword(pswd);
            session.setConfig("StrictHostKeyChecking", "no");
        TestLogger.info(logger,"Establishing Connection...");
        session.connect();
        TestLogger.info(logger,"Connected");

        Channel channel=session.openChannel("exec");
        TestLogger.info(logger,"Channel Opened");
        ((ChannelExec) channel).setCommand(command);
        TestLogger.info(logger,"Command Set");
        channel.setInputStream(null);
        ((ChannelExec) channel).setErrStream(System.err);
        InputStream in = channel.getInputStream();
        channel.connect();
        TestLogger.info(logger,"Channel connected");
        try{
            Thread.sleep(1000);
        }
        catch(Exception ee){
            ee.printStackTrace();
        }

        BufferedReader reader=new BufferedReader(new FileReader("/opt/conf/files//output786.txt"));
        TestLogger.info(logger,"Reading data from file"+reader);

        while((macaddserverip=reader.readLine()) !=null){
            TestLogger.info(logger,"Mac Address and ServerIp value"+macaddserverip);
        }

        TestLogger.info(logger,"Value read from the output of script::"+macaddserverip);

    }
    catch(Exception e){
        e.printStackTrace();
    }

    TestLogger.info(logger,"*************   Test passed for getMacAndServerIp *****************");
    return macaddserverip;

}

Script File(gettingMacAndIPadd.sh) - To get the ipaddress and hardwarewaddress and store it in file output786.txt

ipaddress=`ifconfig | grep -A1 eth0 | grep "inet addr:" | awk '{print $2}' | cut -d":" -f2`
hwadd=`ifconfig | grep eth0 | awk '{print $5}'`
echo $ipaddress $hwadd > output786.txt

My scenario is like above script needs tobe executed and those values should be stored in file using JSCH and access those values using FileReader.

Issue I'm facing is like the output786.txt file is not creating when i ran the above program and not creating in the location /opt/conf/files/.

Please help me out in figuring this issue. Could anyone suggest how can I do this scenario.

The script stores the output to a file on the server . You cannot read a file from a remote server using using the FileReader .

Your should:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM