0

I have a script that fetches data from a site. Basically it is divided into two section. 1.executes commands on a remote machine and saves output in a file 2.read the contents of a file. For some reason it works from time to time. Section 1 always works (checked the remote machine and found the files). The problem is related to cat. I've added to my code the option to dump the results of "CAT" command to a file. Sometimes it has info sometimes it doesn't. However the file is always created! The nodes i'm querying are the same. Timeout of execution of Section 1 on a remote server is 11-12 secs.

Thanks beforehand.

$ssh->exec("rm toolkit/mybatch/$newfileid");
                    $ssh->exec("mobatch $newsiteid 'lt all;ue print -admitted;' toolkit/mybatch");
                    $ssh->setTimeout(15);                       
                    echo $ssh->exec('cat ' . escapeshellarg("toolkit/mybatch/$newfileid") . '| grep -A 10 \'$ ue print \' > toolkit/mybatch/traffic.txt');
                    $traffic = $ssh->exec("cat toolkit/mybatch/traffic.txt");
                    $traffic = substr($traffic,21,-16);
                    $ssh->disconnect();
                    echo $traffic;

I've updated the code above, however, it worked several times , but after deletion of old files, it only creates "traffic.txt" and sometimes it has info in it, sometimes no. Also, the file "traffic.txtescapeshellarg" is not created anymore. So i was forced to go back to my previous solution and read the "traffic.txt".

  • Remember if you're taking user input and passing it through to shell commands you need to [properly escape those variables](http://php.net/manual/en/function.escapeshellarg.php) or bad things happen. – tadman Sep 15 '17 at 16:10
  • Tried as you suggested, still same result: `$ssh->exec('cat toolkit/mybatch/'.escapeshellarg($newfileid).' | grep -A 10 \'$ ue print\'> toolkit/mybatch/traffic.txt'); ` – Ashot Avetisyan Sep 15 '17 at 16:41
  • Correct usage would be `'cat ' . escapeshellarg("toolkit/mybatch/$newfileid")` but you're on the right track generally. Don't forget you can [avoid `cat`](https://stackoverflow.com/questions/11710552/useless-use-of-cat) in many cases like this by using the proper redirection operation: `<` could fix it. – tadman Sep 15 '17 at 16:47
  • 1
    If you've got a solid solution it's worth adding a self-answer here. Good going! – tadman Sep 15 '17 at 17:13
  • 1
    For updates like that you should edit your question and put in the code. It's very hard to read in comments. – tadman Sep 15 '17 at 19:58

1 Answers1

0

So, the solution was very simple. All i needed to do is adding "timeout". The final code:

$ssh->exec("rm toolkit/traffic/$newfileid");
                    $ssh->setTimeout(0);
                    $ssh->exec("mobatch $newsiteid 'lt all;ue print -admitted;' toolkit/traffic");
                    sleep(8);                   
                    $ssh->exec('cat ' . escapeshellarg("toolkit/traffic/$newfileid") . '| grep -A 10 \'$ ue print \' > toolkit/traffic/traffic.txt');

                    $traffic = $ssh->exec("cat toolkit/traffic/traffic.txt");
                    $traffic = substr($traffic,21,-83);
                    echo $traffic;