简体   繁体   English

使用PowerShell从virtualbox下载/上传ftp文件

[英]Downloading/upload files from ftp with powershell from virtualbox

Hello i'm trying to upload files to my virtual machine that run linux and ftp server. 您好我正在尝试将文件上传到运行Linux和ftp服务器的虚拟机。 I have an exception in the code: 我在代码中有一个例外:

    Exception calling "GetRequestStream" with "0" argument(s): "The requested FTP command is not supported when using HTTP proxy."
At D:\Users\h.yordanov\Desktop\PowerShellFtp-master\script.ps1:21 char:1
+ $Run = $FTPRequest.GetRequestStream()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

And the script i used is this: 我使用的脚本是这样的:

    # Config
$Username = "sammy"
$Password = "1111"
$LocalFile = "C:\fff\medias\1618716-maybach-exelero.jpg"
$RemoteFile = "ftp://192.168.56.101/files/1618716-maybach-exelero.jpg"

# Create FTP Rquest Object

    $FTPRequest = [System.Net.FtpWebRequest]::Create("$RemoteFile")
    $FTPRequest = [System.Net.FtpWebRequest]$FTPRequest
    $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile

    $FTPRequest.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
    $FTPRequest.UseBinary = $true
    $FTPRequest.UsePassive = $true
    # Read the File for Upload
    $FileContent = gc -en byte $LocalFile
    $FTPRequest.ContentLength = $FileContent.Length
    # Get Stream Request by bytes
    $Run = $FTPRequest.GetRequestStream()
    $Run.Write($FileContent, 0, $FileContent.Length)
    # Cleanup
    $Run.Close()

$Run.Dispose()

The powershell ver is 5.1. powershell ver是5.1。 I have proxy on the host machine. 我在主机上有代理。

Edit: I'm using the download command now: 编辑:我现在正在使用下载命令:

# Config
$Username = "sammy"
$Password = "1111"
$LocalFile = "C:\fff\medias\1618716-maybach-exelero.jpg"
$RemoteFile = "ftp://192.168.56.101/files/1618716-maybach-exelero.jpg"

# Create a FTPWebRequest 
$FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile) 
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password) 
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile 
$FTPRequest.UseBinary = $true 
$FTPRequest.KeepAlive = $false
# Send the ftp request
$FTPResponse = $FTPRequest.GetResponse() 
# Get a download stream from the server response 
$ResponseStream = $FTPResponse.GetResponseStream() 
# Create the target file on the local system and the download buffer 
$LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create) 
[byte[]]$ReadBuffer = New-Object byte[] 1024 
# Loop through the download 
    do { 
        $ReadLength = $ResponseStream.Read($ReadBuffer,0,1024) 
        $LocalFileFile.Write($ReadBuffer,0,$ReadLength) 
    } 
    while ($ReadLength -ne 0)

But atm i have this error: 但是我有这个错误:

Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (403) Forbidden."
At D:\Users\h.yordanov\Desktop\PowerShellFtp-master\script.ps1:16 char:1
+ $FTPResponse = $FTPRequest.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

Second edit: 第二次编辑:

# Load WinSCP .NET assembly
Add-Type -Path "D:\Users\h.yordanov\Desktop\PowerShellFtp-master\WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = "192.168.56.101"
    UserName = "sammy"
    Password = "1111"
}

# Configure proxy
$sessionOptions.AddRawSettings("ProxyMethod", "1")
$sessionOptions.AddRawSettings("ProxyHost", "proxy.abc.dfg.bg")

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Upload file
    $LocalFile = "C:\fff\medias\test2.txt"
    $RemoteFile = "/home/sammy/ftp/files/test2.txt"
    $session.PutFiles($LocalFile, $RemoteFile).Check()
}
finally
{
    $session.Dispose()
}

After this i get this error: 在此之后我得到这个错误:

Exception calling "Open" with "1" argument(s): "Connection failed.
Can't connect to proxy server
No connection could be made because the target machine actively refused it.
Connection failed."
At D:\Users\h.yordanov\Desktop\PowerShellFtp-master\script.ps1:21 char:5
+     $session.Open($sessionOptions)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SessionRemoteException

Edit 3: 编辑3:

Log from connecting via filezilla. 通过filezilla连接登录。 (I changed the ip) (我换了ip)

2017-06-23 14:50:14 6364 1 Status: Connecting to 192.168.81.3:21...
2017-06-23 14:50:14 6364 1 Status: Connection established, waiting for welcome message...
2017-06-23 14:50:14 6364 1 Response: 220 (vsFTPd 3.0.3)
2017-06-23 14:50:14 6364 1 Command: AUTH TLS
2017-06-23 14:50:14 6364 1 Response: 530 Please login with USER and PASS.
2017-06-23 14:50:14 6364 1 Command: AUTH SSL
2017-06-23 14:50:14 6364 1 Response: 530 Please login with USER and PASS.
2017-06-23 14:50:14 6364 1 Status: Insecure server, it does not support FTP over TLS.
2017-06-23 14:50:14 6364 1 Command: USER sammy
2017-06-23 14:50:14 6364 1 Response: 331 Please specify the password.
2017-06-23 14:50:14 6364 1 Command: PASS ****
2017-06-23 14:50:14 6364 1 Response: 230 Login successful.
2017-06-23 14:50:14 6364 1 Command: SYST
2017-06-23 14:50:14 6364 1 Response: 215 UNIX Type: L8
2017-06-23 14:50:14 6364 1 Command: FEAT
2017-06-23 14:50:14 6364 1 Response: 211-Features:
2017-06-23 14:50:14 6364 1 Response:  EPRT
2017-06-23 14:50:14 6364 1 Response:  EPSV
2017-06-23 14:50:14 6364 1 Response:  MDTM
2017-06-23 14:50:14 6364 1 Response:  PASV
2017-06-23 14:50:14 6364 1 Response:  REST STREAM
2017-06-23 14:50:14 6364 1 Response:  SIZE
2017-06-23 14:50:14 6364 1 Response:  TVFS
2017-06-23 14:50:14 6364 1 Response: 211 End
2017-06-23 14:50:14 6364 1 Status: Server does not support non-ASCII characters.
2017-06-23 14:50:14 6364 1 Status: Logged in
2017-06-23 14:50:14 6364 1 Status: Retrieving directory listing...
2017-06-23 14:50:14 6364 1 Command: PWD
2017-06-23 14:50:14 6364 1 Response: 257 "/" is the current directory
2017-06-23 14:50:14 6364 1 Command: TYPE I
2017-06-23 14:50:14 6364 1 Response: 200 Switching to Binary mode.
2017-06-23 14:50:14 6364 1 Command: PASV
2017-06-23 14:50:14 6364 1 Response: 227 Entering Passive Mode (192,168,81,3,192,147).
2017-06-23 14:50:14 6364 1 Command: LIST
2017-06-23 14:50:14 6364 1 Response: 150 Here comes the directory listing.
2017-06-23 14:50:14 6364 1 Response: 226 Directory send OK.
2017-06-23 14:50:14 6364 1 Status: Directory listing of "/" successful
2017-06-23 14:50:16 6364 1 Status: Retrieving directory listing of "/files"...
2017-06-23 14:50:16 6364 1 Command: CWD files
2017-06-23 14:50:16 6364 1 Response: 250 Directory successfully changed.
2017-06-23 14:50:16 6364 1 Command: PWD
2017-06-23 14:50:16 6364 1 Response: 257 "/files" is the current directory
2017-06-23 14:50:16 6364 1 Command: PASV
2017-06-23 14:50:16 6364 1 Response: 227 Entering Passive Mode (192,168,81,3,193,132).
2017-06-23 14:50:16 6364 1 Command: LIST
2017-06-23 14:50:16 6364 1 Response: 150 Here comes the directory listing.
2017-06-23 14:50:16 6364 1 Response: 226 Directory send OK.
2017-06-23 14:50:16 6364 1 Status: Calculating timezone offset of server...
2017-06-23 14:50:16 6364 1 Command: MDTM test.txt
2017-06-23 14:50:16 6364 1 Response: 213 20170622155419
2017-06-23 14:50:16 6364 1 Status: Timezone offset of server is 0 seconds.
2017-06-23 14:50:16 6364 1 Status: Directory listing of "/files" successful
2017-06-23 14:50:18 6364 1 Status: Retrieving directory listing of "/test"...
2017-06-23 14:50:18 6364 1 Command: CWD /test
2017-06-23 14:50:18 6364 1 Response: 250 Directory successfully changed.
2017-06-23 14:50:18 6364 1 Command: PWD
2017-06-23 14:50:18 6364 1 Response: 257 "/test" is the current directory
2017-06-23 14:50:18 6364 1 Command: PASV
2017-06-23 14:50:18 6364 1 Response: 227 Entering Passive Mode (192,168,81,3,162,156).
2017-06-23 14:50:18 6364 1 Command: LIST
2017-06-23 14:50:18 6364 1 Response: 150 Here comes the directory listing.
2017-06-23 14:50:18 6364 1 Response: 226 Directory send OK.
2017-06-23 14:50:18 6364 1 Status: Directory listing of "/test" successful
2017-06-23 14:50:22 6364 3 Status: Connecting to 192.168.81.3:21...
2017-06-23 14:50:22 6364 3 Status: Connection established, waiting for welcome message...
2017-06-23 14:50:22 6364 3 Response: 220 (vsFTPd 3.0.3)
2017-06-23 14:50:22 6364 3 Command: AUTH TLS
2017-06-23 14:50:22 6364 3 Response: 530 Please login with USER and PASS.
2017-06-23 14:50:22 6364 3 Command: AUTH SSL
2017-06-23 14:50:22 6364 3 Response: 530 Please login with USER and PASS.
2017-06-23 14:50:22 6364 3 Status: Insecure server, it does not support FTP over TLS.
2017-06-23 14:50:22 6364 3 Command: USER sammy
2017-06-23 14:50:22 6364 3 Response: 331 Please specify the password.
2017-06-23 14:50:22 6364 3 Command: PASS ****
2017-06-23 14:50:22 6364 3 Response: 230 Login successful.
2017-06-23 14:50:22 6364 3 Status: Server does not support non-ASCII characters.
2017-06-23 14:50:22 6364 3 Status: Logged in
2017-06-23 14:50:22 6364 3 Status: Starting download of /files/test.txt
2017-06-23 14:50:22 6364 3 Command: CWD /files
2017-06-23 14:50:22 6364 3 Response: 250 Directory successfully changed.
2017-06-23 14:50:22 6364 3 Command: PWD
2017-06-23 14:50:22 6364 3 Response: 257 "/files" is the current directory
2017-06-23 14:50:23 6364 3 Command: TYPE A
2017-06-23 14:50:23 6364 3 Response: 200 Switching to ASCII mode.
2017-06-23 14:50:23 6364 3 Command: PASV
2017-06-23 14:50:23 6364 3 Response: 227 Entering Passive Mode (192,168,81,3,162,251).
2017-06-23 14:50:23 6364 3 Command: RETR test.txt
2017-06-23 14:50:23 6364 3 Response: 150 Opening BINARY mode data connection for test.txt (17 bytes).
2017-06-23 14:50:23 6364 3 Response: 226 Transfer complete.
2017-06-23 14:50:23 6364 3 Status: File transfer successful, transferred 17 bytes in 1 second

I dont see any proxy connection here. 我在这里看不到任何代理连接。

The FtpWebRequest does not support an HTTP proxy for some operations, including file upload. FtpWebRequest不支持某些操作的HTTP代理,包括文件上载。 It's clearly documented on MSDN : 在MSDN上清楚地记录在案

If the specified proxy is an HTTP proxy, only the DownloadFile, ListDirectory, and ListDirectoryDetails commands are supported. 如果指定的代理是HTTP代理,则仅支持DownloadFile,ListDirectory和ListDirectoryDe​​tails命令。

There's no way to upload a file to FTP over an HTTP proxy with PowerShell and the standard .NET framework libraries. 使用PowerShell和标准.NET框架库,无法通过HTTP代理将文件上载到FTP。

You have to use a 3rd party FTP library or PowerShell module. 您必须使用第三方FTP库或PowerShell模块。


For example with WinSCP .NET assembly , you can use: 例如,使用WinSCP .NET程序集 ,您可以使用:

# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = "ftp.example.com"
    UserName = "user"
    Password = "mypassword"
}

# Configure proxy
$sessionOptions.AddRawSettings("ProxyMethod", "3")
$sessionOptions.AddRawSettings("ProxyHost", "proxy")

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Upload file
    $LocalFile = "C:\fff\medias\1618716-maybach-exelero.jpg"
    $RemoteFile = "/files/1618716-maybach-exelero.jpg"
    $session.PutFiles($LocalFile, $RemoteFile).Check()
}
finally
{
    $session.Dispose()
}

For the options for SessionOptions.AddRawSettings , see raw settings . 有关SessionOptions.AddRawSettings的选项,请参阅原始设置

You can have WinSCP GUI generate a PowerShell FTP upload code template , like above, for you. 您可以让WinSCP GUI为您生成PowerShell FTP上传代码模板 ,如上所述。

(I'm the author of WinSCP) (我是WinSCP的作者)


Though your actual problem is that you have proxy configured in your Internet connection options on your machine, while you actually do not want to use them. 虽然您的实际问题是您在计算机上的Internet连接选项中配置了代理,但实际上您并不想使用它们。

The correct solution is to fix the options. 正确的解决方案是修复选项。
Go to Control panel > Network and Internet > Internet Options > Connections > LAN settings . 转到“ 控制面板”>“网络和Internet”>“Internet选项”>“连接”>“LAN”设置

Or as workaround, disable the proxy for a particular connection in your PowerShell code: 或者作为解决方法,在PowerShell代码中禁用特定连接的代理:

$FTPRequest.Proxy = [System.Net.GlobalProxySelection]::GetEmptyWebProxy()

See powershell http get not use proxy . 请参阅powershell http获取不使用代理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM