将带有特殊字符的变量作为密码传递会破坏bash脚本.如何清除bash中的特殊字符? [英] Passing Variable with Special characters as password breaks bash script. How do I Sanitize special characters in bash?

查看:544
本文介绍了将带有特殊字符的变量作为密码传递会破坏bash脚本.如何清除bash中的特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种解决方案,可以对bash密码更改脚本中的变量进行清理.

I am looking for a solution to sanitizing variables in my bash password change script.

下面的脚本正在运行,但是我发现某些特殊字符"会破坏我的脚本.我现在无法控制正在传递的字符.我正在寻找对变量进行传递之前对其进行清理的方法,或者将这些变量作为一个整体推开.我尝试使用"$ {PASS}"代替"$ {PASS}",但是在这种情况下脚本无法完成.

The script below is working, however I have found that some "Special Characters" will break my script. I am not controlling at the moment what Characters are being passed through. I am either looking to sanitize the variables before passing them through, or pushing the variables as a whole untouched. I have tried using '${PASS}' in place of "${PASS}" however the script would not complete when this was the case.

任何人都可以提出任何建议,我将不胜感激.我已经尝试过在发布之前搜索此问题的答案,但没有找到任何相对的答案,因此,如果在其他地方回答了这个问题,我感到抱歉.

I would appreciate any recommendations anyone could offer. I have tried searching for the answer to this question before posting but didn't find anything relative so i am sorry if this has been answered elsewhere.

#!/bin/bash

# Two variables are passed, Username and new Password. 

USERNAME=$1
PASS=$2

expect << EOF
spawn passwd ${USERNAME}
expect "Enter new UNIX password:"
send "${PASS}\r"
expect "Retype new UNIX password:"
send "${PASS}\r"
expect eof;
EOF

expect << EOF
spawn htdigest /.passwd "Authenticated Users" ${USERNAME}
expect "New password:"
send "${PASS}\r"
expect "Re-type new password:"
send "${PASS}\r"
expect eof;
EOF

expect << EOF
spawn htpasswd /squiddb ${USERNAME}
expect "New password:"
send "${PASS}\r"
expect "Re-type new password:"
send "${PASS}\r"
expect eof;
EOF

先谢谢您!

推荐答案

为什么不直接使用Expect shell来执行此操作.

Why not use the Expect shell directly for doing this.

#!/usr/bin/expect

set timeout 20
set user [lindex $argv 0]
set password [lindex $argv 1]

spawn passwd $user
expect "Enter new UNIX password:"
send "$password\r";
expect "Retype new UNIX password:"
send "$password\r";
wait 1

spawn htdigest /.passwd "Authenticated Users" $user
expect "New password:"
send "$password\r"
expect "Re-type new password:"
send "$password\r"
wait 1

spawn htpasswd /squiddb $user
expect "New password:"
send "$password\r"
expect "Re-type new password:"
send "$password\r"

exit 0

执行上述操作

./SCRIPTNAME.exp user password

这篇关于将带有特殊字符的变量作为密码传递会破坏bash脚本.如何清除bash中的特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