简体   繁体   English

Qt / C ++在远程计算机上执行命令并与其提示进行交互

[英]Qt/C++ Execute Commands on a Remote Machine & Interact with its Prompts

I am developing a C++ program using the Qt 5 Framework and need to do the following: 我正在使用Qt 5 Framework开发一个C ++程序,需要执行以下操作:

  1. Connect to a remote server using SSH 使用SSH连接到远程服务器
  2. Execute commands like mkdir ~/testing 执行mkdir~ / testing等命令
  3. Execute commands like mysqldump -u test -p123 testdb -> testdb.sql 执行mysqldump -u test -p123 testdb - > testdb.sql等命令

Basically the standard commands you would run as if you were using the terminal. 基本上,您将运行的标准命令就像使用终端一样。

I looked into QProcess but it is limited and I don't feel comfortable using echo "password" | 我查看了QProcess,但它是有限的,我觉得使用echo“密码”|感觉不舒服 ssh -x hostname -l username, etc... ssh -x hostname -l username等...

Is there any C++ or Qt library that can do what I am trying to accomplish? 是否有任何C ++或Qt库可以完成我想要完成的任务?

You should learn how to configure ssh (with public keys) without any password (or just a passphrase added with ssh-add before starting your Qt application). 您应该学习如何在没有任何密码的情况下配置ssh (使用公钥)(或者在启动Qt应用程序之前只使用ssh-add添加密码)。 Read some good ssh tutorial . 阅读一些好的ssh教程 Then use openssl and/or libssh etc... 然后使用openssl和/或libssh等...

I found a solution that fits my needs. 我找到了符合我需求的解决方案。 If I do come up with a solution that is cross platform I will let you guys know. 如果我想出一个跨平台的解决方案,我会让你们知道。 Here's how to make it work in QT on a Linux enviornment. 以下是如何在Linux环境中使其在QT中运行。

QStringList commands;
commands << "-hold";
commands << "-e";
commands << "ssh username@host 'cd /home/user/backups; mysqldump -u root -p mydb > mydb.sql; echo DONE!'";

QProcess *process = new QProcess(0);
process->setProcessChannelMode(QProcess::MergedChannels);
process->start("xterm", commands);

if(!process->waitForStarted()){
    qDebug() << "Could not wait to start...";
}

if(!process->waitForFinished()) {
    qDebug() << "Could not wait to finish...";
}

process->closeWriteChannel();
qDebug() << process->readAll();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM