Jump to content

Changing the output


Boxerman

Recommended Posts

Hey guys,


 


So a short write of what i'm trying to do, i'm trying to SSH connect to a SAN to grab details about the virtual volumes on the SAN and display them on a page. The final aim is to have this run as a cron and inserted into MySQL.


 


However, I have the script below with connects perfectly and displays the information i need, however, i am at a loss on how to actually break the output up into tables..


 


for example (the exact output):



Id Name Rd Mstr Prnt Roch Rwch PPrnt PBlkRemain -----VV_WWN----- -----CreationTime------
0 admin RW 2/3/1 --- --- --- --- -- 500name0405 2007-03-02 12:35:19 EST
1112 name.boot RW 2/3/1 --- --- --- --- -- 50002name0405 2012-02-24 16:54:48 EST
1113 name.0 RW 3/2/1 --- --- --- --- -- 5000name90405 2012-02-24 17:16:23 EST
1171 name.1 RW 1/3/2 --- --- --- --- -- 5000name405 2012-10-29 17:25:17 EDT
1306 name.boot RW 1/2/3 --- --- --- --- -- 50002name0405 2014-04-10 02:44:31 EDT
1307 name.boot RW 1/2/3 --- --- --- --- -- 5000name405 2014-04-10 08:49:09 EDT
1308 name.boot RW 1/2/3 --- --- --- --- -- 5000name0405 2014-04-10 08:50:00 EDT

AS you can see it is currently display rows called ID, Name etc..


 


how would i echo only the Name's of the the volume?


 


So the desired output is:



admin
name.boot
name.0
name.1
name.boot
name.boot
name.boot

Thanks so much for reading! hope you can help!


 


Here is my script so far:



<?PHP
//Array in place as more SAN's to be connected so writing to prepare for future SAN's
$servers = array(
array('ip'=>'hostname','user'=>'username','pass'=>'password'));
foreach($servers as $server)
{
$connection = ssh2_connect($server['ip'], 22);
if (ssh2_auth_password($connection, $server['user'], $server['pass']))
{
$stream = ssh2_exec($connection, 'showvv -d');
stream_set_blocking($stream, true);
$data = '';
while($buffer = fread($stream, 4096)) {
$data .= $buffer;
}
fclose($stream);
echo '<pre>'.$data.'</pre>';
}
}
?>

Thanks again guys! you rock!


Link to comment
Share on other sites

I'd use sscanf

$data = "Id Name Rd Mstr Prnt Roch Rwch PPrnt PBlkRemain -----VV_WWN----- -----CreationTime------
0 admin RW 2/3/1 --- --- --- --- -- 500name0405 2007-03-02 12:35:19 EST
1112 name.boot RW 2/3/1 --- --- --- --- -- 50002name0405 2012-02-24 16:54:48 EST
1113 name.0 RW 3/2/1 --- --- --- --- -- 5000name90405 2012-02-24 17:16:23 EST
1171 name.1 RW 1/3/2 --- --- --- --- -- 5000name405 2012-10-29 17:25:17 EDT
1306 name.boot RW 1/2/3 --- --- --- --- -- 50002name0405 2014-04-10 02:44:31 EDT
1307 name.boot RW 1/2/3 --- --- --- --- -- 5000name405 2014-04-10 08:49:09 EDT
1308 name.boot RW 1/2/3 --- --- --- --- -- 5000name0405 2014-04-10 08:50:00 EDT";

$arr = explode("\n", $data);
unset($arr[0]);   // remove headings

foreach ($arr as $line) {
    $items = sscanf($line, '%d %s %s');
    echo $items[1] . '<br>';
}
Link to comment
Share on other sites

Sorry, the full output (i cut it down to save spamming repeats) but at the end of the list, there is a blank line and then the word total:

admin
name.boot
name.0
name.1
name.boot
name.boot
name.boot
name.boot
name.boot
evaluation
evaluation.name.ro
evaluation-NoChanges
evaluation.name.ro


total

Also, my aim is to insert this into the database a new row for every line in the output. as you pass it to $items[1] wil that not just create one row with everything inside?

 

Thanks again for your help!

Link to comment
Share on other sites

tweaking the loop slightly should strip your last two lines, and you can add the contents to another array as well as echoing it:

 

$fData = array();
$cnt = count($arr)
for($i=0;$i<$cnt-1;$i++) {
    $items = sscanf($arr[$i], '%d %s %s');
    echo $items[1] . '<br>';
    $fData[$i][] = $items[1];
}
...
var_dump($fData);
Link to comment
Share on other sites

Thanks for the reply! however i'm getting the following error:

 

Notice: Undefined offset: 0

 

Here is the entire foreach: 

foreach($servers as $server)
{
    $connection = ssh2_connect($server['ip'], 22);
    if (ssh2_auth_password($connection, $server['user'], $server['pass']))
    {
        $stream = ssh2_exec($connection, 'showvv -d');
        stream_set_blocking($stream, true);
        //Set var for output to be stored in
        $data = '';
        //loop the buffer and store output to $data
        while($buffer = fread($stream, 4096)) {
    $data .= $buffer;
        }
        //close the stream
        fclose($stream);
        //explode array to remove header
        $arr = explode("\n", $data);
        unset($arr[0]);   // remove headings
		$fData = array();
		$cnt = count($arr);
		for($i=0;$i<$cnt-1;$i++) {
		$items = sscanf($arr[$i], '%d %s %s');
		echo $items[1] . '<br>';
		$fData[$i][] = $items[1];
		}
	var_dump($fData);

    }
}

Any suggestions?

Edited by Boxerman
Link to comment
Share on other sites

try

$data = "Id Name Rd Mstr Prnt Roch Rwch PPrnt PBlkRemain -----VV_WWN----- -----CreationTime------
0 admin RW 2/3/1 --- --- --- --- -- 500name0405 2007-03-02 12:35:19 EST
1112 name.boot RW 2/3/1 --- --- --- --- -- 50002name0405 2012-02-24 16:54:48 EST
1113 name.0 RW 3/2/1 --- --- --- --- -- 5000name90405 2012-02-24 17:16:23 EST
1171 name.1 RW 1/3/2 --- --- --- --- -- 5000name405 2012-10-29 17:25:17 EDT
1306 name.boot RW 1/2/3 --- --- --- --- -- 50002name0405 2014-04-10 02:44:31 EDT
1307 name.boot RW 1/2/3 --- --- --- --- -- 5000name405 2014-04-10 08:49:09 EDT
1308 name.boot RW 1/2/3 --- --- --- --- -- 5000name0405 2014-04-10 08:50:00 EDT


0 total ";

$arr = explode("\n", $data);
unset($arr[0]);   // remove headings

foreach ($arr as $line) {
    $items = sscanf($line, '%d %s %s');
    trim($items[1]);
    if (empty($items[1]) || $items[1]=='total') continue;
    echo $items[1] . '<br>';
    $fdata[] = $items[1];
}
echo '<pre>',print_r($fdata, true),'</pre>';

gives

admin
name.boot
name.0
name.1
name.boot
name.boot
name.boot
Array
(
    [0] => admin
    [1] => name.boot
    [2] => name.0
    [3] => name.1
    [4] => name.boot
    [5] => name.boot
    [6] => name.boot
)
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.