现在的位置: 首页 > 综合 > 正文

PHP调用系统命令修改IP,netmask,gateway,mac,dns

2013年12月12日 ⁄ 综合 ⁄ 共 5117字 ⁄ 字号 评论关闭

ip_contrl.php:

<?php
 //include('header.php');
 
 $ipaddr = $_POST['ipaddr'];
 $netmask = $_POST['netmask'];
 $gateway = $_POST['gateway'];
 $mac = $_POST['mac'];
 $dns1 = $_POST['dns1'];
 $dns2 = $_POST['dns2'];
 //echo "ipaddr=$ipaddr;netmask=$netmask<br>";
 
 if( $ipaddr && $netmask && $gateway && $mac && $dns1 && $dns2 )
 {
  system("./ip/changeip 1 $ipaddr $netmask $gateway $mac $dns1 $dns2");
  
  echo "<script type='text/javascript'>
    alert( '网络配置成功!' );
    window.location.href = 'service_manage.php?id=2';
     </script>";
 }
 else
 {
  exec("ifconfig eth0", $res);
  
  $str = explode( "HWaddr ",$res[0] );
  $macaddr = $str[1];    //mac地址
  //echo "str[1]=".$str[1]."<br>";  
  
  $s = explode( "addr:",$res[1] );  
  $s1 = explode( "  Bcast:",$s[1] );
  $ipaddr = $s1[0];  //ip地址
  //echo "s1[0]=".$s1[0]."<br>";
  $s2 = explode( "Mask:",$s1[1] );
  $netmask = $s2[1];  //子网掩码
  //echo "s2[1]=".$s2[1]."<br>";
 }
?>

<script language="javascript">
function checkFormData()
{
   if ( form1.ipaddr.value == "")
 {
  alert ( "IP地址不能为空!" );
  form1.ipaddr.focus();
  return false;
 } 
 if ( form1.ipaddr.value.search (".*?((25[0-5]|2[0-4]//d|1//d{2}|[1-9]//d|//d)//.){3}(25[0-5]|2[0-4]//d|1//d{2}|[1-9]//d|//d).*?") != 0 )
 {
  alert ( "IP格式不正确!" );  
  form1.ipaddr.focus();
  return false; 
   }
 
 if ( form1.netmask.value == "" )
 {
  alert ( "子网掩码不能为空!" );
  form1.netmask.focus();
  return false;
 }
 if ( form1.netmask.value.search (".*?((25[0-5]|2[0-4]//d|1//d{2}|[1-9]//d|//d)//.){3}(25[0-5]|2[0-4]//d|1//d{2}|[1-9]//d|//d).*?") != 0 )
 {
  alert ( "子网掩码格式不正确!" );
  form1.netmask.focus();
  return false;
   }
 
 if ( form1.gateway.value == "" )
 {
  alert ( "网关不能为空!" );
  form1.gateway.focus();
  return false;
 }
 if ( form1.gateway.value.search (".*?((25[0-5]|2[0-4]//d|1//d{2}|[1-9]//d|//d)//.){3}(25[0-5]|2[0-4]//d|1//d{2}|[1-9]//d|//d).*?") != 0 )
 {
  alert ( "网关格式不正确!" );
  form1.gateway.focus();
  return false;
   }
 
 if ( form1.mac.value == "" )
 {
  alert ( "MAC地址不能为空!" );
  form1.mac.focus();
  return false;
 }
 if ( form1.mac.value.search ("^([0-9a-fA-F]{2})(([//s:][0-9a-fA-F]{2}){5})$") != 0 )
 {
  alert ( "MAC地址格式不正确!" );
  form1.mac.focus();
  return false;
   }

 if ( form1.dns1.value == "" )
 {
  alert ( "域名服务器不能为空!" );
  form1.dns1.focus();
  return false;
 }
 if ( form1.dns1.value.search (".*?((25[0-5]|2[0-4]//d|1//d{2}|[1-9]//d|//d)//.){3}(25[0-5]|2[0-4]//d|1//d{2}|[1-9]//d|//d).*?") != 0 )
 {
  alert ( "域名服务器地址格式不正确!" );
  form1.dns1.focus();
  return false;
   }
 
 if ( form1.dns2.value == "" )
 {
  alert ( "备用域名服务器不能为空!" );
  form1.dns2.focus();
  return false;
 } 
 if ( form1.dns2.value.search (".*?((25[0-5]|2[0-4]//d|1//d{2}|[1-9]//d|//d)//.){3}(25[0-5]|2[0-4]//d|1//d{2}|[1-9]//d|//d).*?") != 0 )
 {
  alert ( "备用域名服务器地址格式不正确!" );
  form1.dns2.focus();
  return false;
   }
}
</script>

