|                                                                              | 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 | <?php
namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
use Illuminate\Support\Facades\Process;
class Redes_wifi extends Command
{
    /**
     * The signature of the command.
     *
     * @var string
     */
    protected $signature = 'wifi {essid?} {--list}';
    /**
     * The description of the command.
     *
     * @var string
     */
    protected $description = 'Muestra las contraseñas guardadas de wifi';
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $essid = $this->argument('essid');
        //$profiles = $this->getProfiles();
        $profiles = $this->getProfilesRegex();
        if (isset($essid) && (!$this->option('list'))) {
            $this->line("[{$essid}] ".$this->getPassword($essid));
            return 0;
        }
        foreach ($profiles as $profile) {
            $linea = "[{$profile}] ";
            if (!$this->option('list'))
                $linea .= $this->getPasswordRegex($profile);
            $this->line($linea);
        }
    }
    public function getProfilesRegex()
    {
        /*$result = Process::run('netsh wlan show profile');
        $salidaprofiles = $result->output();*/
        $salidaprofiles = file_get_contents('profiles.txt');
        preg_match_all("/: \s*(.*)/", $salidaprofiles, $profiles);
        return $profiles[1];
    }
    public function getPasswordRegex($profile)
    {
        $result = Process::run("netsh wlan show profile \"{$profile}\" key=clear");
        $salidapassword = $result->output();
        if (str_contains($salidapassword, "No hay ninguna interfaz inalámbrica en el sistema.")) { //Esto evita que ejecute regex si hay fallo
            $this->error("Fallo al ejecutar comando de password");
            die();
        }
        //$salidapassword = file_get_contents('prueba.txt');
        preg_match("/Contenido de la clave\s*:\s*(\S+)/", $salidapassword, $password);
        return $password[1];
    }
    public function getProfiles()
    {
        $result = Process::run('netsh wlan show profile');
        $salidaprofiles = $result->output();
        //$salidaprofiles = file_get_contents('profiles.txt');
        $parseprofiles = explode("-------------------", $salidaprofiles);
        $parseprofiles = end($parseprofiles);
        $parseprofiles = explode("\n", trim($parseprofiles, "\n\r"));
        $profiles = [];
        foreach ($parseprofiles as $profile) {
            $profiles[] = trim(str_replace('    Perfil de todos los usuarios     : ', '', $profile));
        }
        return $profiles;
    }
    public function getPassword($profile)
    {
        $result = Process::run("netsh wlan show profile \"{$profile}\" key=clear");
        $salidapassword = $result->output();
        //$salidapassword = file_get_contents('prueba.txt');
        $parsepassword = explode('--------------------------', $salidapassword);
        $parsepassword = end($parsepassword);
        $parsepassword = explode("Configuraci", trim($parsepassword, "\n"));
        $parsepassword = current($parsepassword);
        $parsepassword = trim($parsepassword);
        $parsepassword = explode("\n", $parsepassword);
        $parsepassword = str_replace('    Contenido de la clave  : ', '', end($parsepassword));
        return $parsepassword;
    }
    /**
     * Define the command's schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    public function schedule(Schedule $schedule): void
    {
        // $schedule->command(static::class)->everyMinute();
    }
}
 |