Jump to content

[SOLVED] Run ncftpput from php and log the exit code


fj1200

Recommended Posts

I originally put this under the PHP Applications forum but didn't get any replies so assume it was the wrong place - so trying again here...    (sorry - it's quite a long one.)

 

I'm experimenting with different options for ftp and want to use my personal fave ftp tool - ncftp.  I like ncftp because it's very fast, very simple and has 12 well-documented exit codes.

 

I want to run it from a php script and then return the exit code to a web page live report.  I've looked at exec() which works and so I'm happy with that, but how do I get the exit code?  I can do it in VBS easily and do so a fair bit, but I'm still very much learning with php.

 

Looked at return() and exit() but not sure they are what I want.  Can anyone throw any light on this for me please or point me in the right direction?

 

(PHP running under WAMP 1.7.0 on Windows Server 2003)

 

Simple script is as follows (the exit code mappings are standard ncftp, btw and what I want to display on screen.):

 

<?php

$cmd = '"ncftpput" -u linktest -p linktest 192.168.0.10 / C:\wamp\www\ftp\test.txt';
$nftp = exec($cmd,$output);

if ($output == "0")    $output = ("Success");
if ($output == "1")    $output = ("Could not connect to remote host.");
if ($output == "2")    $output = ("Could not connect to remote host - timed out.");
if ($output == "3")    $output = ("Transfer failed.");
if ($output == "4")    $output = ("Transfer failed - timed out.");
if ($output == "5")    $output = ("Directory change failed.");
if ($output == "6")    $output = ("Directory change failed - timed out.");
if ($output == "7")    $output = ("Malformed URL.");
if ($output == "8")    $output = ("Usage error.");
if ($output == "9")    $output = ("Error in login configuration file.");
if ($output == "10")    $output = ("Library initialization failed.");
if ($output == "11")    $output = ("Session initialization failed.");

echo $output;

?>

 

The idea is to display the output live in a web page.  However I just get a blank page.  The file copies perfectly, if I use "-d stdout" and pipe it to a text file it all looks good, but in the browser I just get 'Array'.

 

If I echo $nftp I get the last line of the conversation - "221: Goodbye".

 

Now running the above code as a function;

 

<?php
...
function nftp($server)
{
exec($cmd,$output);
.. <code above> ..

and then to see what's coming back at me I've added this at the end:

$key = array_keys($output);
$keys = print_r($key);
Return $keys;
}

-  So now for a successful ftp transfer I get

 

Array (

 

    * => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 13 [14] => 14 [15] => 15 [16] => 16 [17] => 17 [18] => 18 [19] => 19 [20] => 20 [21] => 21 [22] => 22 [23] => 23 [24] => 24 [25] => 25 [26] => 26 [27] => 27 [28] => 28 [29] => 29 [30] => 30 [31] => 31 [32] => 32 [33] => 33 [34] => 34 [35] => 35 [36] => 36 ) Array (

    * => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 13 [14] => 14 [15] => 15 [16] => 16 [17] => 17 [18] => 18 [19] => 19 [20] => 20 [21] => 21 [22] => 22 [23] => 23 [24] => 24 [25] => 25 [26] => 26 [27] => 27 [28] => 28 [29] => 29 [30] => 30 [31] => 31 [32] => 32 [33] => 33 [34] => 34 [35] => 35 [36] => 36 ) 1

 

 

If I put a non-existent IP address :

$server = "192.168.0.3";
nftp($server);
print nftp($server);

 

I get:

 

Array (

 

    * => 0 [1] => 1 ) Array (

    * => 0 [1] => 1 ) 1

 

 

(without the bullet points, of course!)

 

What I need are the codes '1','2,....'11' and depending on what code I get back will depend on what gets displayed.  This will be monitoring some critical servers around the country.  I have a VB script that does it but is doing my head in with 1 line that refuses to work so I want to try with php.

 

Been playing all week, still no further on.  Any help greatly appreciated. Also 'discovered' what PEAR is all about this week, made a HUGE difference to some stuff I've been working on but no help for this project I think.

Link to comment
Share on other sites

Yup - that's got it.  why, oh WHY did I not see that??  ???

 

Many thanks thorpe.

 

This works:

 

<?php

$return = -1;
$cmd = '"D:\Program Files\NcFTP\ncftpput" -u linktest -p linktest 192.168.2.2 / C:\wamp\www\Linktest\nft.txt';

exec($cmd,$output,$return);
if ($return == "0") 	$return = ("Success");
if ($return == "1") 	$return = ("Could not connect to remote host.");
if ($return == "2") 	$return = ("Could not connect to remote host - timed out.");
if ($return == "3") 	$return = ("Transfer failed.");
if ($return == "4") 	$return = ("Transfer failed - timed out.");
if ($return == "5") 	$return = ("Directory change failed.");
if ($return == "6") 	$return = ("Directory change failed - timed out.");
if ($return == "7") 	$return = ("Malformed URL.");
if ($return == "8") 	$return = ("Usage error.");
if ($return == "9") 	$return = ("Error in login configuration file.");
if ($return == "10") 	$return = ("Library initialization failed.");
if ($return == "11") 	$return = ("Session initialization failed.");

echo $return;
?>

 

Now I can do some interesting stuff with it.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.