When automating SFTP transfers using PuTTY's PSFTP.exe with private key authentication, a common roadblock occurs when the private key requires a passphrase. The standard command:
psftp.exe user@host -i private_key.ppk -b script.scr
will still prompt for manual passphrase entry, breaking automation workflows. Let's explore robust solutions.
Using puttygen.exe:
puttygen private_key.ppk -O private -o passwordless_key.ppk
This removes the passphrase requirement but significantly reduces security - only recommended for isolated systems.
First load the key into Pageant:
pageant.exe private_key.ppk
Then modify your batch file:
@echo off
echo your_passphrase | pageant.exe private_key.ppk
psftp.exe user@host -agent -b script.scr
For Windows, use a PowerShell expect-like script:
$process = Start-Process "psftp.exe" -ArgumentList "user@host -i private_key.ppk" -NoNewWindow -PassThru -RedirectStandardInput input.txt
$process.WaitForExit()
Where input.txt contains your passphrase followed by your script commands.
Each method has tradeoffs:
- Passwordless keys: Convenient but insecure
- Pageant: Better security but requires running service
- Expect scripts: Flexible but stores passphrase in files
Consider these more automation-friendly alternatives:
WinSCP /command "open sftp://user:pass@host/ -privatekey=key.ppk" ^
"put *.txt" "exit"
Or for Linux-like environments:
sshpass -f passphrase.txt sftp -i id_rsa user@host
When automating SFTP transfers using PuTTY's psftp.exe
with private key authentication, the passphrase prompt creates a significant automation roadblock. Unlike SSH keys without passphrases, protected keys require interactive input, breaking batch processing workflows.
Before proceeding, understand that automating passphrase input reduces security. The passphrase exists specifically to prevent automated use of stolen private keys. Consider these alternatives first:
1. Use key-based authentication without passphrase (for low-risk environments)
2. Store passphrase-protected key in Pageant (PuTTY authentication agent)
3. Use ssh-agent on Unix systems
4. Implement proper secret management solutions like HashiCorp Vault
When you must automate passphrase input, here are three approaches:
Method 1: Using Plink with -pw Parameter
c:\putty\plink.exe -ssh -i private_key.ppk -pw "your_passphrase" \
username@ftpsite.com -batch -m c:\putty\myscript.scr
Method 2: Expect Script for PSFTP
Create an expect script (requires Windows expect implementation like Expect for Windows):
#!/usr/bin/expect
spawn psftp.exe username@ftpsite.com -i private_key.ppk
expect "Passphrase for key"
send "your_passphrase\r"
expect "sftp>"
send "put File1.txt\r"
# ... additional commands
send "quit\r"
expect eof
Method 3: Convert to OpenSSH Format and Use sshpass
Convert PPK to OpenSSH format using PuTTYgen, then:
sshpass -p "your_passphrase" sftp -oIdentityFile=~/.ssh/id_rsa username@ftpsite.com
If you must implement passphrase automation:
- Restrict script permissions to minimum required users
- Store scripts in secure locations with limited access
- Rotate passphrases regularly
- Monitor SFTP access logs for anomalies
- Consider IP whitelisting for automated connections
For enterprise environments, consider these more secure patterns:
1. Jump server with pre-authenticated connections
2. API-based file transfer services
3. Certificate-based authentication
4. Short-lived credentials with STS (Security Token Service)