Topic "Need help writing a batch script for Connecting via Pageant"

Author Message
JWT210

Guest


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
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26572
Location: Prague, Czechia
Pageant is not a tool intended for automation. It's for convenient interactive use.

In script use open -privatekey=... -passphrase=...
https://winscp.net/eng/docs/scriptcommand_open
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?
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26572
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?
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?
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26572
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.
xiSlickix
[View user's profile]

Joined: 2017-05-15
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 (51.43 KB) [Download]

Description: (none)

NEW2-WinSCP-FOM-PO-Sync.bat (541 Bytes) [Download]

Description: Actual WinSCP script

PO-SYNC-TRANSFER-CLEANUP2.cmd (110 Bytes) [Download]

Description: Calls other scripts.

xiSlickix
[View user's profile]

Joined: 2017-05-15
Posts: 2
I guess disregard my previous post - I just read this: https://winscp.net/forum/viewtopic.php?t=11204

Sorry for bringing up an old issue.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26572
Location: Prague, Czechia
See also https://winscp.net/eng/docs/faq_passphrase
Advertisements

You can post new topics in this forum






Search

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License