doulangdang9986 2014-11-21 14:52
浏览 42

通过PHP exec()运行Clogin

I'm trying to create a PHP page that I can go to in order to get information from cisco switches we have. My only current option for reading data from them is to use rancid's clogin.

When I run:

sudo -u www-data /usr/lib/rancid/bin/clogin -f /home/www-data/.cloginrc -c 'show run int fa 0/1' as-switch-123 2>&1

it runs and returns the information about port 0/1.

However when I run the following:

<?php

    ini_set('display_errors',1);
    error_reporting(E_ALL);


    $mainCommand = "/usr/lib/rancid/bin/clogin -f /home/www-data/.cloginrc -c 'show run int fa 0/1' as-switch-123 2>&1";


    $outputArray = array();
    echo exec($mainCommand,$outputArray,$returnCode);
    echo '<br /><br />';
    var_dump($outputArray);
    echo '<br /><br />';
    var_dump($returnCode);

?>

I get the return from $outputArray shown below:

array(5) { 
    [0]=> string(16) "no such variable" 
    [1]=> string(31) " (read trace on "env(HOME)")" 
    [2]=> string(23) " invoked from within" 
    [3]=> string(40) ""set password_file $env(HOME)/.cloginrc"" 
    [4]=> string(47) " (file "/usr/lib/rancid/bin/clogin" line 66)"
} 

Does anyone know why its not giving me the output the same as when you run it from sudo?

  • 写回答

2条回答 默认 最新

  • drob50257447 2014-11-21 15:00
    关注

    The error you got tells me that there was a problem with an environment variable, and the only environment variable in the line of code it was trying to process is $HOME.

    I expect you would be able to replcate this using:

    sudo -u www-data "HOME= ; /usr/lib/rancid/bin/clogin -f /home/www-data/.cloginrc -c 'show run int fa 0/1' as-switch-123 2>&1"
    

    Similarly setting a value inside the command executed in PHP should solve the problem.

    $mainCommand = "HOME=/home/www-data && /usr/lib/rancid/bin/clogin -f /home/www-data/.cloginrc -c 'show run int fa 0/1' as-switch-123 2>&1";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