Jump to content

Display on as table


Boxerman

Recommended Posts

Hi guys,

 

I've got the following command im trying to push out how it would look if i ran it in ssh

<?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
if(!($con = ssh2_connect("hostname", 22))){
    echo "fail: unable to establish connection\n";
} else {
    if(!ssh2_auth_password($con, "username", "password")) {
        echo "fail: unable to authenticate\n";
    } else {
        echo "okay: logged in...\n <br>";

        if (!($stream = ssh2_exec($con, "showspace" ))) {
            echo "fail: unable to execute command\n";
        } else {
            stream_set_blocking($stream, true);
            $data = "";
            while ($buf = fread($stream,4096)) {
              echo  $data .= $buf;
            }
            fclose($stream);
        }
    }
}
?>

This displays as:

 

---Estimated(MB)---- ---Estimated(MB)---- RawFree UsableFree ---Estimated(MB)---- RawFree UsableFree 135770112 67885056

 

in putty it displays as:

 

TestRepo cli% showspace
---Estimated(MB)----
  RawFree UsableFree
135770112   67885056
 
How would i get this to display the same as above on the php page?
 
 
Also, my 2nd question is how would i get this inputted into MYSQL, i know the command to insert, but how would i enable varibles like 
 
$rawspace
$usablespace
 
or something with will work?
 

 

Link to comment
Share on other sites

 

 

How would i get this to display the same as above on the php page?

You are seeing the result in one line because web browsers ignore whitespace characters. I bet if you right click > view source you'll see the output to be in multiple lines. To have your output display in multiple lines either pass $data to nl2br or wrap $data in <pre></pre> tags.

 

 

 

Also, my 2nd question is how would i get this inputted into MYSQL, i know the command to insert, but how would i enable varibles like 

 

All you do is define your sql query in a string. You'd use those variables as the values. You then execute the query. Basic example

// connect to mysql
$mysqli = new mysqli('localhost', 'user', 'pass', 'database');

// values
$var1 = 'foobar';
$var2 = 'phpfreaks';

// define query
$sql = "INSERT INTO table SET column1='$var1, column2='$var2'";

// execute query
$mysqli->query($sql);
Link to comment
Share on other sites

So you want to get the raw and usable disk space returned by the command into variables.

 

To do that you need to parse the string. Example code

// using regex capture the raw and usable free diskspace values from the last line
preg_match('~(\d+)\s+(\d+)$~', $data, $matches);

// remove first item from $matches array
array_shift($matches);

// convert the remaining values into variables
list($raw, $usable) = $matches;

// output values
echo "Raw: $raw<br />Used: $usable";
Link to comment
Share on other sites

If i wanted to display letters instead of soley numbers i would have to change the list right? if so would it be extract? 

 

What would be the syntax for something like that? here is an example:

 

 
Id Name  LoopA Pos.A LoopB Pos.B Drives Temp  RevA RevB Model Side
 0 cage0 2:0:1     0 3:0:1     0     40 25-33 2.61 2.61 DC2   n/a
 
extract($Id, $Name, $loopA) = $matches;

Edited by Boxerman
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.