Jump to content

How to download a file from a CGI-BIN folder


Recommended Posts

Hello. I want to download a text file that's in a cgi-bin folder and am not sure the best way to do this. I know how to use InetGet to download files that aren't in cgi-bin folders, and I know how to use the _FtpPutFile function to upload files via FTP, but how can I use FTP to download a file from a protected cgi-bin folder? It's easy to do with my FTP client (FileZilla), so I'm assuming there must be a way to do it with AutoIt3. I did read the InetGet help, but couldn't figure out how to apply it to this situation. When I tried a normal InetGet, a file did download, but it was just a 500.shtml file (the custom 500 Internal Server Error page I made for the site). Please advise.

Link to comment
Share on other sites

Are you making sure your login information is the same as you're using for FileZilla? If you're using the anonymous user, you won't have rights to what you need.

Also, the help for InetGet says to use "user:pass@" to specify login information, but I think IE6 blocks this usage. I haven't tested this, so I'm asking whether InetGet has anything to do with this removed functionality in IE or does the code behind InetGet handle this login data on its own?

It would be nice to have a real function to set the login information, such as:

FtpSetLogin("username", "password")
Edited by c0deWorm

My UDFs: ExitCodes

Link to comment
Share on other sites

Link to comment
Share on other sites

  • Developers

I've tried these four kinds of InetGets:

..all to no avail.

<{POST_SNAPBACK}>

What is the exact path you download from when connection with Filezilla ?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

What is the exact path you download from when connection with Filezilla ?

<{POST_SNAPBACK}>

When I see the word path, as opposed to URL, I think of the actual server path, which would be:

/home/account/public_html/cgi-bin/folder/file.log

if that's what you mean

wininet.dll, ftpgetfile.

<{POST_SNAPBACK}>

I know about the ftpputfile function already, so I had already searched for what I figured would be a sister function (ftpgetfile) and didn't find anything at all in the help file or in the AutoIt3 forums, so I'm not sure what you're referring to. I'm trying to stay within AutoIt's functionality here, as opposed to doing a Google and finding some third-party tool.
Link to comment
Share on other sites

  • Developers

When I see the word path, as opposed to URL, I think of the actual server path, which would be:

/home/account/public_html/cgi-bin/folder/file.log

if that's what you mean

I know about the ftpputfile function already, so I had already searched for what I figured would be a sister function (ftpgetfile) and didn't find anything at all in the help file or in the AutoIt3 forums, so I'm not sure what you're referring to. I'm trying to stay within AutoIt's functionality here, as opposed to doing a Google and finding some third-party tool.

<{POST_SNAPBACK}>

Nope i meant the path in FileZilla.....

When you connect with filezilla you probably have to change the directory... what is the path Filezilla shows when you have made cgi-bin as the current directory ?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Nope i meant the path in FileZilla.....

When you connect with filezilla you probably have to change the directory... what is the path Filezilla shows when you have made cgi-bin as the current directory ?

<{POST_SNAPBACK}>

OK, here's what I see for Remote Site when I make the cgi-bin folder the current directory:

/public_html/cgi-bin/

I hope that's what you mean. And thank you for your patience with me.

Link to comment
Share on other sites

autoit can call standard windows dll's (wininet.dll) to add functionality.

ex. _FtpPutFile (wich i made)

<{POST_SNAPBACK}>

OK, I found info on FtpGetFile on this MSDN page. Please tell me: to pull this off in AutoIt3, will I need to use all three dllCall, dllOpen, and dllClose functions? Edited by egalvez
Link to comment
Share on other sites

  • Developers

OK, here's what I see for Remote Site when I make the cgi-bin folder the current directory:

/public_html/cgi-bin/

I hope that's what you mean.  And thank you for your patience with me.

<{POST_SNAPBACK}>

Assuming the file you want to dowload is located in a subdirectory "folder" then this should be the correct url:

InetGet("ftp://username:password@hostIP/public_html/cgi-bin/folder/file.log", "C:\file.log")

Last thing i can think of is : you could check is if there is any case issue with any character....

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I took the FtpPutFile function from this post and tried to hack it to turn it into an FtpGetFile function, but it's not working because I don't really know what I'm doing. I'm wondering if someone who does know what they're doing could help me, please. Here's what I've got:

; Access types for InternetOpen()
Global $INTERNET_OPEN_TYPE_PRECONFIG = 0
Global $INTERNET_OPEN_TYPE_DIRECT = 1
Global $INTERNET_OPEN_TYPE_PROXY = 3
Global $INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4

; Service types for InternetConnect()
Global $INTERNET_SERVICE_FTP = 1
Global $INTERNET_SERVICE_GOPHER = 2
Global $INTERNET_SERVICE_HTTP = 3

