Jump to content

PHP and command line tools


spec36

Recommended Posts

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,

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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,

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.