以编程方式更改过期的 Oracle 数据库密码?

发布于 2024-10-17 21:38:03 字数 183 浏览 1 评论 0原文

在我工作的地方,一些数据库会从生产环境复制到测试环境,但 DBA 在复制后立即将(新)测试数据库上的所有密码设置为过期,以便不知道生产密码。因此,如果我运行 sqlplus 并使用特定用户名连接到测试数据库,它会立即提示我输入新密码。

有没有办法通过java应用程序或shell脚本自动更改特定用户的过期oracle 10g数据库密码?

Where I work, some databases get copied down from our production environment to our test environment, but the DBAs set all the passwords to expired on the (new) test DB right after the copy so that the production passwords are not known. So if I run sqlplus and connect to the test db with a specific username it immediately prompts me for a new password.

Is there a way via a java app or shell scripting to automate the changing of an expired oracle 10g database password for a specific user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此生挚爱伱 2024-10-24 21:38:03

在 Unix 上

如果您使用的是 UNIX,则可以使用 shell 脚本。

我已经像这样测试了它:

drop user foo cascade;
create user foo identified by old_password password expire;
grant create session to foo;
exit

现在使用这个小 scriptlet:

cat <<DOG | sqlplus foo/old_password
    new_password
    new_password
    exit
DOG

并使用 scriptlet 更改密码

sh change_expired_password.sh

然后,密码被更改并且可以连接到实例:

sqlplus foo/new_password@ORCL

显然,您将把 cat ... 在适合您的 shell 脚本中构造。

在 Windows 上

在 Windows 上,您可以使用像这样的批处理文件

@(
  echo new_password
  echo new_password
) | sqlplus foo/old_password@ORCL

On Unix

If you're on unix, you can do with a shell script.

I have tested it like so:

drop user foo cascade;
create user foo identified by old_password password expire;
grant create session to foo;
exit

Now use this little scriptlet:

cat <<DOG | sqlplus foo/old_password
    new_password
    new_password
    exit
DOG

and change the password with the scriptlet

sh change_expired_password.sh

Then, the password is changed and it can be connected to the instance:

sqlplus foo/new_password@ORCL

Obviously, you would put the cat ... construct in a shell script that suits you.

On Windows

On Windows you could go with a batch file like so

@(
  echo new_password
  echo new_password
) | sqlplus foo/old_password@ORCL
a√萤火虫的光℡ 2024-10-24 21:38:03

我假设您遇到的问题是ORA-28001:密码已过期。这通常是 ALTER USER unittest PASSWORD EXPIRE 的结果,或者只是由于用户的配置文件设置而导致密码过期。这是一个相当棘手的情况。

当您使用 SQL*Plus 连接时,它会提示您输入新密码,然后像这样设置新密码:

jxa@ub16a|2014$ sqlplus unittest/unittest@//localhost/orclpdb1

SQL*Plus: Release 12.1.0.2.0 Production on Fri Dec 22 13:06:04 2017
Copyright (c) 1982, 2014, Oracle.  All rights reserved.

ERROR:
ORA-28001: the password has expired

Changing password for unittest
New password: 
Retype new password: 
Password changed

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
UNITTEST@ORCLCDB> 

但是对于 JDBC 或 cx_Oracle,此异常会导致连接失效,您无法使用它发出ALTER USER xx IDENTIFIED BY yy REPLACE zz

因此,您需要做的就是断开现有连接,然后通过神奇设置再次重新连接。 JDBC 的神奇之处在于将 OCINewPassword 连接属性设置为新密码。只需谷歌搜索 OCINewPassword 即可找到示例。

对于 Python,cx_Oracle.connectcx_Oracle.Connection 都有 newpassword= 参数,允许在报告当前密码过期时以编程方式更改密码。

在这两种情况下,使用旧密码进行连接并设置 OCINewPassword (JDBC) 或 newpassword (Python) 就足够了。其结果是连接正常工作并且用户将密码更改为新密码。

这样,它就可以为过期的 Oracle 用户自动更改密码。

I assume the problem you have is with ORA-28001: the password has expired. It is usually an effect of ALTER USER unittest PASSWORD EXPIRE or simply of the password expired because of the user's profile setting. This is rather tricky situation.

When you connect with SQL*Plus it offers you a prompt asking for the new password and then sets the new passwod like this:

jxa@ub16a|2014$ sqlplus unittest/unittest@//localhost/orclpdb1

SQL*Plus: Release 12.1.0.2.0 Production on Fri Dec 22 13:06:04 2017
Copyright (c) 1982, 2014, Oracle.  All rights reserved.

ERROR:
ORA-28001: the password has expired

Changing password for unittest
New password: 
Retype new password: 
Password changed

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
UNITTEST@ORCLCDB> 

But with JDBC or cx_Oracle this exception leaves you with dead connection and you cannot use it to issue ALTER USER xx IDENTIFIED BY yy REPLACE zz.

So what you need to do is drop the existing connection and reconnect once again with a magic setting. The magic for JDBC is setting OCINewPassword connection property to the new password. Just google for the OCINewPassword and it gets you to examples.

With Python's both cx_Oracle.connect and cx_Oracle.Connection have newpassword= argument which allows to change programmatically the password when the current one is reported expired.

In both cases it is enough to connect with the old password and setting the OCINewPassword (JDBC) or newpassword (Python). It results with the working connection and the user having the password changed to the new one.

This way it opens ways to automate your passwords change for expired oracle users.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文