Skip to content

Instantly share code, notes, and snippets.

@toddnestor
Forked from anonymous/permission.rb
Last active July 7, 2017 15:54
Show Gist options
  • Save toddnestor/a70a92db54c87474ab6d24a1bbb5576f to your computer and use it in GitHub Desktop.
Save toddnestor/a70a92db54c87474ab6d24a1bbb5576f to your computer and use it in GitHub Desktop.
require 'net/ssh'
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: ruby permission.rb [-h some-host-option -a lock -u garion]"
%i{host user action}.each do |option|
opts.on("-#{option[0]}VALUE", "--#{option}=VALUE") do |value|
options[option] = value
end
end
end.parse!
# get arguments from command line
# decide what to do with them
# and verify they are legit commands
hostname = options[:host]
lock = options[:action]
user = options[:user]
case lock
when "lock"
cmd = "sudo chsh -s /usr/bin/false #{user}"
locker(hostname, cmd)
when "unlock"
cmd = "sudo chsh -s /bin/bash #{user}"
locker(hostname, cmd)
else quit
end
def locker(host, cmd)
begin
ssh = Net::SSH.start(host)
res = ssh.exec!(cmd)
ssh.close
puts res
rescue
puts "Unable to connect to #{@hostname}!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment