SSH Script to Pull File from Server

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
0
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:

Code:
$File = "c:\wp test\$(get-date -f yyyy-MM-dd).csv"
$ftp = "ftp://<username>:<password>@<hostname>/<path>"

"ftp url: $ftp"

$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)

"Downloading $File..."

$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?
 

dISh

Distinguished
Dec 24, 2008
2
0
18,520
1
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
 

dISh

Distinguished
Dec 24, 2008
2
0
18,520
1
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-installer.exe
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"

 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
0


Is this correct? I am getting an error

Code:
$File = "c:\wp test\2013-06-30.csv"
$ftp = "ssh://<username>:<password>@<hostname>/var/www/html/wp-content/uploads/gravity-form-export/2013-06-30.csv"

"ssh url: $ftp"

$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)

"Downloading $File..."

$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

 

dISh

Distinguished
Dec 24, 2008
2
0
18,520
1
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
 
Thread starter Similar threads Forum Replies Date
H Apps General Discussion 1
ExtreamChaozZ Apps General Discussion 6
D Apps General Discussion 5
V Apps General Discussion 1
D Apps General Discussion 3
packersfan036 Apps General Discussion 4
S Apps General Discussion 1
G Apps General Discussion 1
SyncroScales Apps General Discussion 4
A Apps General Discussion 1
Tom Waller Apps General Discussion 2
tarmiricmi Apps General Discussion 5
C Apps General Discussion 6
T Apps General Discussion 5
R Apps General Discussion 2
C Apps General Discussion 9
K Apps General Discussion 4
J Apps General Discussion 1
P Apps General Discussion 2
O Apps General Discussion 2

ASK THE COMMUNITY