FunSftp
FunSftp is a ruby gem that provides a nice and easy to use wrapper for the Net::SFTP library.
Reference: net-sftp
Installation
gem 'fun_sftp'
Usage
Let's say you need to send a file or directory to a remote host. Well, you can upload it to the remote host via the following:
Setup the connection and specify a source
directory if desired:
include FunSftp
conn = SFTPClient.new(server, username, password)
conn = SFTPClient.new(server, username, password, keys: [/path/to/key], host_key: 'ssh-rsa')
conn.source = 'projects/awesome_project' #optional
Changing directories chdir
and listing all files ll
can be useful when working in a console. FunSftp now allows you to change your location on the remote host. This saves you from constantly entering long absolute paths.
conn.chdir("projects")
conn.ll
conn.pwd
If you need to start from scratch then reset to the base path:
conn.reset_path!
Now, you can upload your file:
conn.upload!("my_local_file_name.txt", "some_remote_directory/give_a_name.txt")
or directory
conn.upload!("my_awesome_local_directory", "desired_remote_directory_name")
That's it! Check the remote host to see that your file posted.
You can also download a remote file to your local machine:
conn.download!("some_remote_directory/file_to_download_name.txt", "desired_local_name.txt")
or directory
conn.download!("some_remote_directory", "desired_local_directory_name")
Need to read a file on the remote host? Ah, it's no biggie...
conn.read("path/to/file_name.txt")
It is also possible to read the attributes size, access time and modified time.
conn.size("path/to/file_name.txt")
conn.atime("path/to/file_name.txt")
conn.mtime("path/to/file_name.txt")
When investigating items on the remote host, these commands can be handy:
conn.has_directory?("directory_name")
conn.entries("directory_name")
conn.items_in("directory_name")
conn.print_directory_items("directory_name")
conn.glob("directory_name", "**/*.rb")
FileUtils
FileUtilities are handy for doing some dirty work on the remote host.
So far, you can make/remove directories, remove/rename files
conn.mkdir!("new_directory_name_here")
Removing a directory is the same as above except you would swap mkdir! for rmdir!:
conn.rmdir!("directory_name_to_remove")
Files can be removed and renamed off the remote host:
conn.rm("file.txt")
conn.rename("old_file_name.txt", "new_file_name.txt")
Hopefully, this is easy enough to work with and transfer files!!
Logging
By default, logging is turned on and will use the Rails logger if within a Rails projects. If not logging will defer to STDOUT. Logging is prominent when downloading or uploading. To turn off logging, you can create an initializer and modify the configuration by:
FunSftp.configure do |config|
config.log = false
end
You can also define your own logger if you'd like.
FunSftp.configure do |config|
config.logger = MyAwesomeLogger.new
end
Contribute
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
License
Copyright (c) 2017 George Diaz
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.