2012-06-06 32 views
-1

項目背景 - >我必須遠程升級Linux嵌入式系統。這需要將tar球文件從Windows Qt應用程序傳輸到通過以太網電纜連接的Linux機箱。 Linux機器有固定的IP地址。從Windows Qt應用程序傳輸文件到Linux並執行它

我到目前爲止所做的工作 - >在Qt中完成新手,我創建了一個對話框來瀏覽文件並檢查tar球文件。

問題 - >現在我想,當我點擊另一個按鈕,說升級,應該將文件從Windows轉移到Linux機器(已固定的IP地址),並執行具有代碼如何bash腳本升級不同的文件。

可能你們請拋開一些光線,我將如何將文件從Windows發送到Linux盒子。我已經看過Qt的FTP客戶端,我猜它會下載文件而不是傳輸它。

感謝和問候,
山姆

回答

0

1)我的批處理腳本說upgrade.bat接受2個參數,比如ip地址,升級tar球 然後用putty.exe將文件傳輸到通過以太網線連接的linux機器上。
例如蝙蝠腳本

PSCP -pw 「布拉布拉」 「%TARGET_UPDATE_FILE%」 用戶@ 「%TARGET_IP%」: 「%BASE_DIR%」/

其中target_update_file =焦油球文件接受從Qt和目標IP = IP地址作爲參數從qt接收bat文件。

現在對QT,在升級按鈕代碼

void Qt_test::ReadOut()  
{  
    QProcess *processInfo = dynamic_cast< QProcess* >(sender()); 

    if(processInfo) 
    { 
     ui->upgrade_info->append(processInfo->readAllStandardOutput()); 
    }   
}  

void Qt_test::ReadError() 
{ 
    QProcess *processInfo = dynamic_cast< QProcess* >(sender()); 

    if(processInfo) 
    { 
     ui->upgrade_info->append(processInfo->readAllStandardError()); 
    } 
} 

void Qt_test::on_file_browse_clicked() 
{ 
    ui->selected_file->setText(QFileDialog::getOpenFileName(this, tr("Open File"), 
                 "/home", 
                 tr("Upgrade file (*.tar.gz)" ))); 
} 

void Qt_test::on_file_upgrade_clicked() 
{ 
     QFileInfo upgradeFileInfo(ui->selected_file->text()); 
     if(!upgradeFileInfo.exists()) 
     { 
      QMessageBox::information(this, tr("Information"), 
          tr("Unable to find file for upgrading!")); 
      return; 
     }  

    QString batchPath= QDir::currentPath() + "\\testUpgrade.bat" ; 
    QFileInfo batchFile(batchPath) ; 
    if(!batchFile.exists()) 
    { 
      QMessageBox::information(this, tr("Information"), 
          tr("Failed to find the upgrade batch file....... ")) ; 
     return; 
    } 

    //Check if cmd.exe is present in Clients system and is located at correct path 
     QFileInfo cmdFile("C:\\Windows\\system32\\cmd.exe"); 
    if(!cmdFile.exists()) 
     { 
      QMessageBox::information(this, tr("Information"), 
          tr("Failed to find the cmd.exe ... Check cmd.exe is installed and is in C:\\Windows\\system32\\ !")) ; 
      return; 
     } 

    QFileInfo puttyFile("C:\\Windows\\system32\\putty.exe"); 
    if(!puttyFile.exists()) 
    { 
      QMessageBox::information(this, tr("Information"), 
           tr("Failed to find the putty.exe ... Check putty.exe(ideally version 0.62.0.0) is installed and is in C:\\Windows\\system32\\ !")) ; 
      return; 
    } 

     QFileInfo plinkFile("C:\\Windows\\system32\\plink.exe"); 
    if(!plinkFile.exists()) 
    { 
      QMessageBox::information(this, tr("Information"), 
           tr("Failed to find the plink.exe ... Check plink.exe(ideally version 0.62.0.0) is installed and is in C:\\Windows\\system32\\ !")) ; 
      return; 
    } 

    QString upgradeFile = upgradeFileInfo.absoluteFilePath(); 
    QString ipAddress = ui->selected_ip->text(); 
    qDebug() << " checksum valu is " <<checkSum.count() ; 
    QStringList arguments ; 

    arguments << " /c" << batchFile.absoluteFilePath()<< upgradeFile << ipAddress ; 
    // copying update 
    QProcess *processUpgrade = new QProcess(this); 
    if(processUpgrade) 
    { std::cout <<" starting to upgrade " << std::endl ; 

     processUpgrade->setEnvironment(QProcess::systemEnvironment()) ; 
     processUpgrade->setProcessChannelMode(QProcess::MergedChannels) ; 

     processUpgrade->start(cmdFile.absoluteFilePath(), arguments) ; 
     processUpgrade->waitForStarted() ; 
     connect(processUpgrade, SIGNAL(readyReadStandardOutput()), this, SLOT(ReadOut())) ; 
      connect(processUpgrade, SIGNAL(readyReadStandardError() ), this, SLOT(ReadErr())) ; 
    } 
} 
1

兩個QFtp和QNetworkAccessManager類可以將文件上傳到FTP服務器上。 QNetworkAccessManager類比QFtp更適合您的工作。但是,它需要在你的Linux機器上進行一定的設置。
您也可以使用TCP Socket連接傳輸文件(請參見類QTcpServerQTcpSocket),這還需要在Linux上運行其他應用程序。所以如果你想讓這個過程自動化,我想你可以編寫一個Qt應用程序,它將文件上傳到運行在你的Linux機器上的FTP服務器上,或者創建兩個簡單的應用程序作爲客戶端和服務器。

+0

感謝您的建議。當我使用Google時,我發現QProcess,我想如果我使用'QProcess *過程; process-> start(「scp -pw rootpsswd c:\ tmp \ foo.tar.gz [email protected]:/ home/guest /」);'然後執行另一個命令,它會調用linux框中的bash腳本來解壓該文件並做需要的東西。你認爲這種方法好嗎? – samantha

+0

@samantha這段代碼將簡單地啓動'scp'(安全拷貝),它是許多Linux發行版的SSH套件的一部分。如果您只想將文件從Windows傳輸到Linux,爲什麼不使用普通(S)FTP客戶端?你需要*在這裏寫下你自己的客戶嗎? – Gnosophilon

+0

@ Gnosophilon,謝謝你的回覆。那麼,Linux的盒子已經有了ftp客戶端。在Windows上的Qt應用程序只是一個小應用程序瀏覽文件和trhough按鈕應升級文件在Linux機器/盒。升級我的意思是它應該將tarball轉移到linux機器上並運行bash腳本來解壓縮文件,檢查校驗和等等 – samantha

相關問題