Post Reply 
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iPXE and calling PHP
2019-02-26, 00:33
Post: #1
iPXE and calling PHP
I'd like to be able to run a php function to write the MAC and serial to a database. This is working, however when calling the php file ipxe exits and returns to the boot selection menu or boots the hard drive (depending on machine). Ideally I'd like to boot a new computer, call a function to write serial and mac to a database (using php i presume), then be able to continue in the ipxe menu to make a different boot selection.

From my testing it appears as long as php is 'ipxe compliant' and finishes running without error, it will return to ipxe menu, however because the php is running its own functions ipxe crashes with an error.

menu.ipxe
Code:
:prestage
echo Adding following details:
echo serial: ${smbios/serial}
echo mac: ${netX/mac}
sleep 3
# chain --replace --autofree http://172.16.0.17/prestage/index.php?serial=${smbios/serial}&mac=${netX/mac}
kernel http://172.16.0.17/prestage/index.php?serial=${smbios/serial}&mac=${netX/mac}
goto start

index.php - example code 'ipxe compliant' - returns to ipxe successfully
Code:
<?php
header ( "Content-type: text/plain" );
echo "#!ipxe \n";
echo "echo Dan is the man \n";
echo "sleep 5 \n";
?>

index.php - example code - breaks with error
Code:
<?php
header ( "Content-type: text/plain" );
echo "#!ipxe \n";
$serial = $_REQUEST['serial'];
$mac    = $_REQUEST['mac'];
echo "echo Dan is the man \n";
echo "sleep 5 \n";
?>

index.php - actual code works for purposes of writing database but crash exits
Code:
<?php
include "conn.php";
$serial = $_REQUEST['serial'];
$mac    = $_REQUEST['mac'];
if ($serial !="") {
    $sql = "insert into prestage (serial, mac) values ('$serial', '$mac')";
    mysqli_query($conn, $sql);
    };
    
?>

Do I need to compile ipxe binary with a different option?
Find all posts by this user
Quote this message in a reply
2019-02-26, 00:55
Post: #2
RE: iPXE and calling PHP
you call this by using kernel? any particular reason?

You can use chain like you have tried, with autofree (not replace) that is a good way to do it, you just have to make sure that what is returned is actually a valid ipxe script.

make sure you have echo "#!ipxe\n";
and not echo "#!ipxe \n"; (note the space)
I would also recommend to skip ?>, that way anything trailing will be not be sent to the client.
And of course make sure what you fetch don't contain warnings or anything else. (also what is the error you are gettings?)

But with all that said what I would suggest is to use initrd instead to do the call, make sure to use --autofree

Use GitHub Discussions
VRAM bin
Visit this user's website Find all posts by this user
Quote this message in a reply
2019-02-27, 04:36
Post: #3
RE: iPXE and calling PHP
Thanks, initrd advice was useful cheers Wink

I'm not sure half the time what I am doing, just managing to get it to work, however after testing various things with my colleague, we came to realise chain and initrd are quite different animals and managed to get a working solution using a stub php file and then calling the actual php. Not sure if its the optimal way but it works so far.

menu.ipxe
Code:
:prestage
#params
#param mac ${netX/mac}
#param serial ${smbios/serial}
#param uuid ${uuid}
#param asset ${asset}
echo Adding following details into prestage DB:
echo serial: ${smbios/serial}
echo mac: ${netX/mac}
# chain command is used here to pass to another 'ipxe menu'
chain --autofree http://172.16.0.17/prestage/stub.php
echo Done!
echo Returning to Main Menu
sleep 3
goto start

stub.php
Code:
<?php
// this is a 'ipxe compliant' menu
// use \n for line return and \ to escape $ values
header ( "Content-type: text/plain" );
echo "#!ipxe\n";
// initrd is used to call external php script where no ipxe commands are used
echo "initrd -a http://172.16.0.17/prestage/index.php?serial=\${smbios/serial}&mac=\${netX/mac}\n";
?>

index.php
Code:
<?php
include "conn.php";

$serial = $_GET['serial'];
$mac    = $_GET['mac'];

if ($serial !="") {
    $sql = "insert into prestage (serial, mac) values ('$serial', '$mac')";
    // built in php command to write data to sqlite DB
    mysqli_query($conn, $sql);
    };    
?>


On a side note I'd prefer the params command but we didn't know how to call/test variables from it. The http://ipxe.org/cmd/params shows an example, "Issues an HTTP Post request using a form parameter list", but from the boot.php side, how does it get the values from there?
Find all posts by this user
Quote this message in a reply
2019-02-27, 08:11
Post: #4
RE: iPXE and calling PHP
How to get values from POST requests you have to read about in the PHP documentation
What do you mean about initrd and chain being different? (well yes they are, they have different purposes, but if you only want to collect parameters, then you don't need or want to chain into different script)

Use GitHub Discussions
VRAM bin
Visit this user's website Find all posts by this user
Quote this message in a reply
2019-02-27, 20:59
Post: #5
RE: iPXE and calling PHP
Thanks for your help
I am a novice and mostly learnt by experimentation than theory.
initrd is different that it does not process any ipxe commands while chain will process ipxe commands, hence won't crash as far as I can tell.
Regarding the php, an example would help, like the example on the params page. chain http://boot.ipxe.org/demo/boot.php##params
What is a specific php documentation term I google for?
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)