dragoninasia2014
dragoninasia2014
2014-04-16 10:04
浏览 123
已采纳

从php中的shell_exec运行sed命令

Please help me as I have been trying to get this to work for two days.

I have a text file which has the array to define username and passwords.

$USERS['user'] = 'pass1';

I am trying to make a PHP to change this password

//name - call_pwd.php
$commandline='./change_password ' . $_SESSION["logged"] . " " . $_POST["old_password"] . " " . $_POST["new_password"] 
$output = shell_exec("$commandline");

Here is the bash file I made to change this password. Because I am not good at it I have to use so many variables to make it easily.

#!/bin/bash
#name - change_password
username=`echo "$1"`
old_password=`echo "$2"`
new_password=`echo "$3"`
old_string=`echo "\['$username'\] = '$old_password'"`
new_string=`echo "['$username'] = '$new_password'"`
sed -i "s/${old_string}/${new_string}/" passwords.php
if [ "$?" == 0 ]
then
echo "Password changed Successfully."
else
echo "Could not change password, try again."
fi

When I run this script from the root prompt it works fine and changes the password. But from PHP it won't work. It keeps giving me the message "Could not change password, try again."

As I read " and ' needs to be escaped if you want to use sed with php and I tried that with

sed -i \"s/${old_string}/${new_string}/\" passwords.php
sed -i \""s/${old_string}/${new_string}/\"" passwords.php

The permissions on passwords.php is full permission for all users. I have also tried putting absolute paths everywhere but that also didn't work.

The $output variable on my call_pwd.php comes out as when I echo $commandline $old_string and $new_string

change_password user pass1 pass2

\['user'\] = 'pass1'
['user'] = 'pass2'
Could not change password, try again. 

Please help me. Thanks. Apologies for such a long post.

图片转代码服务由CSDN问答提供 功能建议

请帮助我,因为我一直试图让它工作两天。 \ n

我有一个文本文件,其中包含用于定义用户名和密码的数组。

  $ USERS ['user'] ='pass1'; 
   
 
 

我正在尝试让PHP更改此密码

  // name  -  call_pwd.php 
 $ commandline ='  。/更改密码 ' 。  $ _SESSION [“已记录”]。  “”。  $ _POST [“old_password”]。  “”。  $ _POST [“new_password”] 
 $ output = shell_exec(“$ commandline”); 
   
 
 

这是我用来更改此密码的bash文件。 因为我不擅长它,所以我必须使用这么多变量才能轻松实现。

 #!/ bin / bash 
#name  -  change_password 
username =`echo  “$ 1”`
old_password =`echo“$ 2”`
new_password =`echo“$ 3”`
old_string =`echo“\ ['$ username'\] ='$ old_password'”`
new_string =`echo“[  '$ username'] ='$ new_password'“`
sed -i”s / $ {old_string} / $ {new_string} /“passwords.php 
if [”$?“  == 0] 
then 
echo“密码已成功更改。”
else 
echo“无法更改密码,请重试。”
fi 
   
 
 

当我运行时 来自根提示符的此脚本可以正常工作并更改密码。 但是从PHP开始它将不起作用。 它不断给我一条消息“无法更改密码,请再试一次。”

当我阅读“并且'如果你想用sed使用sed需要进行转义,我尝试了

  sed -i \“s / $ {old_string} / $ {new_string} / \”passwords.php 
sed -i \“”s / $ {old_string} /  $ {new_string} / \“”passwords.php 
   
 
 

password.php的权限是所有用户的完全权限。 我也尝试过将穷举放在任何地方 但这也行不通。

我的call_pwd.php上的$ output变量出现在我回显$ commandline $ old_string和$ new_string < p> change_password用户pass1 pass2

  \ ['user'\] ='pass1'
 ['user'] ='pass2'
无法更改密码,请重试 。
   
 
 

请帮助我。谢谢。 这么长的帖子的原因。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drny20290570
    drny20290570 2014-04-16 11:58
    已采纳

    Why are you doing it this way? You could do everything in PHP with [un]serialize() and file_[put/get]_contents().

    $ php -r '
        $USERS = [ "user" => "pass1" ];
        file_put_contents("passwords.php", serialize($USERS));'
    $ cat passwords.php; 
    a:1:{s:4:"user";s:5:"pass1";}
    $ php -r '
        $USERS = unserialize(file_get_contents("passwords.php"));
        $USERS["user"] = "newpass";
        file_put_contents("passwords.php", serialize($USERS));'
    $ cat passwords.php
    a:1:{s:4:"user";s:7:"newpass";}
    

    I won't make any comment on the security implications of doing this, but if you want to simply change some values in a text file when you are already using PHP, why not just use PHP?

    点赞 评论

相关推荐