Sign in with
Sign up | Sign in
Your question
Solved

SSH Script to Pull File from Server

Last response: in Applications
Share
July 2, 2013 8:10:50 AM

Hello All,

To start off I created a PowerShell script that worked wonderful for pulling the weekly .csv file from the server and deleting it afterwards. This was done on our QA server. Here's the code:

  1. $File = "c:\wp test\$(get-date -f yyyy-MM-dd).csv"
  2. $ftp = "ftp://<username>:<password>@<hostname>/<path>"
  3.  
  4. "ftp url: $ftp"
  5.  
  6. $webclient = New-Object System.Net.WebClient
  7. $uri = New-Object System.Uri($ftp)
  8.  
  9. "Downloading $File..."
  10.  
  11. $webclient.DownloadFile($uri, $File)


Problem is that our PROD server uses SSH which seems not to work with this script. I have been looking everywhere for a SSH script that can be automated to log on to the server, go to the path, download the file, then delete it.

Any suggestions?
July 2, 2013 8:58:38 AM

Hi Laserpp,

I'm not familiar with powershell but instead of ftp you can use sftp.

Grab Putty for windows, http://tartarus.org/~simon/putty-snapshots/x86/putty-in...
Put the installed directory in system $path or link your script directly to the sftp.exe file.

For the script it is pretty much the same except use sftp with something like:
$ftp = "ssh://<username>:<password>@<hostname>/wp test/$(get-date -f yyyy-MM-dd).csv"

m
0
l
July 2, 2013 1:27:39 PM

dISh said:
Hi Laserpp,

I'm not familiar with powershell but instead of ftp you can use sftp.

Grab Putty for windows, http://tartarus.org/~simon/putty-snapshots/x86/putty-in...
Put the installed directory in system $path or link your script directly to the sftp.exe file.

For the script it is pretty much the same except use sftp with something like:
$ftp = "ssh://<username>:<password>@<hostname>/wp test/$(get-date -f yyyy-MM-dd).csv"



Is this correct? I am getting an error

  1. $File = "c:\wp test\2013-06-30.csv"
  2. $ftp = "ssh://<username>:<password>@<hostname>/var/www/html/wp-content/uploads/gravity-form-export/2013-06-30.csv"
  3.  
  4. "ssh url: $ftp"
  5.  
  6. $webclient = New-Object System.Net.WebClient
  7. $uri = New-Object System.Uri($ftp)
  8.  
  9. "Downloading $File..."
  10.  
  11. $webclient.DownloadFile($uri, $File)


ERROR:
Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At line:11 char:24
+ $webclient.DownloadFile <<<< ($uri, $File)
+ CategoryInfo : NotSpecified: (:)  [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

m
0
l

Best solution

July 9, 2013 10:17:35 AM

Hi Laser,

Sorry little rusty.. based on your response, you want to call scp (secure copy).

Try run the following on the command prompt.

c:\program files (x86)\Putty\pscp.exe -pw <password> <username>@<hostname>:/var/www/html/wp-content/uploads/gravity-form-export/2013-06-30.csv c:\wp test\2013-06-30.csv

or with strings..

_cmd="c:\program files (x86)\Putty\pscp.exe -pw"
usr="username"
pwd="password"
src_file="/var/www/html/wp-content/uploads/gravity-form-export/$(get-date -f yyyy-MM-dd).csv"
dst_file="c:\wp test\$(get-date -f yyyy-MM-dd).csv"

$_cmd $pwd $usr@$host:$src_file $dst_file


hope that works for you..
I have no idea how to put that into PowerShell, should be easy tho
Share
!