最近想做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,可专门指导(友情提示:不免费哦,想白嫖的不要找我了,毕竟是本人辛苦劳动成果)