您好,登錄后才能下訂單哦!
在Linux中,您可以使用Ruby腳本來管理用戶認證
sudo apt-get update
sudo apt-get install ruby
user_management.rb
的新Ruby腳本文件:touch user_management.rb
user_management.rb
文件并添加以下代碼:#!/usr/bin/env ruby
def add_user(username, password)
system("useradd", "-m", username)
system("echo", "'#{username}:#{password}'", "|", "chpasswd")
puts "User '#{username}' added with password '#{password}'"
end
def delete_user(username)
system("userdel", "-r", username)
puts "User '#{username}' deleted"
end
def change_password(username, new_password)
system("echo", "'#{username}:#{new_password}'", "|", "chpasswd")
puts "Password for user '#{username}' changed to '#{new_password}'"
end
def list_users
users = `cat /etc/passwd | cut -d: -f1`.split("\n")
puts "Users:"
users.each { |user| puts "- #{user}" }
end
action = ARGV[0]
username = ARGV[1]
password = ARGV[2]
case action
when "add"
add_user(username, password)
when "delete"
delete_user(username)
when "change_password"
change_password(username, password)
when "list"
list_users
else
puts "Usage: #{$0} <add|delete|change_password|list> [username] [password]"
end
chmod +x user_management.rb
./user_management.rb add myuser mypassword
要刪除用戶,運行:
./user_management.rb delete myuser
要更改用戶密碼,運行:
./user_management.rb change_password myuser newpassword
要列出所有用戶,運行:
./user_management.rb list
請注意,這個腳本需要root權限才能運行,因此在運行腳本時,您可能需要使用sudo
。同時,確保在生產環境中使用更安全的方法來存儲和傳遞密碼。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。