Need help writing a batch script for Connecting via Pageant

Advertisement

JWT210
Guest

Need help writing a batch script for Connecting via Pageant

Good afternoon,

I'm trying to connect to an external vendor's server via batch script. The vendor requested that we connect/authenticate using public keys. I created a public key using pageant and sent that to the vendor. I then can open WinSCP, click on tools, run pageant. I right-click on the icon from the toolbar, and add the key, then enter the phrase. From there I can enter the host name and user (no password). Once I connect, I can drag and drop the file.

At this point, I'd like to code this using a batch script. We current use batch scripting to connect elsewhere with vendor authenticating using host keys. This would be the first time using private/public keys. I created a bat file with the following command:

"C:\Program Files (x86)\WinSCP\WinSCP.com" /script=c:\script.txt

At this point, I'm confused about what to put into my script.txt. Normally for a host key connection, I would enter:

open sftp://user:password@url -hostkey="XXXXXXXX"

Reading through the documentation, I initially tried starting pageant, loading my keys, then having starting WINSCP using this:

"C:\Program Files (x86)\WinSCP\PuTTY\pageant.exe" c:\publickey.ppk -c "C:\Program Files (x86)\WinSCP\winscp.com" /script=c:\script.txt

But I'm not sure how to incorporate the phrase into the script. Would you be able to assist? Just wondering if I'm going in the right direction.

Thanks.

CD

Reply with quote

Advertisement

JWT210
Guest

Martin,

Thanks for the info. Just to confirm, is there any way to run the program via batch/automation in the same manner as it is used interactively?

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
40,551
Location:
Prague, Czechia

JWT210 wrote:

Just to confirm, is there any way to run the program via batch/automation in the same manner as it is used interactively?
I do not understand, what you ask.
What program? WinSCP or Pageant? What "manner" do you mean? Be specific please! What do you want to achieve?

Reply with quote

JWT210
Guest

martin wrote:

JWT210 wrote:

Just to confirm, is there any way to run the program via batch/automation in the same manner as it is used interactively?
I do not understand, what you ask.
What program? WinSCP or Pageant? What "manner" do you mean? Be specific please! What do you want to achieve?

We are looking to send data to a vendor on a nightly basis and would prefer doing this via batch, not interactively. I'm trying to write a script to run pageant via batch to load the public key, authenticate the passphrase, then connect to the url and send the data. The software documentation stated I could run pageant to achieve by running pageant and loading the private keys at start up...then start another program after the keys are loaded (https://winscp.net/eng/docs/ui_pageant). My .bat file runs the following:

"C:\Program Files (x86)\WinSCP\PuTTY\pageant.exe" c:\publickey.ppk -c "C:\Program Files (x86)\WinSCP\winscp.com" /script=c:\script.txt

My script.txt file has the following:

option batch continue
option confirm off
open sftp://userid@hosturl -privatekey=c:\privatekey.ppk -passphrase="passphrase"
cd /newdirectory
put c:\file /file.txt

When I run this bat file via dos command line, the window freezes and nothing happens.

Is it possible to run pageant via batch to load the private key, then run winscp to transfer the data or can pageant do both?

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,551
Location:
Prague, Czechia

JWT210 wrote:

Is it possible to run pageant via batch to load the private key, then run winscp to transfer the data or can pageant do both?
Yes, your command above does that. But you cannot provide a passphrase for encrypted keys this way. It would not even make any sense. It would defy the purpose of Pageant.

Why do you want to use Pageant? It does not make any sense for automatic tasks.

It looks like, you have some conceptual misunderstanding, that keeps pushing you on a wrong track.

Reply with quote

xiSlickix
Joined:
Posts:
2

I have almost the exact same scenario. Rather than placing files on a server, I am pulling new files twice a day. The same situation where I generated a private / public key combination, gave the public key to the SFTP server admin, and have a passphrase on my private key.
I have scripted a batch file that moves old log and purchase order text files from one directory to another, then calls WinSCP to do a one way sync (see attached scripts). If I manually run PO-SYNC-TRANSFER-CLEANUP2.cmd everything executes fine. If I have windows task scheduler run the same command (with admin priv, etc.) the process fails.

Pageant seems to be the missing link here.

Logs.png

Description: Actual WinSCP script
Description: Calls other scripts.

Reply with quote

Advertisement

You can post new topics in this forum