<form name="form1" action="" method="post" onSubmit="return checkFormData()">
 <table align="center" border="1" align="center">
  <tr>
        <th height="22" colspan="2" bgcolor="#4489CA" scope="col"><div align="center" class="STYLE3 STYLE1" style="font-size: 14px; color: #FFFFFF">网络配置</div></th>
     </tr>
  <tr>
   <td>ipadress</td>
   <td><input type="text" name="ipaddr" value="<?=$ipaddr;?>"></td>
  </tr>
  <tr>
   <td>netmask</td>
   <td><input type="text" name="netmask" value="<?=$netmask;?>"></td>
  </tr>
  <tr>
   <td>gateway</td>
   <td><input type="text" name="gateway"></td>
  </tr>
  <tr>
   <td>mac</td>
   <td><input type="text" name="mac" value="<?=$macaddr;?>"></td>
  </tr>
  <tr>
   <td>dns1</td>
   <td><input type="text" name="dns1"></td>
  </tr>
  <tr>
   <td>dns2</td>
   <td><input type="text" name="dns2"></td>
  </tr>
  <tr>
        <td height="37" colspan="2" align="center">     
           <input type="submit" name="Submit" value="提交">
           <input type="reset" name="Submit2" value="重置">
        </td>
     </tr>
 </table>
 <br /><br />
 <table>
  <tr>
   <td align="center">MAC地址的格式:xx:xx:xx:xx:xx:xx</td>
  </tr>
  <tr>
   <td align="center">特别提醒:当你修改IP地址后,应将地址栏中的http://="<?=$ipaddr;?>/softswitch/service_manage.php?id=2中的IP地址换成你修改后的IP</td>
  </tr>
 </table>
</form>

 

changip.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

int main( int argc,char *argv[] )
{//argv[1]=1表示修改IP和子网掩码
 //argv[1]=2表示默认网关
 //argv[1]=3表示修改MAC
    uid_t uid ,euid;
    char str[60];
    //char str2[50];
    uid = getuid() ;
    euid = geteuid();

        /*printf( "--->>>%s<<<---/n",argv[0] );
        printf( "--->>>%s<<<---/n",argv[1] );
        printf( "--->>>%s<<<---/n",argv[2] );
        printf( "--->>>%s<<<---/n",argv[3] );*/

    if(setreuid(euid, uid))  //交换这两个id
    {
        perror("setreuid");
    }
    //system("./ip/changeip 1 $ipaddr $netmask $gateway $mac $dns1 $dns2");
    if( strcmp(argv[1],"1") == 0 )
    {
        //配置mac
        sprintf( str,"/sbin/ifconfig eth0 hw ether %s",argv[5] );
        system( "/sbin/ifconfig eth0 down" );
        system( str );
        system( "/sbin/ifconfig eth0 up " );
        sleep(1);
        //配置ip,子网掩码
        sprintf( str,"ifconfig eth0 %s netmask %s up",argv[2],argv[3] );
        system( str );
        //配置网关
        sprintf( str,"route add default gw %s dev eth0",argv[4] );
        system( str );
        //配置DNS
        system( "echo search localdomain >/etc/resolv.conf" );
        sprintf( str,"echo nameserver %s >>/etc/resolv.conf",argv[6] );
        system( str );
        sprintf( str,"echo nameserver %s >>/etc/resolv.conf",argv[7] );
        system( str );
    }
    /*if( strcmp(argv[1],"2") == 0 )
    {
        sprintf( str,"route add default gw %s dev eth0",argv[2] );
        system( str );
    }*/
    return 0;
}

抱歉!评论已关闭.