Announcement

Collapse
No announcement yet.

Creating basic ftp script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating basic ftp script

    I'm very new (as in the last hour) of trying to create a script that will automatically download a file off a ftp server.

    From what I'm reading you need to do two files. One that starts the process of actually running the ftp file.

    File 1: ftp.bat
    Code:
    ftp -s:C:\ftp1.bat
    That calls my ftp1.bat file and asks it to run....from what I understand.

    File 2: ftp1.bat
    Code:
    open ftp.asdfasdf.com
    username
    password
    mget *.*
    y
    bye
    Now, if I understand this correctly I run my ftp.bat file, which will run the ftp1.bat file. The ftp1.bat file contains the actual information to connect to the ftp server and do what I need it to do. (which is only download 1 file and it's the only file in the directory)

    When I run this, I get a scrolling cmd prompt window of:
    Code:
    ftp -s:C:\ftp1.bat
    And have to do CTRL+C to get out.

    Any suggestions would be very helpful.

    Thanks.

  • #2
    Re: Creating basic ftp script

    From your batch you start: FTP (no path, no extension)
    The seach path for this file will start from within in the current folder before the Path environment variable is searched.
    Because name of your batch happen to be also FTP, the batch will launch itself over and over.

    To solve this you distinguish the two files by including the file extension of ftp.exe:
    ftp.exe -s:"C:\ftp1.bat"


    btw,, The second script (ftp1.bat) dus not realy need the extension "bat" or "cmd", this could be *.ftp or *.ftpcmd or anything else for its extension.


    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Creating basic ftp script

      this is an alternative. If you can use scripting languages like Perl/Python, which have FTP libraries/modules that provide you better control of your FTP process. an example with Perl and Net::FTP module
      Code:
      use Net::FTP;
      my $file_to_get = "file.txt";
      $ftp = Net::FTP->new("127.0.0.1", Debug => 1) or die "Cannot connect to localhost: [email protected]";
      $ftp->login("administrator",'password') or die "Cannot login ", $ftp->message;
      $ftp->cwd("somedir");
      $ftp->get($file_to_get)  or die "get failed ", $ftp->message;
      $ftp->quit;
      no need to use separate files like that with Windows FTP clients.

      Comment


      • #4
        Re: Creating basic ftp script

        Perl is Open Source software you can download it for free - But why should you install and maintain Perl on your computer for just running one FTP script?

        Sample of a consolidated FTP_batch wherein the FTP.exe command line and ftp-cmds are entered:
        Code:
        @echo off
        
        >%temp%.\commands.ftp (
            echo\open ftp.asdfasdf.com
            echo\username
            echo\password
            echo\mget *.*
            echo\y
            echo\bye
        )
        echo\quit >>%temp%.\commands.ftp
        
        ftp.exe -s:"%temp%.\commands.ftp"
        
        del "%temp%.\commands.ftp"
        \Rems
        Last edited by Rems; 14th April 2009, 09:48.

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Creating basic ftp script

          Originally posted by Rems View Post
          But why should you install and maintain Perl on your computer for just running one FTP script?
          because it can do a lot more than just FTP. to a system admin, its an invaluable tool to have. (python as well)

          Comment


          • #6
            Re: Creating basic ftp script

            Originally posted by Rems View Post
            From your batch you start: FTP (no path, no extension)
            The seach path for this file will start from within in the current folder before the Path environment variable is searched.
            Because name of your batch happen to be also FTP, the batch will launch itself over and over.

            To solve this you distinguish the two files by including the file extension of ftp.exe:
            ftp.exe -s:"C:\ftp1.bat"


            btw,, The second script (ftp1.bat) dus not realy need the extension "bat" or "cmd", this could be *.ftp or *.ftpcmd or anything else for its extension.


            \Rems
            This worked beautifully. After all that I just needed the extension. Thanks for the help!

            Comment


            • #7
              Re: Creating basic ftp script

              Originally posted by ghostdog74 View Post
              because it can do a lot more than just FTP. to a system admin, its an invaluable tool to have. (python as well)
              I know that Phyton and Perl are some great tools.
              And once you know the language you can also use it on Linux for example.

              However you can't rule out the old batch files, Vbscripts, and right now even Powershell on Microsoft machines

              A pretty simple Get FTP File function can be found here:
              http://powershell.com/cs/media/p/804.aspx

              You can work it out as you like as it is an example.

              Code:
                
              function Get-FTPFile ($Source,$Target,$UserName,$Password) 
              { 
                   
               # Create a FTPWebRequest object to handle the connection to the ftp server 
               $ftprequest = [System.Net.FtpWebRequest]::create($Source) 
                   
               # set the request's network credentials for an authenticated connection 
               $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 to the server 
                $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 
                $targetfile = New-Object IO.FileStream ($Target,[IO.FileMode]::Create) 
                [byte[]]$readbuffer = New-Object byte[] 1024 
                 
                # loop through the download stream and send the data to the target file 
                do{ 
                     $readlength = $responsestream.Read($readbuffer,0,1024) 
                     $targetfile.Write($readbuffer,0,$readlength) 
                   } 
                 while ($readlength -ne 0) 
                  
                 $targetfile.close() 
              } 
                  
                 $sourceuri = "ftp://MyFtpServer/FolderPath\File.txt" 
                 $targetpath = "C:\temp\MyFile.txt" 
                 $user = "Username" 
                 $pass = "Password" 
                Get-FTPFile $sourceuri $targetpath $user $pass
              The point what I'm trying to make is there is no good or wrong way when scripting.
              Everyone has his or her favorite tools and as long as it does the job then it's good
              Marcel
              Technical Consultant
              Netherlands
              http://www.phetios.com
              http://blog.nessus.nl

              MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
              "No matter how secure, there is always the human factor."

              "Enjoy life today, tomorrow may never come."
              "If you're going through hell, keep going. ~Winston Churchill"

              Comment


              • #8
                Re: Creating basic ftp script

                Originally posted by Dumber View Post

                However you can't rule out the old batch files, Vbscripts, and right now even Powershell on Microsoft machines
                First of all, i want to bring to your attention, my reply to OP is an alternative solution. I am sure there's no rule to forbid me suggesting a viable alternative solution (if OP sees fit to use) right?

                secondly, i don't doubt what Powershell can do as well, after all, its new and i am sure its jam packed with good stuffs that windows sysadmin can use. Reply #5 was posted in response to Rem's query

                >> But why should you install and maintain Perl on your computer for just running one FTP script?

                I am expecting OP to want to do more than FTP in the future.. Maybe next time he's going to do batch telnets or maybe he is going to convert word documents to PDF etc. With such things in mind, using batch/vbscript is shortfall. Having a tool like Perl ( or Powershell (with vast .NET libraries????) with vast libraries (CPAN) to do complex tasks is certainly good to have.

                Comment


                • #9
                  Re: Creating basic ftp script

                  Relax mate. I didn't meant to offend you.
                  I only wanted to make clear that there are many ways to achieve a goal.
                  And there is no good or bad way... unless the script isn't working of course

                  Perl and Phyton are certainly good ways to achieve different goals. Also it's nice that it's platform independent.
                  Marcel
                  Technical Consultant
                  Netherlands
                  http://www.phetios.com
                  http://blog.nessus.nl

                  MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
                  "No matter how secure, there is always the human factor."

                  "Enjoy life today, tomorrow may never come."
                  "If you're going through hell, keep going. ~Winston Churchill"

                  Comment

                  Working...
                  X