tags:

views:

104

answers:

2

I'm building a web control that will let our junior IT staff manage firmware on our LifeSize phones. Currently we do this by uploading the new firmware onto a central server, then running this command for each phone we want to upgrade

cat new_firmware.cramfs | ssh -T [email protected] "upgrade all"

This asks me for the password, then uploads the firmware. Works like a champ, but it takes someone comfortable with CLI tools, SSH access to this server, and patience to look up all the IPs of all the phones.

It looks like we're stuck with a password logon, testing with certificates has been disastrous. The device being acted on is not a full-fledged computer, it's a telephone running a tiny, proprietary embedded OS.

I'm working on a PHP script that can iterate over all the phones, but basically duplicate that function. This is what I have so far:

<?php
$firmware_filename = "new_firmware.cramfs";
$firmware_stream = fopen($firmware_filename,"rb");

$ssh_connection = ssh2_connect("1.1.1.1", 22);
ssh2_auth_password($ssh_connection, "cli", "password");
$ssh_stream = ssh2_exec($ssh_connection,'upgrade all');
$written = stream_copy_to_stream($firmware_stream,$ssh_stream,-1);

if($written != filesize($full_filename)){
 echo "The file is " . filesize($firmware_filename) . " bytes, I only wrote $written" . PHP_EOL;
}else{
 echo "All Good" . PHP_EOL;
}
?>

But this always returns

The file is 26988590 bytes, I only wrote 8192

And the upgrade does not proceed correctly.

+1  A: 

Well you could simply call

system('cat new_firmware.cramfs | ssh -T [email protected] "upgrade all"');

and then replace using your vars:

system('cat ' . $firmware . ' | ssh -T ' . $username . '@' . $host . ' "upgrade all"');

is this a solution for you?

you can automate the ssh-access by placing the certificate-file into .ssh-directory. Read about SSH login without password.

regards

Atmocreations
Be very careful about this type of strategy from a security standpoint. You must use escapeshellcmd()/escapeshellarg() on all variables you are concatenating into a string that ultimately will be passed to system(). Otherwise you may be opening yourself up to shell command injection vulnerabilities.
Asaph
@Asaph: sure, you're absolutely right. But I think this question is about easy automation of a job. Indeed, this wouldn't be something I'd include in a production-ready solution which is deployed on a public server.
Atmocreations
A: 

There are several things you could try:

  1. Copy the file first, then run the command on the now-local file.
  2. Assuming that you're filling an 8k buffer, try writing in a loop until you've successfully written the whole file
  3. Take the easy way out, and just set up ssh keys so you don't need to enter a password, and exec shell commands directly from your script
Frank Farmer