pscp新机器提示Store key in cache? 重装后提示Update cached key?的解决方案

2 篇文章 0 订阅

最近想做windows平台的linux服务器自动化管理工具,考虑到易用性(无需事先在linux服务器上进行任何配置)以及开发周期,稳定性,决定采用现成方案pscp进行调用,毕竟pscp支持文件以及文件夹的上传/下载但是原版pscp有个致命问题是每次新机器提示Store key in cache? 重装后提示Update cached key?,因此考虑通过修改源码的方式来去掉验证,爽快的通过用户名密码进行调用:

首先下载putty源码并用vs2015打开,注意目录不要打开错了,要打开WINDOWS下的MSVC下的PUTTY.sln(因为src下也有一个,经测试用那个无法进行后续操作)

首先啥也不干先编译一下试试:报错

winstuff.h 中添加#define SECURITY_WIN32宏定义

重新编译,通过,下面修改key相关的代码,改为不再需要输入y即可通过key相关验证:

首先打开解决方案资源管理器-pscp中的wincons.c,找到verify_ssh_host_key函数,将图上所示部分注释,然后在上面增加两行

    store_host_key(host, port, keytype, keystr);
    return 1;

即可,聪明的你应该能看出来这其实就是直接跳过输入y或n,保存key的操作

改完之后,保存,vs2015-生成-重新生成解决方案-生成成功

修改后的代码奉上:

int verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
                        char *keystr, char *fingerprint,
                        void (*callback)(void *ctx, int result), void *ctx)
{
    int ret;
    HANDLE hin;
    DWORD savemode, i;

    static const char absentmsg_batch[] =
    "The server's host key is not cached in the registry. You\n"
    "have no guarantee that the server is the computer you\n"
    "think it is.\n"
    "The server's %s key fingerprint is:\n"
    "%s\n"
    "Connection abandoned.\n";
    static const char absentmsg[] =
    "The server's host key is not cached in the registry. You\n"
    "have no guarantee that the server is the computer you\n"
    "think it is.\n"
    "The server's %s key fingerprint is:\n"
    "%s\n"
    "If you trust this host, enter \"y\" to add the key to\n"
    "PuTTY's cache and carry on connecting.\n"
    "If you want to carry on connecting just once, without\n"
    "adding the key to the cache, enter \"n\".\n"
    "If you do not trust this host, press Return to abandon the\n"
    "connection.\n"
    "Store key in cache? (y/n) ";

    static const char wrongmsg_batch[] =
    "WARNING - POTENTIAL SECURITY BREACH!\n"
    "The server's host key does not match the one PuTTY has\n"
    "cached in the registry. This means that either the\n"
    "server administrator has changed the host key, or you\n"
    "have actually connected to another computer pretending\n"
    "to be the server.\n"
    "The new %s key fingerprint is:\n"
    "%s\n"
    "Connection abandoned.\n";
    static const char wrongmsg[] =
    "WARNING - POTENTIAL SECURITY BREACH!\n"
    "The server's host key does not match the one PuTTY has\n"
    "cached in the registry. This means that either the\n"
    "server administrator has changed the host key, or you\n"
    "have actually connected to another computer pretending\n"
    "to be the server.\n"
    "The new %s key fingerprint is:\n"
    "%s\n"
    "If you were expecting this change and trust the new key,\n"
    "enter \"y\" to update PuTTY's cache and continue connecting.\n"
    "If you want to carry on connecting but without updating\n"
    "the cache, enter \"n\".\n"
    "If you want to abandon the connection completely, press\n"
    "Return to cancel. Pressing Return is the ONLY guaranteed\n"
    "safe choice.\n"
    "Update cached key? (y/n, Return cancels connection) ";

    static const char abandoned[] = "Connection abandoned.\n";

    char line[32];

    /*
     * Verify the key against the registry.
     */
    ret = verify_host_key(host, port, keytype, keystr);

    if (ret == 0)               /* success - key matched OK */
    return 1;
    store_host_key(host, port, keytype, keystr);
    return 1;
 //   if (ret == 2) {               /* key was different */
    //if (console_batch_mode) {
    //    fprintf(stderr, wrongmsg_batch, keytype, fingerprint);
 //           //return 0;
    //}
    //fprintf(stderr, wrongmsg, keytype, fingerprint);
    //fflush(stderr);
 //   }
 //   if (ret == 1) {               /* key was absent */
    //if (console_batch_mode) {
    //    fprintf(stderr, absentmsg_batch, keytype, fingerprint);
 //           //return 0;
    //}
    //fprintf(stderr, absentmsg, keytype, fingerprint);
    //fflush(stderr);
 //   }
    //hin = GetStdHandle(STD_INPUT_HANDLE);
    //GetConsoleMode(hin, &savemode);
    //SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
             //ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
    //ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
    //SetConsoleMode(hin, savemode);

 //   if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') {
    //if (line[0] == 'y' || line[0] == 'Y')
    //    store_host_key(host, port, keytype, keystr);
 //       return 1;
 //   } else {
    //fprintf(stderr, abandoned);
 //       return 0;
 //   }
}

之后在pscp下的Debug文件夹下即可找到新生成的pscp,可以直接使用了,以后无论是新装机器还是重装机器,都不会有烦人的输入是否保存key了!

本人经测试是没有问题的,相信稍有基础的同学也能看懂,如果有缺乏耐心需要可执行程序源码等或者小白还有问题可联系我qq1415532825,可专门指导(友情提示:不免费哦,想白嫖的不要找我了,毕竟是本人辛苦劳动成果)

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙猫哪里跑

谢大爷赏赐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值