我们需要一个shell脚本来查找和replace/ home文件夹中的所有index.php文件中的一段注入代码
你可以用sed来做。 这是一个很好的教程 。 它可能就像find /home/user -iname index.php -exec sed -is/<piece of code to find/<replace with that>/ {} \;那样简单find /home/user -iname index.php -exec sed -is/<piece of code to find/<replace with that>/ {} \;
我build议你在做这些之前进行备份,并运行一些testing,看看它是否工作正常。 请记住,/ /之间的东西是正则expression式,并根据其格式,你可能会匹配更多的东西,你需要的东西。 正如我所说,testing之前,在您的“活”文件上运行。
编辑:修复查找命令,谢谢雷姆斯
for file in $(find /home/user -iname index.php) do echo "replacing in file $file ..." sed -i 's/<piece of code to find>/<replace with that>/g' $file done
如果你想先testing一下,可以先把它放在一个临时文件中,检查是否正确,然后覆盖原来的文件:
for file in $(find /home/user -iname index.php) do echo "reading from $file, writing to $file.tmp ..." sed 's/<piece of code to find>/<replace with that>/g' $file > $file.tmp done
现在浏览一些文件,检查replace是否正确。 如果一切正常,然后将新的index.php.tmp文件重命名为index.php
for file in $(find /home/user -iname index.php.tmp) do echo "moving $file.tmp to $file ..." mv $file.tmp $file done
如果你想要replace的代码中有斜杠(“/”),那么你可以在sed substitute命令中使用另外一个分隔符:#代码段#用这个#代替#