我前几天碰到一个需求,需要在代码中实现将远程服务器中的文件拷贝到本地,在linux中很好实现,通过sshpass来实现用户名和密码的输入,并且通过scp来拷贝文件
sshpass -p hcsci123456 scp hcsci@192.168.50.50:~/GL01master.log ./
但是在windows环境下是不能执行sshpass的,于是我用到了pscp工具,在powershell中执行
pscp -pw ${password} -r ${username}@${remoteHost}:${remotePath} ${localPath}
将它整合为一个脚本文件就是
param(
[string]$localPath = "E:/test/" ,
[string]$remoteHost
)
$username = "hcsci"
$password = "1"
$remotePath = "/home/hcsci/TC384/base_map"
$command = "pscp -pw ${password} -r ${username}@${remoteHost}:${remotePath} ${localPath}"
Invoke-Expression $command
在代码中调用这个脚本即可,demo如下:
//获取测试底图
bool PCRLightAccuracyWidget::getTestPhoto(const QString &path)
{
//删除base_map文件夹
QString deletePath = path + "/base_map";
QDir dir(deletePath);
if(dir.exists())
{
if(dir.removeRecursively())
{
qDebug() << "删除文件成功!" << deletePath;
}
else
{
qDebug() << "删除文件失败:" << deletePath;
return false;
}
}
else
{
qDebug() << "base_map不存在,不进行删除";
}
QProcess process;
connect(&process, &QProcess::readyReadStandardOutput, this, [=](){
QProcess *process = qobject_cast<QProcess *>(sender());
if (process) {
QString output = process->readAllStandardOutput();
qDebug() << "Output:" << output;
}
});
connect(&process, &QProcess::readyReadStandardError, this, [=](){
QProcess *process = qobject_cast<QProcess *>(sender());
if (process) {
QString errorOutput = process->readAllStandardError();
qDebug() << "Error Output:" << errorOutput;
}
});
// 脚本路径
QString scriptPath = QCoreApplication::applicationDirPath() + "/CopyFiles.ps1";
// 准备参数
QString ip;
if(m_id == ENUM_CLASS::PCR_Id::M1)
{
ip = "192.168.137.101";
}
else
{
ip = "192.168.137.102";
}
QStringList arguments;
arguments << "-ExecutionPolicy" << "Bypass" << "-File" << scriptPath << path << ip;
// 调用 PowerShell 执行脚本
process.start("powershell", arguments);
// 等待命令执行结束
if(!process.waitForFinished(-1))
{
qDebug() << "执行获取测试底图脚本失败:" << process.errorString();
return false;
}
QString output = process.readAllStandardOutput();
QString errorOutput = process.readAllStandardError();
qDebug() << "Output:" << output;
if(!errorOutput.isEmpty())
{
qDebug() << "Error Output:" << errorOutput;
return false;
}
return true;
}
但是在连接远程主机时,有时候会遇到一些问题,如:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the ECDSA key sent by the remote host is SHA256:ruFzQrEzCAM+pmhyMANEyVCJAKry4tqvkoMHcNzWhk8. Please contact your system administrator. Add correct host key in C:\\Users\\HCSCI/.ssh/known_hosts to get rid of this message. Offending ECDSA key in C:\\Users\\HCSCI/.ssh/known_hosts:1 ECDSA host key for 192.168.137.101 has changed and you have requested strict checking. Host key verification failed.
解决方式如下:
1、known_hosts
文件中删除与 IP 地址 192.168.137.101
相关的密钥
ssh-keygen -R 192.168.137.101
之后再连接远程主机,若遇到交互输入,则输入yes
2、还有一种方法是将StrictHostKeyChecking改为no