Connect to ftp via Proxy

Advertisement

Finn
Joined:
Posts:
2
Location:
Denmark

Connect to ftp via Proxy

Hi

I'm trying to connect to an FTP server and have to do i via a proxy server.
I can connect to my internal server via below powershell and get the files I need but when I add
$sessionOptions.AddRawSettings("ProxyMethod", "3")
$sessionOptions.AddRawSettings("ProxyHost", "proxy server ip")
$sessionOptions.AddRawSettings("ProxyPort", "8080")
I do get a connection failed.
I use the same code to connect to the external ftp whit the added sessionoptions-addrawsettings .....
Can any one help ?
I know that the firewall and proxy do work as i can connect via the GUI and via cli with below cli as well
WinSCP.exe ftp://ipaddress-of-remote-ftp -rawsettings ProxyMethod=3 ProxyHost=proxy-ip-address ProxyPort=8080

# Load WinSCP .NET assembly
Add-Type -Path "C:\_install\WinSCP-5.17.9-Automation\WinSCPnet.dll"
 
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::ftp
    HostName = "ftp.examlpe.cp,"
    UserName = "username"
    Password = "mypassword"
    
}
 
$session = New-Object WinSCP.Session
 
try
{
    # Connect
    $session.Open($sessionOptions)
 
    # Get list of files in the directory
    $directoryInfo = $session.ListDirectory($remotePath)
 
    # Select the most recent file
    some more code

Reply with quote

Advertisement

Finn

No one who is able to help

I still get connection time uot when i try to connect via our blue coad proxy.
But as i mentioned i can connect via the GUI and via cli

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
36,025
Location:
Prague, Czechia

Re: Connect to ftp via Proxy

Please post full session log files both from the GUI (or the cli) and your PowerShell script.

Reply with quote

Guest

Re: Connect to ftp via Proxy

Sorry for the late reply but i left Work early

Here is my PowerShell script
param (
    $localPath = "c:\lidar\downloaded\",
    $remotePath = "/Lid/WIPO0100306/average_data/",
    $remotePath2 = "/Nac/T10min/",
    $remotePath3 = "/Tow/T10min/",
    $fileName = "*"
)
 
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\lidar\WinSCP-5.17.9-Automation\WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions.AddRawSettings("ProxyMethod", "3")
    $sessionOptions.AddRawSettings("ProxyHost", "proxy server ip")
    $sessionOptions.AddRawSettings("ProxyPort", "8080")
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::ftp
        HostName = "ftp server address"
        UserName = "my-username"
        Password = "my-password "
 
    }
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        # Get list of files in the directory
        $directoryInfo = $session.ListDirectory($remotePath)
 
        # Select the most recent file
        $latest =
            $directoryInfo.Files |
            Where-Object { -Not $_.IsDirectory } |
            Sort-Object LastWriteTime -Descending |
            Select-Object -First 1
 
        # Any file at all?
        if ($latest -eq $Null)
 
 
        {
            Write-Host "No file found"
            exit 1
        }
 
        # Download the selected file
        $session.GetFiles(
            [WinSCP.RemotePath]::EscapeFileMask($latest.FullName), $localPath).Check()
 
##------------------NEXT FILE PARH ----------------------------------##
 
        # Get list of files in the directory
        $directoryInfo = $session.ListDirectory($remotePath2)
 
        # Select the most recent file
        $latest =
            $directoryInfo.Files |
            Where-Object { -Not $_.IsDirectory } |
            Sort-Object LastWriteTime -Descending |
            Select-Object -First 1
 
        # Any file at all?
        if ($latest -eq $Null)
 
 
        {
            Write-Host "No file found"
            exit 1
        }
 
        # Download the selected file
        $session.GetFiles(
            [WinSCP.RemotePath]::EscapeFileMask($latest.FullName), $localPath).Check()
 
##------------------NEXT FILE PARH ----------------------------------##
 
         # Get list of files in the directory
        $directoryInfo = $session.ListDirectory($remotePath3)
 
        # Select the most recent file
        $latest =
            $directoryInfo.Files |
            Where-Object { -Not $_.IsDirectory } |
            Sort-Object LastWriteTime -Descending |
            Select-Object -First 1
 
        # Any file at all?
        if ($latest -eq $Null)
 
 
        {
            Write-Host "No file found"
            exit 1
        }
 
        # Download the selected file
        $session.GetFiles(
            [WinSCP.RemotePath]::EscapeFileMask($latest.FullName), $localPath).Check()
 
##------------------NEXT FILE PARH ----------------------------------##
 
    }
 
 
 
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum