`
HuNanPengdake
  • 浏览: 232109 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Linux ifconfig查看本机IP提示command not found

阅读更多
本文转自:http://blog.csdn.net/aigoleague/article/details/7996151


问题:在red hat enterprise linux 5中查询IP地址时,输入ifconfig命令报错:“ifconfig: command not found”
原因:ifconfig命令所在路径/sbin未包含在系统环境变量PATH中

解决方法:
1. 直接输入:/sbin/ifconfig
2. 临时修改环境变量:在shell中输入
   $export PATH = $PATH:/sbin
   然后再输入ifconfig命令即可,但是这只是临时更改了shell中的PATH,如果关闭shell,则修改消失,下次还需要重复如上操作
3.永久修改PATH变量使之包含/sbin路径:
   打开/etc/profile文件,在其中输入export PATH=$PATH:/sbin,保存并重启X即可,这样一来,PATH路径永久修改成功,以后任何时候只需书序ifconfig命令即可


下面的代码可以得到本机的ip地址
function getIP(){
    exec("/sbin/ifconfig",$out,$stats);
    //var_dump($out);
    if(!empty($out))
    {  
        if(isset($out[1]) && strstr($out[1],'addr:'))
        {  
            $tmpArray = explode(":", $out[1]);
            print_r($tmpArray);
            $tmpIp = explode(" ", $tmpArray[1]);
            print_r($tmpIp);
            //if(preg_match ("/192\.168\.1\./", trim($tmpIp[0])))         //pattern根据需要修改
            //{
            return trim($tmpIp[0]);
            //}
        }  
    }  
    return '127.0.0.1';

}

$my_ip = getIP();

//列出php的内置变量
php -i
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics