-
Notifications
You must be signed in to change notification settings - Fork 0
/
sftphy.rb
224 lines (198 loc) · 8.17 KB
/
sftphy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Copyright 2023 Elijah Gordon (NitrixXero) <[email protected]>
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'optparse'
require 'net/sftp'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: ruby t.rb [options]"
opts.on("-u", "--upload FILE", "Upload FILE via SFTP") do |file|
options[:upload_file] = file
end
opts.on("-d", "--download REMOTE_PATH", "Download file from REMOTE_PATH via SFTP") do |remote_path|
options[:download_remote_path] = remote_path
end
opts.on("-m", "--mkdir DIRECTORY_PATH", "Create directory on the SFTP server") do |directory_path|
options[:create_directory] = directory_path
end
opts.on("--rmdir DIRECTORY_PATH", "Remove directory from the SFTP server") do |directory_path|
options[:remove_directory] = directory_path
end
opts.on("--rmfile FILE_PATH", "Remove file from the SFTP server") do |file_path|
options[:remove_file] = file_path
end
opts.on("-q", "--queryperm REMOTE_PATH", "Query permissions of a file/directory on the SFTP server") do |remote_path|
options[:query_permissions] = remote_path
end
opts.on("-c", "--chmod PERMISSIONS", "Change permissions of a file/directory on the SFTP server") do |permissions|
options[:change_permissions] = permissions
end
opts.on("-l", "--list REMOTE_PATH", "List files and directories at REMOTE_PATH on the SFTP server") do |remote_path|
options[:list_remote_path] = remote_path
end
opts.on("-h", "--host HOST", "SFTP host") do |host|
options[:host] = host
end
opts.on("-U", "--username USERNAME", "SFTP username") do |username|
options[:username] = username
end
opts.on("-p", "--password PASSWORD", "SFTP password") do |password|
options[:password] = password
end
opts.on("-r", "--remote REMOTE_PATH", "Remote path on the SFTP server") do |remote_path|
options[:remote_path] = remote_path
end
end.parse!
def upload_file_to_sftp(host, username, password, remote_path, local_path)
begin
Net::SFTP.start(host, username, password: password) do |sftp|
sftp.upload!(local_path, remote_path)
end
puts "File uploaded successfully!"
rescue StandardError => e
puts "Error uploading file: #{e.message}"
end
end
def download_file_from_sftp(host, username, password, remote_path, local_path)
begin
Net::SFTP.start(host, username, password: password) do |sftp|
sftp.download!(remote_path, local_path)
end
puts "File downloaded successfully!"
rescue StandardError => e
puts "Error downloading file: #{e.message}"
end
end
def create_directory_on_sftp(host, username, password, remote_path)
begin
Net::SFTP.start(host, username, password: password) do |sftp|
sftp.mkdir!(remote_path)
end
puts "Directory created successfully!"
rescue StandardError => e
puts "Error creating directory: #{e.message}"
end
end
def remove_directory_on_sftp(host, username, password, remote_path)
begin
Net::SFTP.start(host, username, password: password) do |sftp|
sftp.rmdir!(remote_path)
end
puts "Directory removed successfully!"
rescue StandardError => e
puts "Error removing directory: #{e.message}"
end
end
def remove_file_on_sftp(host, username, password, remote_path)
begin
Net::SFTP.start(host, username, password: password) do |sftp|
sftp.remove!(remote_path)
end
puts "File removed successfully!"
rescue StandardError => e
puts "Error removing file: #{e.message}"
end
end
def query_permissions_on_sftp(host, username, password, remote_path)
begin
permissions = nil
Net::SFTP.start(host, username, password: password) do |sftp|
attributes = sftp.stat!(remote_path)
permissions = attributes.permissions.to_s(8)[-3, 3]
end
puts "Permissions for #{remote_path}: #{permissions}"
rescue StandardError => e
puts "Error querying permissions: #{e.message}"
end
end
def change_permissions_on_sftp(host, username, password, remote_path, permissions)
begin
Net::SFTP.start(host, username, password: password) do |sftp|
sftp.setstat(remote_path, permissions: permissions.to_i(8))
end
puts "Permissions for #{remote_path} changed to: #{permissions}"
rescue StandardError => e
puts "Error changing permissions: #{e.message}"
end
end
def list_files_and_directories_on_sftp(host, username, password, remote_path)
begin
files = []
directories = []
Net::SFTP.start(host, username, password: password) do |sftp|
sftp.dir.foreach(remote_path) do |entry|
if entry.file?
files << entry.name
elsif entry.directory?
directories << entry.name
end
end
end
puts "Files at #{remote_path}:"
puts files.join("\n")
puts "\nDirectories at #{remote_path}:"
puts directories.join("\n")
rescue StandardError => e
puts "Error listing files and directories: #{e.message}"
end
end
if options[:upload_file]
if options[:host] && options[:username] && options[:password] && options[:remote_path]
upload_file_to_sftp(options[:host], options[:username], options[:password], options[:remote_path], options[:upload_file])
else
puts "Please provide all the required options for uploading."
end
elsif options[:download_remote_path]
if options[:host] && options[:username] && options[:password] && options[:remote_path]
download_file_from_sftp(options[:host], options[:username], options[:password], options[:download_remote_path], options[:download_remote_path].split('/').last)
else
puts "Please provide all the required options for downloading."
end
elsif options[:create_directory]
if options[:host] && options[:username] && options[:password] && options[:create_directory]
create_directory_on_sftp(options[:host], options[:username], options[:password], options[:create_directory])
else
puts "Please provide all the required options for creating a directory."
end
elsif options[:remove_directory]
if options[:host] && options[:username] && options[:password] && options[:remove_directory]
remove_directory_on_sftp(options[:host], options[:username], options[:password], options[:remove_directory])
else
puts "Please provide all the required options for removing a directory."
end
elsif options[:remove_file]
if options[:host] && options[:username] && options[:password] && options[:remove_file]
remove_file_on_sftp(options[:host], options[:username], options[:password], options[:remove_file])
else
puts "Please provide all the required options for removing a file."
end
elsif options[:query_permissions]
if options[:host] && options[:username] && options[:password] && options[:query_permissions]
query_permissions_on_sftp(options[:host], options[:username], options[:password], options[:query_permissions])
else
puts "Please provide all the required options for querying permissions."
end
elsif options[:change_permissions]
if options[:host] && options[:username] && options[:password] && options[:remote_path] && options[:change_permissions]
change_permissions_on_sftp(options[:host], options[:username], options[:password], options[:remote_path], options[:change_permissions])
else
puts "Please provide all the required options for changing permissions."
end
elsif options[:list_remote_path]
if options[:host] && options[:username] && options[:password] && options[:list_remote_path]
list_files_and_directories_on_sftp(options[:host], options[:username], options[:password], options[:list_remote_path])
else
puts "Please provide all the required options for listing files and directories."
end
else
puts "Usage: ruby sftphy.rb [--help]"
end