1

To configure manually a WiFi network, I need the hexadecimal key.
In Ubuntu, I can obtain it with this comand:

wpa_passphrase network passphrase

And the result is:

network={
    ssid="network"
    #psk="passphrase"
    psk=72feda58f99812cd6a4a075047270e361e3ae18f8cb191eb8d55ac07f928a466
}

Then.. How can I obtain the psk with PHP?

EDIT: I do this:

<?php
$fp = fopen("data.txt", "w+");
    if(!$fp) die ("Errore nell'apertura del file");

exec("wpa_passphrase network passphrase",$output);

$conf = "";
for($i=0;$i<5;$i++)
    $conf .= $output[$i]."";
fwrite($fp,$conf);
fclose($fp);

exec("sudo cp data.txt /etc/wpa_supplicant.conf"); //this doesn't work!

echo "ok<br>".$conf
?>
Simone Sessa
  • 795
  • 1
  • 12
  • 35
  • 1
    Not sure what you mean... Is your question about how to execute OS commands from PHP? (In that case this is most probably a duplicate.) If you are looking for a PHP lib doing this, it is off-topic on Stackoverflow. Could you elaborate? What have you tried? – Gábor Bakos May 25 '15 at 09:50
  • I've did something.. Now I need to copy the data.txt file as /etc/wpa_supplicant.conf – Simone Sessa May 25 '15 at 10:02
  • 1
    Thanks, this is much better. This way probably someone will have idea what is your problem. My guess `sudo` requires authentication or the user of php is not one of the sudoers. – Gábor Bakos May 25 '15 at 10:09

2 Answers2

0

I've solved, this is the code:

<?php

$fp = fopen("data.txt", "w+");
if(!$fp) die ("Errore nell'apertura del file");

exec("wpa_passphrase network passphrase",$output);

$conf = "";
for($i=0;$i<5;$i++)
$conf .= $output[$i]."\n";
fwrite($fp,$conf);
fclose($fp);

exec("sudo cp data.txt /etc/wpa_supplicant.conf");

echo "1";

?>

But, to execute sudo command, I must add "www-data" user into sudoers file. I did this following this reply

Community
  • 1
  • 1
Simone Sessa
  • 795
  • 1
  • 12
  • 35
0

In case anyone is still interested in this: I solved this using plain PHP by reading through the source of wpa_passphrase and comparing the result to the Wireshark PSK tool.

function wpa_passphrase($ssid, $passphrase) {
    $bin = hash_pbkdf2('sha1', $passphrase, $ssid, 4096, 32, true);
    return bin2hex($bin);
}

$psk = wpa_passphrase('network', 'passphrase');

Requires PHP >= 5.5

oelna
  • 2,210
  • 3
  • 22
  • 40