{"id":1178,"date":"2010-07-19T15:36:06","date_gmt":"2010-07-19T06:36:06","guid":{"rendered":"http:\/\/livedevil.net\/?p=1178"},"modified":"2010-07-19T15:36:06","modified_gmt":"2010-07-19T06:36:06","slug":"powershell-ftp-list-parsing","status":"publish","type":"post","link":"https:\/\/talsu.net\/?p=1178","title":{"rendered":"Powershell &#8211; FTP List Parsing"},"content":{"rendered":"<p>Powershell \uc744 \uc774\uc6a9\ud574 FTP Server\ub97c \uc0c1\ub300\ub85c \uc790\ub3d9\ud654 \uc791\uc5c5\uc744 \ud558\ub2e4\ubcf4\ub2c8 File \uc5c5\ub85c\ub4dc, \ub2e4\uc6b4\ub85c\ub4dc \ubfd0 \ub9cc \uc544\ub2c8\ub77c. \ud2b9\uc815 FTP \uacbd\ub85c \uc544\ub798\uc5d0 \uc788\ub294 File \uacfc Directory \ub4e4\uc758 \uc815\ubcf4\uac00 \ud544\uc694 \ud588\ub2e4. \uc774\ub54c FTP Server\ub97c \uc0c1\ub300\ub85c WebRequest\ub97c \ubcf4\ub0b4\ub294\ub370 \uc774\ub54c Request Method\ub97c &#8220;List&#8221;\ub85c \ud55c\ub2e4.<\/p>\n<p>\uc774\uc804\uc5d0 \uc62c\ub9b0 \ud3ec\uc2a4\ud2b8\uc778 Get-WebResponseString \uc744 \uc774\uc6a9\ud558\uc5ec List\ub97c \uc694\uccad \ud574 \ubcf4\uc790.<\/p>\n<p><\/br><br \/>\n<strong>\ucc38\uace0<\/strong> <a href=\"?p=1166\">Get-WebResponseString<\/a><br \/>\n<\/br><\/p>\n<pre class=\"lang:ps decode:true\">\n$Url = \"ftp:\/\/Use-Powershell.com\"\n$Username = \"talsu\"\n$Password = \"pass1234\"\n\n$credential = New-Object System.Net.NetworkCredential @($Username, $Password)\n\nGet-WebResponseString -Url $Url -Credential $credential -Method \"List\"\n<\/pre>\n<p>\ub2e4\uc74c\uacfc \uac19\uc740 \uacb0\uacfc\uac00 \ub098\uc628\ub2e4. \ub0b4\uac00 \ud0c0\uac9f\uc73c\ub85c \ud558\uace0 \uc788\ub294 FTP Server\ub294 Ubuntu\uc5d0 \uc124\uce58 \ub418\uc5b4 \uc788\ub294 vsFTPd \uc774\ub2e4. \ub9cc\uc57d \ub2e4\ub978 FTP Server\ub77c\uba74 \ucd9c\ub825 \ud3ec\uba67\uc774 \ub2e4\ub97c \uc218\ub3c4 \uc788\ub2e4.<\/p>\n<pre class=\"lang:default nums:false decode:true\">\ndrwxrwxrwx   10 1000     1000         4096 Jul 16 09:53 Storage\n-rw-r--r--    1 1000     1000          179 Jun 16 21:19 examples.desktop\ndrwx------    2 1000     1000         4096 Jul 01 17:55 test\ndrwxr-xr-x    2 1000     1000         4096 Jun 16 21:29 \uacf5\uac1c\ndrwxr-xr-x    2 1000     1000         4096 Jul 05 12:18 \ub2e4\uc6b4\ub85c\ub4dc\ndrwxr-xr-x    2 1000     1000         4096 Jun 16 21:29 \ubb38\uc11c\ndrwxr-xr-x    2 1000     1000         4096 Jul 01 22:52 \ubc14\ud0d5\ud654\uba74\ndrwxr-xr-x    2 1000     1000         4096 Jun 16 21:29 \ube44\ub514\uc624\ndrwxr-xr-x    3 1000     1000         4096 Jul 01 22:29 \uc0ac\uc9c4\ndrwxr-xr-x    2 1000     1000         4096 Jun 16 21:29 \uc74c\uc545\ndrwxr-xr-x    2 1000     1000         4096 Jun 16 21:29 \ud15c\ud50c\ub9bf\n<\/pre>\n<p>\uc774 \ucd9c\ub825\uc740 \ud558\ub098\uc758 \ubb38\uc790\uc5f4\uc774\ub2e4. \uc774\uac83\uc744 Parsing \ud574\uc57c \uc6d0\ud558\ub294 \uc815\ubcf4\ub85c \uc815\ub82c\ud558\uace0 \uc0ac\uc6a9 \ud560 \uc218 \uc788\ub2e4. \ub2e4\uc74c\uc740 FTP List\ub97c Parsing \ud558\ub294 Function \uc778\ub370 \ucd94\ucd9c\ud558\ub294 Regex\ub294 \uc790\uc2e0\uc758 Server\uc640 \ub2e4\ub97c\uc218 \uc788\uc73c\ub2c8 \uc54c\ub9de\uac8c \uc218\uc815 \ud558\ub3c4\ub85d \ud558\uc790.<\/p>\n<h2>Convert-FtpList<\/h2>\n<pre class=\"lang:ps wrap:false decode:true\">\nFunction Convert-FtpList\n{\n\tparam ([String]$ListString)\n\n\t$FileCache = [System.IO.Path]::GetTempFileName()\n\tSet-Content -Path $FileCache -Value $ListString\n\n\t$FTPListRegex = \"(?<dir>[-ld])(?<permission>([-r][-w][-xs]){3})s+(?<filecode>d+)s+(?<owner>w+)s+(?<group>w+)s+(?<size>d+)s+(?<timestamp>(w+s+d+s+d{2}:d{2})|w+s+d+s+d{4})s+(?<name>.+)\"\n\n\t$PSObjects = (Get-Content $FileCache) | % {\n\t\tif ($_ -match $FTPListRegex )\n\t\t{\n\t\t\tNew-Object PSObject -Property @{\n\t\t\t\tName = [String]$Matches[\"name\"];\n\t\t\t\tSize = [Int64]$Matches[\"size\"];\n\t\t\t\tType = $(if($Matches[\"dir\"] -eq \"d\"){ \"Directory\" }else{\"File\"});\n\t\t\t\tLastWriteTime = [DateTime]$Matches[\"timestamp\"];\n\t\t\t}\n\t\t}\n\t}\n\n\tRemove-Item $FileCache\n\n\treturn $PSObjects | Sort Type\n}\n<\/pre>\n<p>\uc784\uc2dc \ud30c\uc77c\uc744 \ub9cc\ub4e4\uc5b4 String\uc744 File\uc5d0 \uc4f0\uace0 Get-Content \uc758 \ucd9c\ub825\uc744 Pipeline \uc73c\ub85c \ubcf4\ub0b4\uba74 \ud55c\uc904\uc529 \uc811\uadfc \ud560 \uc218 \uc788\ub2e4. \ud55c\uc904\uc529 \uc811\uadfc \ud558\uae30 \uc704\ud574 \uc5ec\ub7ec\uac00\uc9c0 \ubc29\ubc95\uc774 \uc788\uc744 \uc218 \uc788\uc9c0\ub9cc \ud3b8\ud55c \ubc29\ubc95\uc73c\ub85c \ud588\ub2e4. (\ud6a8\uc728\uc740 \ubcc4\ub85c \uc88b\uc9c0 \ubabb\ud558\ub2e4.)<\/p>\n<p>\ud55c\uc904\uc529 \uc811\uadfc\ud558\uc5ec Regex\ub85c \uac12\uc744 \ucd94\ucd9c \ud574\ub0b4\uace0 \uadf8\uac83\uc744 Hash Table\uc744 \ub9cc\ub4e0\ub2e4. \uc774\ub54c \ud615 \ubcc0\ud658\uc744 \uac19\uc774 \ud558\ub294\ub370 DataTime \ud0c0\uc785\uc774 \ud3b8\ub9ac\ud558\uac8c\ub3c4 \ubcc4\ub2e4\ub978 \uacfc\uc815 \uc5c6\uc774 Parsing\uc774 \uc798 \ub418\uc5c8\ub2e4.<\/p>\n<p>\ucd5c\uc885 \ucd9c\ub825\uc740 HashTable\uc744 Property\ub85c \uc62e\uae30\ub294 Custom PSObject \ub4e4\uc774\ub2e4. \uc774\uac83\uc73c\ub85c \uc6d0\ud558\ub294 \uc815\ubcf4\ub85c \uc815\ub82c \uc791\uc5c5\uc744 \ud560 \uc218 \uc788\uac8c \ub418\uc5c8\ub2e4.<\/p>\n<h2>\uc0ac\uc6a9 \uc608<\/h2>\n<pre class=\"lang:ps decode:true\">\n$Url = \"ftp:\/\/Use-Powershell.com\"\n$Username = \"talsu\"\n$Password = \"pass1234\"\n\n$credential = New-Object System.Net.NetworkCredential @($Username, $Password)\n\n$ListString = Get-WebResponseString -Url $Url -Credential $credential -Method \"List\"\n\nConvert-FtpList $ListString\n<\/pre>\n<p>\ucd9c\ub825\uc740 \ub2e4\uc74c\uacfc \uac19\ub2e4. (Format-Table Type, LastWriteTime, Size, Name -AutoSize)<\/p>\n<pre class=\"lang:default nums:false decode:true\">\nType      LastWriteTime               Size Name\n----      -------------               ---- ----\nDirectory 2010-07-19 \uc624\ud6c4 4:21:00     4096 \uacf5\uac1c\nDirectory 2010-07-19 \uc624\uc804 5:12:00     4096 \ub2e4\uc6b4\ub85c\ub4dc\nDirectory 2010-07-19 \uc624\ud6c4 4:21:00     4096 \ud15c\ud50c\ub9bf\nDirectory 2010-07-19 \uc624\ud6c4 4:21:00     4096 \ubb38\uc11c\nDirectory 2010-07-19 \uc624\uc804 1:22:00     4096 \uc0ac\uc9c4\nDirectory 2010-07-19 \uc624\ud6c4 4:21:00     4096 \uc74c\uc545\nDirectory 2010-07-19 \uc624\uc804 1:22:00     4096 \ubc14\ud0d5\ud654\uba74\nDirectory 2010-07-19 \uc624\ud6c4 4:21:00     4096 \ube44\ub514\uc624\nDirectory 2010-07-19 \uc624\ud6c4 4:09:00     4096 Storage\nDirectory 2010-07-19 \uc624\uc804 1:17:00     4096 test\nFile      2010-07-19 \uc624\ud6c4 4:21:00      179 examples.desktop\nFile      2010-07-19 \uc624\ud6c4 11:01:00 2964966 wordpress-3.0.zip\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Powershell \uc744 \uc774\uc6a9\ud574 FTP Server\ub97c \uc0c1\ub300\ub85c \uc790\ub3d9\ud654 \uc791\uc5c5\uc744 \ud558\ub2e4\ubcf4\ub2c8 File \uc5c5\ub85c\ub4dc, \ub2e4\uc6b4\ub85c\ub4dc \ubfd0 \ub9cc \uc544\ub2c8\ub77c. \ud2b9\uc815 FTP \uacbd\ub85c \uc544\ub798\uc5d0 \uc788\ub294 File \uacfc Directory \ub4e4\uc758 \uc815\ubcf4\uac00 \ud544\uc694 \ud588\ub2e4. \uc774\ub54c FTP Server\ub97c \uc0c1\ub300\ub85c WebRequest\ub97c \ubcf4\ub0b4\ub294\ub370 \uc774\ub54c Request Method\ub97c &#8220;List&#8221;\ub85c \ud55c\ub2e4. \uc774\uc804\uc5d0 \uc62c\ub9b0 \ud3ec\uc2a4\ud2b8\uc778 Get-WebResponseString \uc744 \uc774\uc6a9\ud558\uc5ec List\ub97c \uc694\uccad \ud574 \ubcf4\uc790. \ucc38\uace0 Get-WebResponseString $Url = &#8220;ftp:\/\/Use-Powershell.com&#8221; $Username = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[26],"tags":[80,121,122,124,163,481,212,213,224],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pXV5a-j0","_links":{"self":[{"href":"https:\/\/talsu.net\/index.php?rest_route=\/wp\/v2\/posts\/1178"}],"collection":[{"href":"https:\/\/talsu.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/talsu.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/talsu.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/talsu.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1178"}],"version-history":[{"count":0,"href":"https:\/\/talsu.net\/index.php?rest_route=\/wp\/v2\/posts\/1178\/revisions"}],"wp:attachment":[{"href":"https:\/\/talsu.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/talsu.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/talsu.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}