以编程方式更改过期的 Oracle 数据库密码?
在我工作的地方,一些数据库会从生产环境复制到测试环境,但 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Unix 上
如果您使用的是 UNIX,则可以使用 shell 脚本。
我已经像这样测试了它:
现在使用这个小 scriptlet:
并使用 scriptlet 更改密码
然后,密码被更改并且可以连接到实例:
显然,您将把
cat ... 在适合您的 shell 脚本中构造。
在 Windows 上
在 Windows 上,您可以使用像这样的批处理文件
On Unix
If you're on unix, you can do with a shell script.
I have tested it like so:
Now use this little scriptlet:
and change the password with the scriptlet
Then, the password is changed and it can be connected to the instance:
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
我假设您遇到的问题是
ORA-28001:密码已过期
。这通常是 ALTER USER unittest PASSWORD EXPIRE 的结果,或者只是由于用户的配置文件设置而导致密码过期。这是一个相当棘手的情况。当您使用
SQL*Plus
连接时,它会提示您输入新密码,然后像这样设置新密码:但是对于 JDBC 或 cx_Oracle,此异常会导致连接失效,您无法使用它发出
ALTER USER xx IDENTIFIED BY yy REPLACE zz
。因此,您需要做的就是断开现有连接,然后通过神奇设置再次重新连接。 JDBC 的神奇之处在于将
OCINewPassword
连接属性设置为新密码。只需谷歌搜索OCINewPassword
即可找到示例。对于 Python,
cx_Oracle.connect
和cx_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 ofALTER 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: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 theOCINewPassword
and it gets you to examples.With Python's both
cx_Oracle.connect
andcx_Oracle.Connection
havenewpassword=
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) ornewpassword
(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.