关于windows脚本的一些东西

我前几天碰到一个需求,需要在代码中实现将远程服务器中的文件拷贝到本地,在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

相关推荐

  1. 关于windows脚本一些东西

    2024-06-13 13:14:03       11 阅读
  2. 记录一些容易遗忘东西

    2024-06-13 13:14:03       13 阅读
  3. 关于Windows驱动中DPC同步一些见解说明

    2024-06-13 13:14:03       9 阅读
  4. 关于TC简单编程AB爪爪几点东西

    2024-06-13 13:14:03       17 阅读
  5. 关于 UbuntuServer 一些配置

    2024-06-13 13:14:03       32 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-13 13:14:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-13 13:14:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-13 13:14:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-13 13:14:03       20 阅读

热门阅读

  1. Python有哪些经典的常用库?

    2024-06-13 13:14:03       13 阅读
  2. 聊聊JetCacheProxyConfiguration

    2024-06-13 13:14:03       10 阅读
  3. 测试面试点

    2024-06-13 13:14:03       8 阅读
  4. 【星海随笔】ELK优化

    2024-06-13 13:14:03       6 阅读
  5. [EFI]NUC11电脑 Hackintosh 黑苹果efi引导文件

    2024-06-13 13:14:03       10 阅读
  6. 13. FastLED 示例3则

    2024-06-13 13:14:03       7 阅读
  7. gitlab下载及安装

    2024-06-13 13:14:03       8 阅读
  8. 2024年6月关于docker拉取镜像超时失败问题解决

    2024-06-13 13:14:03       12 阅读
  9. 【吃包子game】

    2024-06-13 13:14:03       8 阅读
  10. 电脑快捷键大全

    2024-06-13 13:14:03       8 阅读