【问题标题】:sudo command inside shell script and called by PHPshell 脚本中的 sudo 命令并由 PHP 调用
【发布时间】:2015-11-30 05:59:14
【问题描述】:

背景

我制作了一个 PHP 网络应用程序 来执行 Linux Shell 脚本 来更改 CentOS 7 的 network-scripts 中的数据。在换句话说,这是一个可以在 CentOS 7 中更改 IP 的 PHP Web 应用程序。

脚本本身可以更改,我可以使用带有适当参数的 SSH 运行脚本,用法如下:

sh ./ipchanger.sh <fileName> <oldIpAddress> <newIpAddress> <oldSubnetMask> <newSubnetMask> <oldGateway> <newGateway>

示例用法:

 sh ./ipchanger.sh /etc/sysconfig/network-scripts/ifcfg-ens32 192.168.1.5 192.168.1.205 PREFIX0=32 PREFIX0=24 192.168.1.1 192.168.1.1

这会将 IP 从192.168.1.5 更改为192.168.1.205,子网掩码将从255.255.255.255 更改为255.255.255.0。网关将保持不变。


PHP 应用程序

数据将从使用 PHP 处理的表单发布。该代码将检查 IP 地址是否正确。如果参数被收集并正确,我的 PHP 代码将调用 shell 脚本来更改网络脚本。

像这样:

$retval = exec('sh /var/www/html/ipchanger/ipchanger.sh {$fileName} {$currentIpAddress} {$newIpAddress} {$currentSubnetMask} {$newSubnetMask} {$currentGateway} {$newGateway}');

这意味着:

$retval = exec('sh /var/www/html/ipchanger/ipchanger.sh /etc/sysconfig/network-scripts/ifcfg-ens32 192.168.1.5 192.168.1.205 PREFIX0=32 PREFIX0=24 192.168.1.1 192.168.1.1');


Shell 脚本

#!/bin/sh
#
# My IP Changer

fileName="$1"
currentIpAddress="$2"
newIpAddress="$3"
currentSubnetMask="$4"
newSubnetMask="$5"
currentGateway="$6"
newGateway="$7"

`sudo sed -i -e "s/$currentIpAddress/$newIpAddress/g" ${fileName}`
`sudo sed -i -e "s/$currentSubnetMask}/$newSubnetMask/g" ${fileName}`
`sudo sed -i -e "s/$currentGateway/$newGateway/g" ${fileName}`


问题

文件/etc/sysconfig/network-scripts/ifcfg-ens32 根本没有改变。如果我在 SSH 中运行 shell 脚本(请参阅背景章节中的示例用法),它可以工作!所以我的shell脚本应该没问题。


其他试验

1.将 echo 放入 shell 脚本以查看参数是否在正确位置
结果:是。
参数显示与预期一样。

2。将 2>&1 放在 exec() 后面
结果:显示消息。
sudo: sorry, you must have a tty to run sudo。我不知道sed 是否需要root 权限。所以我还是把它放在了shell脚本中,让shell脚本执行起来更流畅。

3.删除 shell 脚本中的 sudo
结果:在 SSH 中,很好;在 PHP 中,显示消息。
sed: couldn't open temporary file /etc/sysconfig/network-scripts/sedJfDtCD: Permission denied。我用谷歌搜索了这条消息。使用sed -i时,会创建一个临时文件来存放原始文件,以防脚本出错。

4.删除 shell 脚本中 sed 命令中的 -i
结果:失败。
该脚本无法执行其任务。


其他信息

  • 操作系统: CentOS
  • 网络服务器类型: LAMP
  • whoami:阿帕奇
  • 脚本使用:内部使用。所以我不关心安全问题
  • 请帮忙!谢谢。

    【问题讨论】:

    • 好吧,你的问题就在那里:sudo: sorry, you must have a tty to run sudosudo 在从 tty 以外的任何地方调用时都不会运行(这是一个登录 shell,与您尝试从中调用它的 PHP 脚本相反)。
    • 给你的脚本适当的权限,你甚至不需要考虑这样做。
    • 这个问题可能会有所帮助:stackoverflow.com/questions/3173201/sudo-in-php-exec 尤其是来自visudorequiretty 部分。
    • @l'L'l 该文件在以下文件中的 chmod 为 0777:index.phpipchanger.shifcfg-ens32,并且所有者是 root。
    • chmod 只是其中的一部分;您还需要修改脚本所有者chown

    标签: php bash shell sed centos


    【解决方案1】:

    我最近发布了一个项目,它允许 PHP 获取并与真正的 Bash shell 交互(如果需要,作为用户:apache/www-data 或 root)。在这里获取:https://github.com/merlinthemagic/MTS

    下载后,您只需使用以下代码。

    //You could maintain your ipchanger.sh script and simply trigger that script
    //through the shell, but the point of the shell project is that it lets you 
    //trigger commands directly. in your case you could do this:
    
    $ifFilePath = '/etc/sysconfig/network-scripts/ifcfg-ens32';    
    
    $ifCfg= "DEVICE=ens32";
    $ifCfg.= "\nIPADDR=192.168.1.205";
    $ifCfg.= "\nPREFIX0=24";
    $ifCfg.= "\n192.168.1.1";
    
    $strCmd = "echo \'".$ifCfg."\' > \'".$ifFilePath."\'";
    
    //But if you wanna stick with your script then:
    
    $strCmd = "sh ./ipchanger.sh /etc/sysconfig/network-scripts/ifcfg-ens32 192.168.1.5 192.168.1.205 PREFIX0=32 PREFIX0=24 192.168.1.1 192.168.1.1";
    
    //in either case the $strCmd is triggered like this:
    $shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
    $return1  = $shell->exeCmd($strCmd);
    

    配置文件更改后,您可以使用相同的 shell 重新加载接口配置:

    $return2  = $shell->exeCmd('service network restart');
    

    【讨论】:

      【解决方案2】:

      不是直接为您解决正确的问题,但我建议在此类文件更改时添加一些安全性

      并优化最后三行:

      `sudo sed -i -e "s/$currentIpAddress/$newIpAddress/g;s/$currentSubnetMask}/$newSubnetMask/g;s/$currentGateway/$newGateway/g" ${fileName}`
      
      • 每个s/// 中的最后一个g 通常是不必要的(只需逐行更改1 个)

      【讨论】:

      • 我是 Shell Script 的新手(3 天前开始学习)所以感谢您的建议。我已经编辑了我的 shell 脚本。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多