require 'net/ftp'
# The scanDir function searches through the directory that is passed in as an argument. If that directory has any sub directories,
# then the function calls itself to handle them.
def scanDir(ftp, dir)
ftp.chdir(dir)
puts ftp.pwd + "/."
entries = ftp.list('*')
entries.each do |entry|
if entry.split[2] == "<dir>" then
# If this is a directory then call scanDir to scan it.
scanDir(ftp, entry.split.last)
else
# else, print out the name of the file.
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 "FTPSCAN:"
puts " The ftpscan script recursively scans an ftp directory and returns a list of all the files and directories contained therein."
puts
puts " USAGE:"
puts " ruby ftpscan.rb [ftpserver] [username] [password]"
puts
puts " EXAMPLE:"
puts " ruby ftpscan.rb tsftp.turner.com steveo stevepass"
puts
else
# Get the command line arguments
server, user, pass = ARGV
# Connect to the FTP Server
ftp = Net::FTP.new(server)
# Login to the FTP account
ftp.login(user, pass)
# Recursively scan the directories.
scanDir(ftp, '')
# Close the FTP connection.
ftp.close
end
Enjoy!
No comments:
Post a Comment