티스토리 뷰

운영체제

Powershell FTP Client 사용법

warpmemory 2017. 7. 21. 16:24
ftp_upload_example.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ftphost = "%(host)s"
$ftpport = %(port)s
$ftpusername= "%(user)s"
$ftppassword = "%(passwd)s"
 
$filename = [io.path]::GetFileName("%(file_full_path)s")
[Net.ServicePointManager]::ServerCertificateValidationCallback ={$true}
$ftpURL = "ftp://{0}:{1}/{2}" -f $ftphost, $ftpport, $filename
$ftp = [System.Net.FtpWebRequest]::Create($ftpURL)
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.UsePassive = $true
$ftp.UseBinary = $true
$ftp.EnableSsl = $true
$ftp.Credentials = new-object System.Net.NetworkCredential($ftpusername, $ftppassword)
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$rs = $ftp.GetRequestStream()
$reader = New-Object System.IO.FileStream ("%(file_full_path)s", [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read)
[byte[]]$buffer = new-object byte[] 262144
[int]$count = 0
do { $count = $reader.Read($buffer, 0, $buffer.Length); $rs.Write($buffer,0,$count)} while ($count -gt 0)
$reader.Close()
$rs.Close()
cs


ftp_download_example.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ftphost = "%(host)s"
$ftpport = %(port)s
$ftpusername= "%(user)s"
$ftppassword = "%(passwd)s"
 
[Net.ServicePointManager]::ServerCertificateValidationCallback ={$true}
$ftpURL = "ftp://{0}:{1}/%(filename)s" -f $ftphost, $ftpport
$ftp = [System.Net.FtpWebRequest]::Create($ftpURL)
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.UsePassive = $true
$ftp.UseBinary = $true
$ftp.EnableSsl = $true
$ftp.Credentials = new-object System.Net.NetworkCredential($ftpusername, $ftppassword)
$ftp.Method = [System.Net.WebRequestMethods+FTP]::DownloadFile
$rs = $ftp.GetResponse()
$st = $rs.GetResponseStream()
$targetFile = "%(download_dir)s\\%(filename)s"
$writer = New-Object IO.FileStream ($targetFile,[IO.FileMode]::Create)
[byte[]]$buffer = new-object byte[] 262144
do { $count = $st.Read($buffer,0,262144); $writer.Write($buffer,0,$count)} while ($count -ne 0)
$writer.Close()
$st.Close()
$rs.Close()
cs


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
페이지
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함