oracle 怎么随机生成密码

2020-08-31 科技 114阅读
第一步:在数据库中创建用来管理密码的表。
举例:生成Linux中oracle、grid用户随机密码
CREATE TABLE HOST_TAB (HOSTID NUMBER, HOSTNAME VARCHAR2(100), DESCRIPTION VARCHAR2(2000)); --创建业务系统列表
CREATE TABLE PASSWORD_TAB (HOSTID NUMBER,ORACLE_PASSWORD VARCHAR2(100),GRID_PASSWORD VARCHAR2(100),SHELL_TEXT VARCHAR2(100),CHANGE_DATE DATE) ;--创建密码维护表
第二步:创建可以随机生成密码的函数。
CREATE OR REPLACE FUNCTION random_password(password_num in varchar2)
RETURN VARCHAR2
PARALLEL_ENABLE is
optx char(1);
rng NUMBER;
n BINARY_INTEGER;
ccs VARCHAR2(128); -- candidate character subset
xstr VARCHAR2(4000);
BEGIN
FOR i IN 1 .. length(password_num) LOOP
/* Get random integer within specified range */
n := TRUNC(rng * dbms_random.value) + 1;
/* Append character to random_password2 */
xstr := xstr || SUBSTR(ccs, n, 1);
optx := SUBSTR(password_num, I, 1);
IF optx = 'u' THEN
-- upper case alpha characters only
ccs := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
rng := 26;
ELSIF optx = 'l' THEN
-- lower case alpha characters only
ccs := 'abcdefghijklmnopqrstuvwxyz';
rng := 26;
ELSIF optx = 'a' THEN
-- alpha characters only (mixed case)
ccs := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' || 'abcdefghijklmnopqrstuvwxyz';
rng := 52;
ELSIF optx = 'n' THEN
-- any numeric characters (upper)
ccs := '0123456789';
rng := 10;
ELSIF optx = 'x' THEN
-- any special characters (upper)
ccs := ' !"#$%&()*+,-./:;<=>?@';
rng := 23;
ELSIF optx = 'p' THEN
-- any printable char (ASCII subset)
ccs := ' !"#$%&''()*+,-./' || '0123456789' || ':;<=>?@' ||
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' || '[\]^_`' ||
'abcdefghijklmnopqrstuvwxyz' || '{|}~';
rng := 95;
ELSE
ccs := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
rng := 26; -- default to upper case
END IF;
END LOOP;
RETURN xstr;
END random_password;
第三步:使用存储过程来实现。并且创建数据库调度任务定期修改密码。
create or replace procedure change_passwords(host_name varchar2) as
cursor v_cur is
select t1.hostid from host_tab t1 where t1.hostname = lower(host_name);
begin
for v_i in v_cur loop
begin
insert into password_tab
(hostid, oracle_password, grid_password, shell_text, change_date)
values
(v_i.hostid,
'',
'',
'echo oracle:' || RANDOM_PASSWORD(‘ulaxpnlaxpnl’) ||
'|chpasswd && echo grid:' || RANDOM_PASSWORD('ulaxpnlaxpnl') ||
'|chpasswd',
sysdate);
commit;
end;
end loop;
update password_tab t
set t.oracle_password = substr(t.shell_text, 13, 11),
t.grid_password = substr(t.shell_text, 47, 11);
commit;
end;
使用:
execute change_passwords(hostname=>’test’); test 为host_tab 表中hostname。
select t2.hostname,
t2.description,
t1.oracle_password,
t1.grid_password,
t1.shell_text,
t1.change_date
from password_tab t1
left join host_tab t2
on t1.hostid = t2.hostid;
最后在root用户下执行shell_text中的命令。
也可以通过oracle 的dbms scheduler job 和 crontab来自动实现。
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com