Global $INTERNET_DEFAULT_FTP_PORT = 21

Global $INTERNET_FLAG_ASYNC = 0x10000000
Global $INTERNET_FLAG_PASSIVE = 0x08000000

Global $FTP_TRANSFER_TYPE_UNKNOWN = 0x00000000
Global $FTP_TRANSFER_TYPE_ASCII = 0x00000001
Global $FTP_TRANSFER_TYPE_BINARY = 0x00000002

Dim $nRetCode

Func _FtpGetFile($sServer, $sUserName, $sPassword, $sRemoteFile, $sNewFile, $bFailIfExists, $hFlagsAndAttributes, $nType = 0, $nPassive = 1)
   Local $hWininet
   Local $hInternet
   Local $hConnect
   Local $aDllRet
   Local $nRet = 0
   
   $hWininet = DllOpen("wininet.dll")
   If $hWininet = -1 Then Return $nRet
   $aDllRet = DllCall($hWininet, "ptr", "InternetOpen", _
                      "str", "AutoITFTPUDF", _
                      "long", $INTERNET_OPEN_TYPE_DIRECT, _
                      "int", 0, _
                      "int", 0, _
                      "long", $INTERNET_FLAG_ASYNC)
   If Not @error And $aDllRet[0] <> 0 Then
      $hInternet = $aDllRet[0]
      If $nPassive Then
         $nPassive = $INTERNET_FLAG_PASSIVE
      Else
         $nPassive = 0
      EndIf
      $aDllRet = DllCall($hWininet, "ptr", "InternetConnect", _
                         "ptr", $hInternet, _
                         "str", $sServer, _
                         "long", $INTERNET_DEFAULT_FTP_PORT, _
                         "str", $sUserName, _
                         "str", $sPassword, _
                         "long", $INTERNET_SERVICE_FTP, _
                         "long", $nPassive, _
                         "int", 0)
      If Not @error And $aDllRet[0] <> 0 Then
         $hConnect = $aDllRet[0]
         $aDllRet = DllCall($hWininet, "int", "FtpGetFile", _
                            "ptr", $hConnect, _
                            "str", $sRemoteFile, _
                            "str", $sNewFile, _
                            "bool", $bFailIfExists, _
                            "str", $hFlagsAndAttributes, _
                            "long", $nType, _
                            "int", 0)
         If Not @error Or $aDllRet[0] <> 0 Then
            $nRet = -1
         Else
            $aDllRet = DllCall("kernel32.dll", "long", "GetLastError")
            If Not @error Then $nRet = $aDllRet[0]
         EndIf
      ; Send QUIT command to ftp server
         $aDllRet = DllCall($hWininet, "int", "FtpCommand", _
                            "ptr", $hConnect, _
                            "int", 0, _
                            "long", $FTP_TRANSFER_TYPE_ASCII, _
                            "str", "QUIT", _
                            "int", 0, _
                            "int", 0)
         DllCall($hWininet, "int", "InternetCloseHandle", "ptr", $hConnect)
      EndIf
      DllCall($hWininet, "int", "InternetCloseHandle", "ptr", $hInternet)
   EndIf
   DllClose($hWininet)
   Return $nRet
EndFunc

$nRetCode = _FtpGetFile("11.22.33.44", _
                        "myusername", _
                        "mypassword", _
                        "public_html/folder/file.txt", _
                        "c:\file.txt", _
                        "FALSE", _
                        "", _
                        $FTP_TRANSFER_TYPE_ASCII, 0)
                        if $nRetCode <> -1 Then
                            msgbox(4112,"FtpGetFile test","Download of file was NOT successful")
                        Else
                            msgbox(4160,"FtpGetFile test","Download of file WAS successful")
                        EndIf
Edited by egalvez
Link to comment
Share on other sites

heres some code that can download from my ftp server.

(also a nice sneak preview of the new _FTPOpen func :whistle:)

CODE
#region _FTPOpen

;===============================================================================

;

; Function Name: _FTPOpen()

;

; Description: Connects to an FTP server.

;

; Parameter(s): (InternetConnect)

; $s_ServerName - Server name/ip.

; $s_Username - Username.

; $s_Password - Password.

; $i_ServerPort - Server port ( 0 is default (21) )

; $l_Service - Type of service to access.

; $l_Flags - Special flags.

; $l_Context - I dont got a clue what this does.

;

; (InternetOpen)

; $s_Agent - Random name. ( like "myftp" )

; $l_AccessType - I dont got a clue what this does.

; $s_ProxyName - ProxyName.

; $s_ProxyBypass - ProxyByPasses's.

; $l_Flags2 - Special flags.

;

; Requirement(s): DllCall, wininet.dll

;

; Return Value(s): On Success - Returns 2 indentifier's.

; On Failure - 0 and sets @ERROR to -1

;

; Author(s): Wouter van Kesteren

;

;===============================================================================

Func _FTPOpen($s_ServerName, $s_Username, $s_Password, $i_ServerPort = 0, $l_Service = 1, $l_Flags = 0, $l_Context = 0, $s_Agent = 0, $l_AccessType = 1, $s_ProxyName = '', $s_ProxyBypass = '', $l_Flags2 = 0)

If $s_Agent = 0 Then $s_Agent = Random(1, 99999, 1) & Random(1, 99999, 1)

Local $ai_InternetOpen = DllCall('wininet.dll', 'long', 'InternetOpen', 'str', $s_Agent, 'long', $l_AccessType, 'str', $s_ProxyName, 'str', $s_ProxyBypass, 'long', $l_Flags2)

If @error Or $ai_InternetOpen[0] = 0 Then

SetError(1)

Return 0

EndIf

Local $ai_InternetConnect = DllCall('wininet.dll', 'long', 'InternetConnect', 'long', $ai_InternetOpen[0], 'str', $s_ServerName, 'int', $i_ServerPort, 'str', $s_Username, 'str', $s_Password, 'long', $l_Service, 'long', $l_Flags, 'long', $l_Context)

If @error Or $ai_InternetConnect[0] = 0 Then

SetError(1)

Return 0

EndIf

Local $ai_Return[2] = [$ai_InternetOpen[0], $ai_InternetConnect[0]]

Return $ai_Return

EndFunc ;==> _FTPOpen()

#endregion

#region _FTPGetFile

Func _FTPGetFile($l_FTPSession, $s_RemoteFile, $s_LocalFile, $i_overwrite = False, $l_Flags_a_a = 0, $l_Flags = 0, $l_Context = 0)

#cs

BOOL FtpGetFile(

HINTERNET hConnect,

LPCTSTR lpszRemoteFile,

LPCTSTR lpszNewFile,

BOOL fFailIfExists,

DWORD dwFlagsAndAttributes,

DWORD dwFlags,

DWORD_PTR dwContext

);

#ce

$l_FTPSession = $l_FTPSession[1]

Local $ai_FTPPutFile = DllCall('wininet.dll', 'int', 'FtpGetFile', 'long', $l_FTPSession, 'str', $s_RemoteFile, 'str', $s_LocalFile, 'int', $i_overwrite, 'long', $l_Flags_a_a, 'long', $l_Flags, 'long', $l_Context)

If @error Or $ai_FTPPutFile[0] = 0 Then

SetError(1)

Return 0

EndIf

Return $ai_FTPPutFile[0]

EndFunc ;==> _FTPGetFile()

#endregion

#region _FTPClose

;===============================================================================

;

; Function Name: _FTPClose()

; Description: Closes the _FTPOpen session.

; Parameter(s): $l_InternetSession - The Array from _FTPOpen()

; Requirement(s): DllCall, wininet.dll

; Return Value(s): On Success - 1

; On Failure - Returns 0 and sets @error

; Author(s): Wouter van Kesteren

;

;===============================================================================

Func _FTPClose($l_InternetSession)

Local $ai_InternetCloseHandle = DllCall('wininet.dll', 'int', 'InternetCloseHandle', 'long', $l_InternetSession[0])

If @error Or $ai_InternetCloseHandle[0] = 0 Then

SetError(1)

Return 0

EndIf

Return 1

EndFunc ;==> _FTPClose()

#endregion

$server = '127.0.0.1'

$username = 'w0uter'

$pass = 'some pass'

$conn = _FTPOpen($server, $username, $pass)

ConsoleWrite(_FTPGetFile($conn, 'autoit-v3-setup.exe', 'C:\ftpget.exe') & @LF)

_FTPClose($conn)

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Link to comment
Share on other sites

Thank you for the code. Unfortunately, when I tried it out, I got this error:

Line 47  (File "C:\...\ftpOpen.au3"):

Func _FTPGetFile($l_FTPSession, $s_RemoteFile, $s_LocalFile, $i_overwrite = False, $l_Flags_a_a = 0, $l_Flags = 0, $l_Context = 0)

Error: Badly formatted "Func" statement.

Did I copy and paste that line wrongly somehow?

Link to comment
Share on other sites

OK, I changed False to 0. Now I get a different error:

Local $ai_Return[2]= [$ai_InternetOpen[0], $ai_InternetConnect[0]]

Local $ai_Return[2] ^ ERROR

Error: No variable given for "Dim", "Local", "Global" or "Const" statement.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...