Skip to content

Instantly share code, notes, and snippets.

@beepony
Last active January 24, 2023 17:09
Show Gist options
  • Save beepony/f903042e6decdeccc5d6b133a0e6f6ab to your computer and use it in GitHub Desktop.
Save beepony/f903042e6decdeccc5d6b133a0e6f6ab to your computer and use it in GitHub Desktop.
ruby ftp list all files
require 'net/ftp'
def scan(ftp, dir)
ftp.chdir(dir)
puts ftp.pwd + "/."
entries = ftp.list('*')
entries.each do |entry|
if entry.split(/\s+/)[0][0,1] == "d" then
scan(ftp, entry.split.last)
else
puts ftp.pwd + "/" + entry.split.last
end
end
# Since we dipped down a level to scan this directory, lets go back to the parent so we can scan the next directory.
ftp.chdir('..')
end
# Determine if the user is asking for help.
helpArgs = ["h", "-h", "/h", "?", "-?", "/?"]
if helpArgs.index(ARGV[0]) != nil || ARGV.length == 0 then
puts <<-eos
FTPSCAN:
This script recursively scans an ftp directory and returns a list of all the files and directories contained therein.
USAGE:
ruby ftpscan.rb [ftpserver] [username] [password]
EXAMPLE:
ruby ftpscan.rb ftp.example.com trey treypass
eos
else
server, user, pass = ARGV
ftp = Net::FTP.new(server)
ftp.login(user, pass)
scan(ftp, 'http')
ftp.close
end
# thanks for ref:http://zurb.com/forrst/posts/Ruby_Code_to_Recursively_Scan_an_FTP_Site-gSZ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment