Boxerman Posted November 11, 2014 Share Posted November 11, 2014 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? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 11, 2014 Share Posted November 11, 2014 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); Quote Link to comment Share on other sites More sharing options...
Boxerman Posted November 11, 2014 Author Share Posted November 11, 2014 Thanks! How would i break the command up? So from the output i spilt it into two varibles? $1 and $2 for space? like above? If ssh outputed foobar and phpfreaks how would php work out that $var1 is foobar and $var2 is phpfreaks? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 11, 2014 Share Posted November 11, 2014 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"; Quote Link to comment Share on other sites More sharing options...
Boxerman Posted November 11, 2014 Author Share Posted November 11, 2014 (edited) 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 November 11, 2014 by Boxerman Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 11, 2014 Share Posted November 11, 2014 No not at all. You will need to come up with your own way of parsing the data into the necessary variables. Is the data you posted from the output of another command? Quote Link to comment Share on other sites More sharing options...
Boxerman Posted November 11, 2014 Author Share Posted November 11, 2014 Yes it is, sorry. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 12, 2014 Share Posted November 12, 2014 So what have you tried? Quote Link to comment 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.