2012-06-06 36 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

相关问题