|
/*
|
|
* DLink SSH Connection API
|
|
* Alejandro Antúnez - SODITEC S.L
|
|
* (C) 2015 - 2016 */
|
|
|
|
#include "dlinkpasswordgen.h"
|
|
|
|
|
|
DLinkPasswordGen::DLinkPasswordGen(QObject *parent) : QObject(parent)
|
|
{
|
|
ssh = new QSshSocket();
|
|
connect(ssh, SIGNAL(loginSuccessful()), this, SLOT(successLog()));
|
|
connect(ssh, SIGNAL(error(QSshSocket::SshError)), this, SLOT(error(QSshSocket::SshError)));
|
|
}
|
|
|
|
QString DLinkPasswordGen::GetSystemPWD(){
|
|
QFile *devNameFile;
|
|
devNameFile = new QFile("/tmp/clave.txt");
|
|
//if (!devNameFile->exists()) { QMessageBox::information(this, "Error", "No se ha encontrado el fichero de clave"); exit (-1); }
|
|
devNameFile->open(QFile::ReadWrite);
|
|
QString pwd(devNameFile->readAll());
|
|
pwd = pwd.remove('\n') ;
|
|
devNameFile->close();
|
|
//char* pwd = getenv("SYS_PWD_DLINKPWD");
|
|
return QString(pwd);
|
|
}
|
|
|
|
void DLinkPasswordGen::ConnectRouter(){
|
|
ssh->connectToHost(HOST_ROUTER);
|
|
ssh->login(USER_DEFAULT, PWD_DEFAULT);
|
|
}
|
|
|
|
void DLinkPasswordGen::DisconnectRouter(){
|
|
//if (Ready)
|
|
// while (!Ready){qDebug()<<"Espera Ready";};
|
|
ssh->quit();
|
|
}
|
|
|
|
DLinkPasswordGen::~DLinkPasswordGen(){
|
|
//delete ssh;
|
|
}
|
|
|
|
|
|
void DLinkPasswordGen::DesconectarWIFI(){
|
|
QString ssh_command("ifconfig ra0 down");
|
|
ssh->executeCommand(ssh_command);
|
|
}
|
|
|
|
void DLinkPasswordGen::ConectarWIFI(){
|
|
QString ssh_command("ifconfig ra0 up");
|
|
ssh->executeCommand(ssh_command);
|
|
}
|
|
|
|
QString DLinkPasswordGen::GeneratePWD(){
|
|
//QString ssh_command(COMMAND_SSH);
|
|
//qDebug()<<"ENTRO";
|
|
QString command_with_pwd = "nvram set wl0_wpa_psk=%1";
|
|
command_with_pwd = command_with_pwd.arg(GetSystemPWD());
|
|
command_with_pwd += ";nvram commit; sleep 3; reboot;";
|
|
ssh->executeCommand(command_with_pwd);
|
|
/*
|
|
command_with_pwd += ";nvram commit; reboot;";
|
|
|
|
//Consultamos si ya existe esa pasword
|
|
QString ssh_consulta="nvram get wl0_wpa_psk;";
|
|
QString old_pass;
|
|
ssh->RESPUESTA_SSH="";
|
|
ssh->executeCommand(ssh_consulta);
|
|
if(ssh_consulta!=GetSystemPWD()){
|
|
//ssh->executeCommand(command_with_pwd);
|
|
}
|
|
while(ssh->RESPUESTA_SSH==""){qDebug()<<"SSH";} //Espero a que termine el hilo de SSH
|
|
|
|
//elimino el salto de linea
|
|
old_pass=ssh->RESPUESTA_SSH;
|
|
old_pass.replace("\n","");
|
|
qDebug() << " A DLINK generate: "<< ssh->RESPUESTA_SSH;
|
|
sleep(1);
|
|
if(old_pass!=GetSystemPWD()){
|
|
qDebug() << " ANTIGUA: "<< old_pass<<"NUEVA: "<<GetSystemPWD();
|
|
|
|
ssh->executeCommand(command_with_pwd);
|
|
qDebug()<<"HA PASADO";
|
|
Ready = false;
|
|
}*/
|
|
return GetSystemPWD();
|
|
}
|
|
|
|
QString DLinkPasswordGen::GeneratePWD(QString rootUser, QString pre, QString clave){
|
|
//QString ssh_command(COMMAND_SSH);
|
|
qDebug()<<"ENTRO";
|
|
QString comandCurl("curl -X POST --user \"%1:%2\" -d \"submit_button=WL_WPATable&action=ApplyTake&change_action=gozila_cgi&submit_type=save&security_varname=&security_mode_last=&wl_wep_last=&filter_mac_value=&wl0_security_mode=psk2&wl0_crypto=tkip%5aes&wl0_wpa_psk=%3&wl0_wl_unmask=0&wl0_wpa_gtk_rekey=3600\" -H \"Referer: https://%4/WL_WPATable.asp\" -k https://%4/apply.cgi >/dev/null 2>&1; curl -X POST --user \"%1:%2\" -d \"submit_button=WL_WPATable&action=ApplyTake&change_action=gozila_cgi&submit_type=save&security_varname=&security_mode_last=&wl_wep_last=&filter_mac_value=&wl0_security_mode=psk2&wl0_crypto=tkip%5aes&wl0_wpa_psk=%3&wl0_wl_unmask=0&wl0_wpa_gtk_rekey=3600\" -H \"Referer: http://%4/WL_WPATable.asp\" http://%4/apply.cgi >/dev/null 2>&1; sleep 40;");
|
|
qDebug() << comandCurl;
|
|
comandCurl = comandCurl.arg(rootUser, clave, GetSystemPWD(), "127.0.0.1", "%2B");
|
|
QString command_with_pwd = comandCurl + pre;
|
|
ssh->executeCommand(command_with_pwd);
|
|
return GetSystemPWD();
|
|
}
|
|
|
|
|
|
QString DLinkPasswordGen::QueSsid(){
|
|
ssh->RESPUESTA_SSH="";
|
|
QString ssh_consulta="nvram get wl0_ssid;";
|
|
QString ssid;
|
|
ssh->executeCommand(ssh_consulta);
|
|
while(ssh->RESPUESTA_SSH==""){
|
|
//qDebug()<<"nuevo SSID";
|
|
} //Espero a que termine el hilo de SSH
|
|
//elimino el salto de linea
|
|
ssid=ssh->RESPUESTA_SSH;
|
|
ssid.replace("\n","");
|
|
//qDebug() << " A DLINK: "<< ssh->RESPUESTA_SSH;
|
|
ssh->RESPUESTA_SSH="";
|
|
return ssid;
|
|
}
|
|
|
|
QString DLinkPasswordGen::HayWifi(){
|
|
sleep(3);
|
|
ssh->RESPUESTA_SSH="";
|
|
QString ssh_consulta="cat /sys/class/net/ra0/operstate";
|
|
QString radio;
|
|
ssh->executeCommand(ssh_consulta);
|
|
while(ssh->RESPUESTA_SSH==""){} //Espero a que termine el hilo de SSH
|
|
//elimino el salto de linea
|
|
radio=ssh->RESPUESTA_SSH;
|
|
radio.replace("\n","");
|
|
//qDebug() << " A radio: "<< ssh->RESPUESTA_SSH;
|
|
ssh->RESPUESTA_SSH="";
|
|
return radio;
|
|
}
|
|
|
|
void DLinkPasswordGen::error(QSshSocket::SshError err){
|
|
Ready = false;
|
|
}
|
|
|
|
|
|
void DLinkPasswordGen::successLog(){
|
|
Ready = true;
|
|
emit onLoginSuccess();
|
|
}
|