简体   繁体   English

当 Pageant 完成加载 SSH 密钥时运行批处理文件

[英]Run a batch file when Pageant finishes loading SSH keys

I've written two batch files - one to launch Pageant and load my keys, and the other to ssh some files onto a remote server.我编写了两个批处理文件 - 一个用于启动 Pageant 并加载我的密钥,另一个用于将一些文件 ssh 到远程服务器。 Individually, the scripts work perfectly.单独来看,这些脚本可以完美运行。 I am trying to combine them into a single batch file, but I can't get it to work.我试图将它们组合成一个批处理文件,但我无法让它工作。

Here are the scripts - only a line each, really.这是脚本 - 每个只有一行,真的。

To launch Pageant and load keys:要启动 Pageant 并加载密钥:

start E:\PuTTY\pageant.exe E:\Keys\priv.ppk

exit

To use pscp:使用 pscp:

pscp F:\website\foobar\src\* foo@178.128.10.35:/var/www/html

The problem is that the first script launches a password prompt.问题是第一个脚本会启动密码提示。 If I finish this and then launch the next script, everything works perfectly.如果我完成此操作然后启动下一个脚本,则一切正常。 But I've been unable to combine these two into one script in a way where the second command runs after the prompt from the first one is complete.但我一直无法将这两个组合成一个脚本,其中第二个命令在第一个命令的提示完成后运行。 How would I create a batch file that did so?我将如何创建一个这样做的批处理文件?

You can hardly solve this in a batch file. 您几乎无法在批处理文件中解决此问题。 Pageant is GUI application. Pageant是GUI应用程序。 It can hardly somehow signal back to a batch file that it finished loading keys. 它几乎无法以某种方式发信号通知批处理文件它已完成了密钥的加载。

For this reason, Pageant has -c switch , which makes it run a specified program/batch-file after the keys are loaded: 因此, Pageant具有-c switch ,这使得它在加载密钥后运行指定的程序/批处理文件:

You can arrange for Pageant to start another program once it has initialised itself and loaded any keys specified on its command line. 一旦Pageant初始化了自己的程序并加载了其命令行上指定的任何键,就可以安排Pageant启动另一个程序。 This program (perhaps a PuTTY, or a WinCVS making use of Plink, or whatever) will then be able to use the keys Pageant has loaded. 然后,该程序(可能是PuTTY或使用Plink的WinCVS或任何其他工具)将能够使用Pageant加载的键。

You do this by specifying the -c option followed by the command, like this: 您可以通过在命令后指定-c选项来执行此操作,如下所示:

 C:\\PuTTY\\pageant.exe d:\\main.ppk -c C:\\PuTTY\\putty.exe 

So this should to what you want: 因此,这应该符合您的要求:

start E:\PuTTY\pageant.exe E:\Keys\priv.ppk -c C:\path\your_scp_batch.bat

You can take advantage of the way pageant behaves if another instance is already running.如果另一个实例已经在运行,您可以利用选美的行为方式。 This way you can load a key into pageant and then keep using it in a single script.通过这种方式,您可以将密钥加载到选美中,然后继续在单个脚本中使用它。

START pageant.exe
TIMEOUT 1
pageant.exe E:\Keys\priv.ppk

:: Do stuff here.

TASKKILL /im pageant.exe

Here's how it works:这是它的工作原理:

  • START pageant.exe Start pageant in the background. START pageant.exe在后台开始选美。
  • TIMEOUT 1 Suspend the script for a second to ensure pageant is running before executing next line. TIMEOUT 1在执行下一行之前暂停脚本一秒钟以确保选美正在运行。
  • pageant.exe E:\\Keys\\priv.ppk Launch pageant again and tell it to load the key. pageant.exe E:\\Keys\\priv.ppk再次启动 pageant 并告诉它加载密钥。 Since another instance of pageant is already running, this instance terminates after loading the key, making the script proceed only then.由于另一个选美实例已在运行,因此该实例在加载密钥后终止,使脚本仅在那时继续运行。
  • TASKKILL /im pageant.exe Kill pageant so the key can no longer be used. TASKKILL /im pageant.exe杀死选美使密钥无法再使用。

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

相关问题 如何使用JMeter在Pageant中加载SSH密钥 - How can I use JMeter to load SSH keys in Pageant 启动 Pageant SSH-Agent 并将所有密钥加载到一个文件夹中 - Start Pageant SSH-Agent and load all keys in a folder Git Bash 和 Pageant 不使用密钥 - Git Bash and Pageant are not using keys 如何在后台启动批处理文件中运行 ssh 反向隧道? - how to run a ssh reverse tunnel in a startup batch file in background? 尝试在后台运行时未执行批处理文件 - Batch file not executed when trying to run in background 是否可以在程序启动时运行批处理文件? - Is it possible to run a batch file when a program is launched? 如何在我启动一个程序的地方编写批处理文件,当该程序完成或关闭时,启动另一个程序 - How Can I Write a Batch File Where I Start one Program and When that Program Finishes or Closes, Start Another 如何使用 terraform 上的选美来建立 SSH 连接以提供文件? - How to make a SSH connection using pageant on terraform for provisioning files? 在 Windows 服务器中通过 SSH 在网络驱动器中运行批处理文件的最佳方法 - Best way to run batch file in network drive in Windows Sever through SSH 安装TortoiseGit时替代Git的选美 - alternative to pageant for Git when TortoiseGit is installed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM