简体   繁体   English

带有 sqlplus 和密码特殊字符的 Shell 脚本

[英]Shell Script with sqlplus and special characters on password

I have a question that mix Linux / Unix shell-scripting and sqlplus (Oracle) that is driving me crazy.我有一个混合 Linux / Unix shell 脚本和 sqlplus (Oracle) 的问题,这让我发疯。 :-) :-)

sqlplus utilize a syntax like this: sqlplus 使用这样的语法:

./sqlplus johnF/mypassword@127.0.0.1:1521/SID

And it works fine.它工作正常。 However my password is not simple as "mypassword", it utilize "!"但是我的密码并不像“mypassword”那么简单,它使用“!” and "@" and sometimes even "\\".和“@”,有时甚至是“\\”。 For this example, let's suppose that my password is !p@ssword在这个例子中,假设我的密码是 !p@ssword

If I use the following syntax in sqlplus it works:如果我在 sqlplus 中使用以下语法,它会起作用:

./sqlplus johnF/'"!p@ssword"'@127.0.0.1:1521/SID

That's great.那太好了。 However I wanted to use it in a shell script that get call sqlplus and get many parameters from files (username, password, SID and SQL QUERY), just for example let me use a reduced code.但是,我想在 shell 脚本中使用它来调用 sqlplus 并从文件中获取许多参数(用户名、密码、SID 和 SQL 查询),例如让我使用简化的代码。

#!/bin/bash

while IFS=: read -r line
do

        echo "./sqlplus johnF/$line@127.0.0.1:1521/SID" 
        echo -e 'select 1 from dual;\nexit;' |  ./sqlplus johnF/$line@127.0.0.1:1521/SID

done < $1

I have attempted to fix it in many ways, including:我尝试通过多种方式修复它,包括:

echo -e 'select 1 from dual;\nexit;' |  ./sqlplus johnF/'"$line"'@127.0.0.1:1521/SID
echo -e 'select 1 from dual;\nexit;' |  ./sqlplus johnF/'\"$line\"'@127.0.0.1:1521/SID
echo -e 'select 1 from dual;\nexit;' |  ./sqlplus johnF/\'\"$line\"\'@127.0.0.1:1521/SID

And many others and all fails, in a few cases the first echo print the output exactly as it should be passed to sqlplus, but it never works, returns login denied (wrong password) or connection issues (maybe the @ being intercepted as wrong target).和许多其他人一样都失败了,在少数情况下,第一个 echo 完全按照应传递给 sqlplus 的方式打印输出,但它永远不会工作,返回登录被拒绝(密码错误)或连接问题(也许 @ 被拦截为错误的目标)。

How to solve this puzzle?这个谜题怎么解?

Thanks.谢谢。

Configure the config file sqlnet.ora for an easy connection.配置配置文件sqlnet.ora以方便连接。

NAMES.DIRECTORY_PATH= (TNSNAMES,ezconnect)

Change the password @T!ger to the user "Scott".将密码@T!ger 更改为用户“Scott”。

oracle@esmd:~>
oracle@esmd:~> sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Mon Jan 29 11:05:04 2018

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production

SQL> alter user "Scott" identified by "@T!ger";

User altered.

Example 1 Script is test_echo.sh示例 1脚本为 test_echo.sh

    #!/bin/sh

    username=\"Scott\"
    password=\"@T!ger\"
    ezconnect=10.89.251.205:1521/esmd

    echo username:  $username
    echo password:  $password
    echo ezconnect  $ezconnect

 echo -e 'show user \n  select 1 from dual;\nexit;' |  sqlplus  $username/$password@$ezconnect

oracle@esmd:~> ./test_echo.sh
username: "Scott"
password: "@T!ger"
ezconnect 10.89.251.205:1521/esmd

SQL*Plus: Release 11.2.0.3.0 Production on Mon Jan 29 11:02:52 2018

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production

SQL> USER is "Scott"
SQL>
         1
----------
         1

SQL> Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production

Example 2 Run script test_echo.sh in silent mode sqlplus示例 2以静默模式 sqlplus 运行脚本 test_echo.sh

#!/bin/sh

username=\"Scott\"
password=\"@T!ger\"
ezconnect=10.89.251.205:1521/esmd

echo username:  $username
echo password:  $password
echo ezconnect  $ezconnect
echo -e 'show user \n  select 1 from dual;\nexit;' |  sqlplus -s  $username/$password@$ezconnect

oracle@esmd:~> oracle@esmd:~> ./test_echo.sh
username: "Scott"
password: "@T!ger"
ezconnect 10.89.251.205:1521/esmd
USER is "Scott"

         1
----------
         1

Example 3 A little bit Another syntax示例 3一点点另一种语法

#!/bin/sh

username=\"Scott\"
password=\"@T!ger\"
ezconnect=10.89.251.205:1521/esmd


echo username:  $username
echo password:  $password
echo ezconnect: $ezconnect

testoutput=$(sqlplus -s $username/$password@$ezconnect  << EOF
set pagesize 0 feedback off verify off heading off echo off;
show user
SELECT to_char(sysdate,'DD-MM-YYYY HH24:MI')||' Test passed' from dual
exit;
EOF
)

echo $testoutput

oracle@esmd:~> ./test_Upper_case.sh
username: "Scott"
password: "@T!ger"
ezconnect: 10.89.251.205:1521/esmd
USER is "Scott" 29-01-2018 11:55 Test passed

i assume you issued this to alter your user's password :我假设你发出这个来改变你的用户密码:

alter user johnF identified by "!p@ssword";  

since因为

alter user johnF identified by !p@ssword;  

doesn't conforms oracle password definition rules.不符合oracle密码定义规则。

and then it's enough to write such a script in your file to connect your schema :然后在您的文件中编写这样一个脚本来连接您的架构就足够了:

#!/bin/bash
# cnn.sh
line '"!p@ssword"'
echo line
sqlplus johnF/$line@127.0.0.1:1521/yourSID

and call from prompt :并从提示调用:

$ . cnn.sh

I've encountered the same problem here as well (which really drives me crazy), and this is my answer.我在这里也遇到了同样的问题(这真的让我发疯了),这就是我的答案。

All the special characters allowed in Oracle could be found on this page: https://docs.oracle.com/cd/E11223_01/doc.910/e11197/app_special_char.htm#MCMAD416 Oracle 中允许的所有特殊字符都可以在此页面上找到: https : //docs.oracle.com/cd/E11223_01/doc.910/e11197/app_special_char.htm#MCMAD416

  1. If your Oracle password contains any of the special characters on the above page except for a single quotation mark.如果您的 Oracle 密码包含上述页面中除单引号外的任何特殊字符。 #o@%+!$(){}[]/^?:`~ for example. #o@%+!$(){}[]/^?:`~ 例如。

You can use it like this:你可以这样使用它:

sh test.sh oracle_user '"#o@%+!$(){}[]/\^?:`~"' ip port service_name
  1. If your Oracle password contains a single quotation mark.如果您的 Oracle 密码包含单引号。 #o@%+!$(){}[]/^?:`~' for example. #o@%+!$(){}[]/^?:`~' 例如。

You can use it like this:你可以这样使用它:

sh test.sh oracle_user '"#o@%+!$(){}[]/\^?:`~'"'"'"' ip port service_name

Please be noted that the single quotation mark ' should be replaced with '""'.请注意,单引号 ' 应替换为 '""'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Bash Shell中获取并使用带有特殊字符的密码 - Get and use a password with special characters in Bash shell 在shell脚本中grepping包含特殊字符的变量 - grepping variables containing special characters in a shell script Shell脚本以递归方式重命名带有特殊字符的文件名转义? - Shell script to recursively rename filenames with special characters escaped? 如何处理 shell 脚本的命令行参数中的特殊字符 - How to handle special characters in command line arguments for a shell script 使用 printf 时如何转义 shell 脚本中的特殊字符? - While using printf how to escape special characters in shell script? 如何传递带有特殊字符的参数来调用shell脚本 - How to pass arguments with special characters to call shell script 在shell脚本中有没有分隔符的接受特殊字符的方法吗? - Any way to accept special characters without delimiters in a shell script? 将带有特殊字符的变量作为密码传递会破坏bash脚本。 如何清除bash中的特殊字符? - Passing Variable with Special characters as password breaks bash script. How do I Sanitize special characters in bash? mongodb shell 登录 - 密码包含特殊字符,如 -(连字符)和 '(单引号) - mongodb shell login - password contains special characters like -(hyphen) and '(single quote) bash脚本中的特殊字符 - special characters in bash script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM