Recent comments


Syndicate


Follow Me

Twitter Facebook

FTP? nah, more like remote navigator.

I was checking out the ftp library of ruby and since the commands are minimal, I figured it would be fun to create a simple ftp program in one file. Since I'm feeling lazy, it ended-up as a simple remote directory navigator instead.

require 'net/ftp'

commands = ['ls', 'cd', 'quit']
response = ''

Net::FTP.open('ftp.mysite.com', 'ftpusername', 'ftppassword') do |ftp|
  until response == :quit
    print ">> "
    response = gets.chomp
    
    args = response.split(' ')
    response = args[0]

    #check for valid commands
    unless commands.include?(response)
      puts "\nInvalid command"
      puts "Usage: ls, cd , size \n\n"
      next
    end
    
    #if user want to quit
    unless response != 'quit'
      response = :quit
      next
    end
    
    #respond accordingly
    case response
    when 'ls'
      files = ftp.nlst('.')
      unless files.empty?
        files.each {|file| puts file}
      end
    when 'cd'
      unless args[1]
        puts "\nWrong number of arguments"
        puts "Usage: cd \n\n"
        next
      end
      ftp.chdir(args[1])
      puts "Directory changed"
    end
  end
end

I think the code is short and straight forward but I'll explain it roughly. We pass a code block to the open method of the FTP class and inside the code block, we have a loop that keeps on asking for commands. If the user enters quit, we break out of the loop. The nice thing on calling the open method instead of the new is that it closes the ftp connection right after the code block ends.

I might upgrade this someday

I might upgrade this someday to a full pledge FTP program and since I'm into FXRuby, I'll make it fancy by adding a GUI. Hmm, lets see..

Cool. Yeah, more like remote

Cool. Yeah, more like remote navigator.Hmm, I think should start coding and learning new things. This post gives me motivation to do so.

 Nice, I'll look forward

 Nice, I'll look forward too!  Btw, just added you on my links. Cheers!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.