Solved

SSH Script to Pull File from Server

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:


$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?
3 answers Last reply Best Answer
More about ssh script pull file server
  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"
  2. 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-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"



    Is this correct? I am getting an error

    
    $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
  3. Best answer
    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
Ask a new question

Read More

FTP Servers Apps PowerShell SSH