FTP
Dealing with FTP is something needed in many cases, Let's see how easy is that in Ruby with AIO example.

FTP Client

1
require 'net/ftp'
2
3
ftp = Net::FTP.new('rubyfu.net', 'admin', '[email protected]') # Create New FTP connection
4
ftp.welcome # The server's welcome message
5
ftp.system # Get system information
6
ftp.chdir 'go/to/another/path' # Change directory
7
file.pwd # Get the correct directory
8
ftp.list('*') # or ftp.ls, List all files and folders
9
ftp.mkdir 'rubyfu_backup' # Create directory
10
ftp.size 'src.png' # Get file size
11
ftp.get 'src.png', 'dst.png', 1024 # Download file
12
ftp.put 'file1.pdf', 'file1.pdf' # Upload file
13
ftp.rename 'file1.pdf', 'file2.pdf' # Rename file
14
ftp.delete 'file3.pdf' # Delete file
15
ftp.quit # Exit the FTP session
16
ftp.closed? # Is the connection closed?
17
ftp.close # Close the connection
Copied!
Yep, it's simple as that, easy and familiar.
TIP: You can do it all above way using pure socket library, it's really easy. You may try to do it.

FTP Server

    Install ftpd gem
    1
    gem install ftpd
    Copied!
1
#
2
# Pure Ruby FTP server
3
# KING SABRI | @KINGSABRI
4
#
5
require 'ftpd'
6
7
class Driver
8
attr_accessor :path, :user, :pass
9
def initialize(path)
10
@path = path
11
end
12
13
def authenticate(user, password)
14
true
15
end
16
17
def file_system(user)
18
Ftpd::DiskFileSystem.new(@path)
19
end
20
21
end
22
23
class FTPevil
24
25
def initialize(path=".")
26
@driver = Driver.new(File.expand_path(path))
27
@server = Ftpd::FtpServer.new(@driver)
28
configure_server
29
print_connection_info
30
end
31
32
def configure_server
33
@server.server_name = "Rubyfu FTP Server"
34
@server.interface = "0.0.0.0"
35
@server.port = 21
36
end
37
38
def print_connection_info
39
puts "[+] Servername: #{@server.server_name}"
40
puts "[+] Interface: #{@server.interface}"
41
puts "[+] Port: #{@server.port}"
42
puts "[+] Directory: #{@driver.path}"
43
puts "[+] User: #{@driver.user}"
44
puts "[+] Pass: #{@driver.pass}"
45
puts "[+] PID: #{$}"
46
end
47
48
def start
49
@server.start
50
puts "[+] FTP server started. (Press CRL+C to stop it)"
51
$stdout.flush
52
begin
53
loop{}
54
rescue Interrupt
55
puts "\n[+] Closing FTP server."
56
end
57
end
58
end
59
60
if ARGV.size >= 1
61
path = ARGV[0]
62
else
63
puts "[!] ruby #{__FILE__} <PATH>"
64
exit
65
end
66
67
FTPevil.new(path).start
Copied!
Run it
1
ruby ftpd.rb .
2
3
Interface: 0.0.0.0
4
Port: 21
5
Directory: /tmp/ftp-share
6
User:
7
Pass:
8
PID: 2366
9
[+] FTP server started. (Press CRL+C to stop it)
Copied!
Copy link