Boxerman Posted January 15, 2016 Share Posted January 15, 2016 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! Quote Link to comment https://forums.phpfreaks.com/topic/300363-changing-the-output/ Share on other sites More sharing options...
Barand Posted January 15, 2016 Share Posted January 15, 2016 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/300363-changing-the-output/#findComment-1529709 Share on other sites More sharing options...
Boxerman Posted January 15, 2016 Author Share Posted January 15, 2016 That works perfectly! thank you so much. One last thing, the output has the word "total" on the output the last time, is there a way to remove that and the black line above? Quote Link to comment https://forums.phpfreaks.com/topic/300363-changing-the-output/#findComment-1529721 Share on other sites More sharing options...
Barand Posted January 15, 2016 Share Posted January 15, 2016 I don't know where that comes from. There is no "total" in the data you posted. Quote Link to comment https://forums.phpfreaks.com/topic/300363-changing-the-output/#findComment-1529722 Share on other sites More sharing options...
Boxerman Posted January 15, 2016 Author Share Posted January 15, 2016 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! Quote Link to comment https://forums.phpfreaks.com/topic/300363-changing-the-output/#findComment-1529724 Share on other sites More sharing options...
Muddy_Funster Posted January 15, 2016 Share Posted January 15, 2016 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); Quote Link to comment https://forums.phpfreaks.com/topic/300363-changing-the-output/#findComment-1529737 Share on other sites More sharing options...
Boxerman Posted January 15, 2016 Author Share Posted January 15, 2016 (edited) 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 January 15, 2016 by Boxerman Quote Link to comment https://forums.phpfreaks.com/topic/300363-changing-the-output/#findComment-1529743 Share on other sites More sharing options...
Muddy_Funster Posted January 15, 2016 Share Posted January 15, 2016 I messed that you were unsetting $arr[0], so just change the for loop to start with $i=1 instead of $i=0 Quote Link to comment https://forums.phpfreaks.com/topic/300363-changing-the-output/#findComment-1529744 Share on other sites More sharing options...
Boxerman Posted January 15, 2016 Author Share Posted January 15, 2016 Muddy! You da man! It's removed total, but still displaying the blank line at the bottom I'm missing something just not sure what! Quote Link to comment https://forums.phpfreaks.com/topic/300363-changing-the-output/#findComment-1529746 Share on other sites More sharing options...
Barand Posted January 15, 2016 Share Posted January 15, 2016 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/300363-changing-the-output/#findComment-1529748 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.