I have a CSV file that sits on a Raspberry PI and outputs:
2018-03-22 12:43:21,NM_Test.h264,-2
On my host server I have a PHP script that takes the output from the CSV file and displays it as a HTML table on my webpage:
$command = "ssh -p 97 -i /var/www/html/test.rsa [email protected] tail -1 /var/log/playlog.csv";
$output = exec($command);
$array = explode(',',$output);
echo '<div class="container"><table class="table table-striped">
<tr>
<th>Status</th>
<th>Name</th>
<th>Date/Time</th>
<th>Playing</th>
<th>Error</th>
</tr>
<tr>
<td>';
if(in_array('0', $array, true)){
echo '<div id="circleGreen"></div>';
}
if (in_array('-2', $array, true)){
echo '<div id="circleRed"></div>';
}
echo'</td>
<td>Guildford test</td>
<td>'.$array[0].'</td>
<td>'.$array[1].'</td>
<td>';
This method works for one SSH connection, but how can I run the SSH command multiple times for connecting to different PIs?
I thought about creating a txt file and saving each SSH command in there, and then have my PHP script read/execute each command line by line:
// example txt file
ssh -p 97 -i test.rsa [email protected]
ssh -p 97 -i test2.rsa [email protected]
// and so on..
// only the rsa key name & IP address changes
But I'd like a more efficient solution to my problem.
Update
As recommended I used the phpsec library to SSH onto the PI:
<?php
include('Net/SSH2.php');
include('phpseclib1.0.10/Crypt/RSA.php');
$ssh = new Net_SSH2('192.xxx.xxx.xxx', 97);
$key = new Crypt_RSA();
$key->loadKey(file_get_contents('test.rsa'));
if (!$ssh->login('pi', $key)){
exit ('Login Failed');
}
echo $ssh->exec('ls -la');
?>
But by doing so I still have to type the IP address as well as the RSA key name. I'd like a solution that allows me to quickly ssh into multiple PIs and execute the exec command.
One possible solution I can think of is allowing my script to read the known_hosts file? Is that possible?
AESSSL(like making self singed certs) and many other cryptographic things. The only thing I find it lacking in is no PGP encryption, but than again there isn't really any easy to use libraries for that..