Last active
December 3, 2016 06:29
-
-
Save inokappa/4d102f40fc3b1f8331a5658de6e51e3e to your computer and use it in GitHub Desktop.
作成した FTP ユーザーが正しく FTP 操作を行えるかを確認するスクリプト的な何か
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/ruby | |
| # -*- coding: utf-8 -*- | |
| require "net/ftp" | |
| require "yaml" | |
| class FtpCheck | |
| def initialize(host, mode) | |
| @ftp = Net::FTP.new | |
| @ftp.passive = mode | |
| @host = host | |
| end | |
| # サーバーに接続 | |
| def login(username, password) | |
| begin | |
| @ftp.connect(@host) | |
| @ftp.login(username, password) | |
| puts "check: FTP サーバーに接続 [ " + "\e[32m" + "ok" + "\e[0m" + " ]" | |
| rescue Net::FTPError => e | |
| puts "check: FTP サーバーに接続 + #{e}" | |
| end | |
| end | |
| # カレントディレクトリを表示 | |
| def check_current_dir | |
| puts @ftp.pwd | |
| puts "check: カレントディレクトリを表示 [ " + "\e[32m" + "ok" + "\e[0m" + " ]" | |
| end | |
| # ディレクトリを移動 | |
| def check_chdir | |
| begin | |
| @ftp.chdir "../" | |
| puts "check: ディレクトリを移動 [ " + "\e[32m" + "ok" + "\e[0m" + " ]" | |
| rescue Net::FTPError => e | |
| puts "check: ディレクトリを移動 #{e}" | |
| ensure | |
| puts @ftp.pwd | |
| end | |
| end | |
| # ファイルをアップロード | |
| def check_upload | |
| begin | |
| @ftp.put("sample.html", "./test/sample.html") | |
| puts "check: ファイルをアップロード [ " + "\e[32m" + "ok" + "\e[0m" + " ]" | |
| rescue Net::FTPError => e | |
| puts "check: ファイルをアップロード #{e}" | |
| ensure | |
| puts @ftp.ls("./test/*") | |
| end | |
| end | |
| # ファイルの一覧を取得 | |
| def check_ls | |
| begin | |
| puts @ftp.ls("./test/*") | |
| puts "check: ファイルの一覧を取得 [ " + "\e[32m" + "ok" + "\e[0m" + " ]" | |
| rescue Net::FTPError => e | |
| puts "check: ファイルの一覧を取得 #{e}" | |
| ensure | |
| puts @ftp.ls("./*") | |
| end | |
| end | |
| # ファイル名を変更 | |
| def check_move_file_name | |
| begin | |
| @ftp.rename("./test/sample.html", "./test/sample1.html") | |
| puts "check: ファイル名を変更 [ " + "\e[32m" + "ok" + "\e[0m" + " ]" | |
| rescue Net::FTPError => e | |
| puts "check: ファイル名を変更 #{e}" | |
| ensure | |
| puts @ftp.ls("./test/*") | |
| end | |
| end | |
| # ファイルを取得 | |
| def check_get_file | |
| begin | |
| @ftp.get "./test/sample1.html" | |
| puts "check: ファイルを取得 [ " + "\e[32m" + "ok" + "\e[0m" + " ]" | |
| rescue Net::FTPError => e | |
| puts "check: ファイルを取得 #{e}" | |
| ensure | |
| system("ls -l sample1.html") | |
| end | |
| end | |
| # ファイルを削除 | |
| def check_remove_file | |
| begin | |
| @ftp.delete "./test/sample1.html" | |
| puts "check: ファイルを削除 [ " + "\e[32m" + "ok" + "\e[0m" + " ]" | |
| rescue Net::FTPError => e | |
| puts "check: ファイルを削除 #{e}" | |
| ensure | |
| puts @ftp.ls("./test/") | |
| end | |
| end | |
| # ディレクトリを作成 | |
| def check_create_dir | |
| begin | |
| @ftp.mkdir "./test" | |
| puts "check: ディレクトリを作成 [ " + "\e[32m" + "ok" + "\e[0m" + " ]" | |
| rescue Net::FTPError => e | |
| puts "check: ディレクトリを作成 #{e}" | |
| ensure | |
| puts @ftp.ls("./") | |
| end | |
| end | |
| # ディレクトリを削除 | |
| def check_remove_dir | |
| begin | |
| @ftp.rmdir "./test" | |
| puts "check: ディレクトリを削除 [ " + "\e[32m" + "ok" + "\e[0m" + " ]" | |
| rescue Net::FTPError => e | |
| puts "check: ディレクトリを削除 #{e}" | |
| ensure | |
| puts @ftp.ls("./test/") | |
| end | |
| end | |
| # 通信を終了 | |
| def quite | |
| @ftp.quit | |
| puts "check: 通信を終了 [ " + "\e[32m" + "ok" + "\e[0m" + " ]" | |
| end | |
| end | |
| configs = YAML.load_file('access.yml') | |
| configs.each do |c| | |
| check = FtpCheck.new(c['host'], c['mode']) | |
| puts "host: #{c["host"]} ##########################################################" | |
| c["users"].each do |user| | |
| puts "user: #{user["username"]}" | |
| check.login(user["username"], user["password"]) | |
| check.check_current_dir | |
| check.check_chdir | |
| check.check_ls | |
| check.check_create_dir | |
| check.check_upload | |
| check.check_move_file_name | |
| check.check_get_file | |
| check.check_remove_file | |
| check.check_remove_dir | |
| check.quite | |
| puts "------------------------------------------------------------------------------" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment