Skip to content

Instantly share code, notes, and snippets.

@sms420
Created April 15, 2010 00:53
Show Gist options
  • Select an option

  • Save sms420/366555 to your computer and use it in GitHub Desktop.

Select an option

Save sms420/366555 to your computer and use it in GitHub Desktop.
ora_acct_mgr.rb
#!/usr//bin/env ruby
# ora_acct_mgr.rb
# Sean Stephenson
# creates and deletes accounts on Oracle
require 'groupFile'
getGroupFile
/**************** do_oracle(sql) ******************/
def do_oracle(sql)
sql.chop! if sql[-1] == ';'
cmd = <<-EOJ
sqlplus -S system/manager <<-EOF
set echo off head off pagesize 0
#{sql};
set echo on head on pagesize 24
quit
EOF
EOJ
result = IO.popen(cmd)
abort("access to Oracle failed\n") unless result
return result.to_a
end
/**************** help ******************/
def help
puts "\tUSAGE
[-m]: calls rcopy -m
[-c]: creates account(s)
[-d]: deletes account(s)
[-h]: help"
end
/**************** callRcopy ******************/
def callRcopy
`/usr/local/bin/rcopy.rb -m`
end
/**************** createAccount(user) ******************/
def createAccount(user)
totalNewUsers = user.length - 1
# puts "create these user(s): "
# (1..totalNewUsers).each do |i|
# puts user[i]
# end
(1..totalNewUsers).each do |createUser|
str = "grant connect to #{user[createUser]} identified by changeme;
alter user #{user[createUser]} profile Lmyprofile;"
puts "***********************\n\ncreate account:: #{user[createUser]}\n\n"
do_oracle(str)
puts "***********************\n"
end
end
/**************** deleteAccount(user) ******************/
def deleteAccount(user)
totalUsersToDelete = user.length - 1
# puts "delete these user(s): "
# (1..totalUsersToDelete).each do |i|
# puts user[i]
# end
(1..totalUsersToDelete).each do |dropUser|
puts "***********************\n\ndelete account: #{user[dropUser]}\n\n"
str =" drop user #{user[dropUser]} cascade;"
do_oracle(str)
puts "***********************\n"
end
end
/**************** deleteAccountsMatchingRegexp(match) ******************/
def deleteAccountsMatchingRegexp(match)
return if ARGV[0] != "-d"
puts "delete accounts matching this regexp: #{match}"
str = "select distinct username from all_users
where regexp_like(username, '#{match}', 'i')"
usersToDelete = do_oracle(str)
usersToDelete.slice!(-1) # removes last item in array (it's empty)
# puts usersToDelete.to_a
(1..totalUsersToDelete).each do |dropUser|
puts "***********************\n\ndelete account: #{match[dropUser]}\n\n"
str =" drop user #{match[dropUser]} cascade;"
do_oracle(str)
puts "***********************\n"
end
end
/**************** isRegexp?(match) ******************/
def isRegexp?(match)
if match =~ /[\^$.|?*+()]/
return true
else
return false
end
end
/** ASSIGN VALUES FROM INPUT **/
user = Array.new
totalParams = ARGV.length - 1 # get just the usernames
(0..totalParams).each do |i|
if isCRN?(ARGV[i])
begin
line = getCRNFromGroupFile(ARGV[i])
userArray = createUserArray(line)
userArray.each do |u|
user.push(u)
end
rescue
puts "no such CRN in group file"
end
else
if isRegexp?(ARGV[i])
deleteAccountsMatchingRegexp(ARGV[i])
else
user.push(ARGV[i])
end
end
end
/** GET INPUT **/
case ARGV[0]
when "-m" then callRcopy
when "-c" then createAccount(user)
# only call when a distinct username is passed, else
# deleting accounts matched with regexes called above
when "-d" then deleteAccount(user) if user[1] != nil
when "-h" then help
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment