Skip to content

Instantly share code, notes, and snippets.

@nemf
Created July 24, 2012 05:30
Show Gist options
  • Save nemf/3168223 to your computer and use it in GitHub Desktop.
Save nemf/3168223 to your computer and use it in GitHub Desktop.
#!ipxe
imgfree
chain http://${next-server}/switch-pxe.php?serial=${serial:uristring} || shell
<?php
#
# Read parameter from HTTP Request
#
$serial=$_REQUEST['serial'];
#
# Read parameter from MachineManage
#
$x = parse_csv("data/x.csv",0,1);
$v = parse_csv("data/v.csv",0,1);
#var_dump($x);
#var_dump($v);
#
# check BOMC finish or not
#
if ( count( glob("log/UXSPI_7944_${serial}_*") ) < 1 ){
bomc();
exit;
}
#
# Specify OS
#
$id = serial2id( $serial, $x );
if ( $id === FALSE ){
$id = serial2id( $serial, $v );
if ( $id === FALSE ){
error("Can't Find your Serial No ($serial)");
} else {
if ( count( glob( "log/results/v_Live_${serial}_*" ) ) > 0 ){
$flag = "esxi";
esxi();
exit;
}
$flag = "v_Live";
v_Live();
#echo $v[$id]["serial"] . "\n";
#echo $v[$id]["hostname"] . "\n";
}
} else {
$flag = "x_Live";
x_Live();
#echo $x[$id]["serial"] . "\n";
#echo $x[$id]["hostname"] . "\n";
}
function error($msg){
echo "#!pxe\n";
echo "echo $msg\n";
echo "shell\n";
}
function bomc (){
echo "#!ipxe\n";
echo "dhcp\n";
echo "set 210:string http://\${next-server}/LiveImage/bomc/pxedir/\n";
echo "chain \${210:string}pxelinux.0\n";
}
function v_Live ( $serial ){
global $serial;
echo "#!ipxe\n";
echo "dhcp\n";
echo "set 210:string http://\${next-server}/LiveImage/pxe1-esxi/\n";
echo "chain \${210:string}pxelinux.0\n";
$suffix = date("Ymd-Hi");
touch( "log/results/v_Live_${serial}_$suffix" );
}
function x_Live (){
echo "#!ipxe\n";
echo "dhcp\n";
echo "set 210:string http://\${next-server}/LiveImage/pxe1-xsv/\n";
echo "chain \${210:string}pxelinux.0\n";
}
function esxi (){
echo "#!ipxe\n";
echo "dhcp\n";
echo "set 210:string http://\${next-server}/LiveImage/esxi/\n";
echo "chain \${210:string}pxelinux.0\n";
}
function serial2id ( $serial, $array ){
$results = FALSE;
if ( !empty($serial) && is_array($array) ){
for($i=1; $i<=count($array); $i++){
if ( $array[$i]["serial"] === $serial ) {
return $i;
}
}
}
return $results;
}
function parse_csv( $csv_file, $title_row = -1, $start_row = 0, $key_cal = -1, $length = 0, $delimiter = ',', $enclosure = '"' ) {
$results = FALSE;
if ( file_exists($csv_file) && ($fp = fopen($csv_file, 'r')) ) {
$row = 0;
$results = array();
$col_name = array();
while ( $field_array = fgetcsv($fp, $length, $delimiter, $enclosure) ) {
if ( $row === $title_row ) {
$col = 0;
foreach ($field_array as $value) {
$col_name[$col] = !empty($value) ? $value : $col;
$col++;
}
}
if ( $row >= $start_row ) {
$result = array();
$col = 0;
foreach ($field_array as $value) {
$key = isset($col_name[$col]) ? $col_name[$col] : $col;
$result[$key] = $value;
$col++;
}
$key = $row;
if ( is_numeric($key_cal) ) {
$key =
( $key_cal >= 0 && isset($col_name[$key_cal]) )
? $result[$col_name[$key_cal]]
: $row;
} else {
foreach ($col_name as $value) {
if ( $key_cal === $value ) {
$key = $result[$value];
break;
}
}
}
$results[$key] = $result;
}
$row++;
}
fclose($fp);
}
return $results;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment