Command Line SFTP with SCP

If you’re making a batch file that requires you to upload some files over SFTP/SCP then you’ll need to use an external program such as WinSCP that has command line access to upload the files. Unfortunately you cannot use the native FTP client in Windows. First create a config file with your server settings and save it in the same directory as your batch file. I also make use of an RSA key pair for added security, in this example that public key will be saved as key.ppk using PuTTY’s key format.

# Automatically answer all prompts negatively not to stall
# the script on errors
option batch on

# Disable overwrite confirmations that conflict with the previous
option confirm off

# Connect using a password
# open user:password@example.com
# Connect
open sftp://user:pass@ip:22 -hostkey="ssh-rsa 2048 ba:52:06:a7:7a:4a:17:1e:49:a9:ef:ae:08:5b:90:54" -privatekey="key.ppk"

# Change remote directory
cd /home/user/files

# Force binary mode transfer
option transfer binary

# Upload file to the server
put file2upload.jar

# Disconnect
close

# Exit WinSCP
exit

Now in your batch file, after you’re done compiling or what ever it is you need to do first, add this line (making sure to replace the path for WinSCP with the one you’ve used on your system):

"C:\Program Files (x86)\WinSCP\WinSCP.exe" /console /script=sftp.scp

Tags: , , , ,

  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Leave a Reply