spec36 Posted December 28, 2011 Share Posted December 28, 2011 I am trying to execute a command line tool, but am having a problem adding a variable to the line. Here is the code $filename = "devices.txt"; $handle = fopen($filename, 'r'); while (!feof($handle)) { $devices = fgets($handle, 4096); $query = 'ipmitool -I lanplus -H 192.168.1.1 -U admin -P adminpass sdr list'; passthru($query); } flcose($handle); On this line I don't want to have the IP manually added like so. $query = 'ipmitool -I lanplus -H 192.168.1.1 -U admin -P adminpass sdr list'; Instead I want to be able to insert the $devices variable which contains the values from the text file so it would look something like this: $query = 'ipmitool -I lanplus -H $devices -U admin -P adminpass sdr list'; Problem is the command line tool does not run this way. How do I properly add the $devices variable so the command line tool ipmitool will run with its value? Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/253956-php-and-command-line-tools/ Share on other sites More sharing options...
kicken Posted December 28, 2011 Share Posted December 28, 2011 $query = 'ipmitool -I lanplus -H '.$devices.' -U admin -P adminpass sdr list'; Variables are not translated inside single-quote strings. You have to concatenate them, or change your string to use double-quotes. Quote Link to comment https://forums.phpfreaks.com/topic/253956-php-and-command-line-tools/#findComment-1301934 Share on other sites More sharing options...
spec36 Posted December 28, 2011 Author Share Posted December 28, 2011 I tried to concatenate the string with what you suggested, but it still does not insert the device name from the text file. Example: Putting this code in and concatenating $query = 'ipmitool -I lanplus -H '.$devices.' -U admin -P adminpass sdr list'; If I echo the value of $query below it outputs this using the above structure on the command. ipmitool -I lanplus -H -U admin -P adminpass sdr list Notice the variable should have inserted the value next to the -H, but instead it is just missing from the command? Also, if I echo the output of $devices I do see the proper names listed. So I know the problem is not with reading the file or with the loop, but something with the way the command is being sent to the shell I guess. Any other ideas? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/253956-php-and-command-line-tools/#findComment-1301940 Share on other sites More sharing options...
ManiacDan Posted December 28, 2011 Share Posted December 28, 2011 Then $devices isn't being set like you think it is. Use something like file() or file_get_contents() instead of fgets with a line limiter. Quote Link to comment https://forums.phpfreaks.com/topic/253956-php-and-command-line-tools/#findComment-1301960 Share on other sites More sharing options...
spec36 Posted January 3, 2012 Author Share Posted January 3, 2012 Thanks for the help. I now have the device name inserting properly into the command, but the command will not run on the server. When I run this code: $lines = file ("/path/to/myfile/devices.txt"); foreach ($lines as $line) { $ipmi_query = 'ipmitool -I lanplus -H ' . $line . '-U username -P password sdr list'; passthru($ipmi_query); } I get an error that says: sh: line 1: -U: command not found Now I know -U is a valid parameter for the ipmitool. When I echo the command ($impi_query) to the screen it shows like this: ipmitool -I lanplus -H device01-name -U username -P password sdr list Notice the line break in the command. I believe this is why the output is saying that "-U command not found". Does anyone know how I can keep that command so it runs on the server as one line instead of two? I don't know why the command is being broken into two line. Thanks for the help, Quote Link to comment https://forums.phpfreaks.com/topic/253956-php-and-command-line-tools/#findComment-1303769 Share on other sites More sharing options...
Adam Posted January 3, 2012 Share Posted January 3, 2012 Use trim to remove any white space in $line. Quote Link to comment https://forums.phpfreaks.com/topic/253956-php-and-command-line-tools/#findComment-1303782 Share on other sites More sharing options...
spec36 Posted January 3, 2012 Author Share Posted January 3, 2012 Thanks Adam. I was using trim before the for loop and it was not working. I added trim directly to the concatenated variable in the for loop and it now works. Thanks for the help everyone. Quote Link to comment https://forums.phpfreaks.com/topic/253956-php-and-command-line-tools/#findComment-1303804 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.