0

Using Mint terminal my script connects using ssh2_connect and ssh2_auth-password. When am logged in successfully I want to run a command which will give me the hardware cpu. Is there a way I can use to exec the command in my script then show the results. I have used system and exec for pinging. if i was in the terminal i do the login. then type "get hardware cpu"

in the terminal it would look like this:

Test~ $ get hardware cpu

1
  • Are you perhaps looking for ssh2_exec? There is a comment on that page that seems to do exactly what you are looking for. Commented Nov 15, 2012 at 15:14

3 Answers 3

1

You need access to the shell:

$connection = ssh2_connect($ip, $port);
$stream = ssh2_shell($connection);
fputs($stream, $input);
$buffer = fread($stream, 8192);

Have a look at this example:

$input = "ls\nexit\n";
$connection = ssh2_connect($host, $port);
ssh2_auth_password($connection, $user, $pass);
$stream = ssh2_shell($connection);
stream_set_blocking($stream, 1);
stream_set_timeout($stream, 2);
fputs($stream, $input);

while (!feof($stream)) {
        echo $buffer = fread($stream, $buffersize);
}

You might have to sleep() and fread() manually since people experience problems when setting the ssh2 stream blocking.

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried want to show the result cause when I put in get hardware cpu in the terminal after I have done my ssh2 authintication it works in the terminal want to put this command and run in my php script.
Here is my script: <?php $connection = ssh2_connect('100.168.0.3', 22); if (ssh2_auth_password($connection, $username, $password)) { echo "Authentication Successful!\n"; $stream = ssh2_shell($connection); $input ='get hardware cpu'; fputs($stream, $input); $buffer = fread($stream, 8192); echo $buffer; } else { echo "Authentication unsuccessful!\n"; } ?>
Resource id#5 that is what i get when I run my php file
1

You could try this (requires phpseclib, a pure PHP SSH2 implementation):

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec("grep 'model name' /proc/cpuinfo");
?>

Comments

0

I don't know where this "get hardware" command comes from. If you're trying to figure out what cpu is powering the system, then

grep 'model name' /proc/cpuinfo

will output the ID string of the cpu:

marc@panic:~$ grep 'model name' /proc/cpuinfo
model name      : Intel(R) Core(TM)2 CPU          6600  @ 2.40GHz
model name      : Intel(R) Core(TM)2 CPU          6600  @ 2.40GHz

1 Comment

You'd need to run it on the terminal via SSH - not via SCP. But you're in luck - if you can do SCP that means you have SSH terminal access as SCP runs through the terminal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.