Automatic Wan Gateway configuration through php shell



  • Hello all,

    For my project, i'd like to automate the WAN interface Gateway in the case of a static IPv4 configuration.

    Actually I have a .sh script that I upload to a temporary repo onto the pfSense. This script contain the php shell commands I want to execute. Once the shel script has been uploaded, I connect to the pfSense and launch this script.

    For now, I am able to switch from DHCP configuration to static IPv4, I'm able to set the wan speed, but I am absolutely unable to set an interface Gateway and use it.

    Do you have any idea how can I create a new Gateway and use it for a specific interface?

    For your information, here is the shell script :

    speedIndex=$1
    dhcpStatus=$2
    netmask=$3
    staticIpAddress=$4
    firewallGateway=$5
    cmd=/tmp/speedinfo
    rm -f "$cmd" >> /dev/null
    
    # pfsense is limited to sh and sh does not support arrays:
    #media = ( "autoselect" "10BaseT/UTP" "10BaseT/UTP" "100BaseTX" "100BaseTX" "1000BaseT" )
    #mediaopt=( "" "" "full-duplex" "" "full-duplex" "" ) 
    if [ "$speedIndex" -eq 0 ]; then
      media="autoselect"
      mediaopt=""
    fi
    if [ "$speedIndex" -eq 1 ]; then
    media="10BaseT/UTP"
    mediaopt=""
    fi
    if [ "$speedIndex" -eq 2 ]; then
    media="10BaseT/UTP"
    mediaopt="full-duplex"
    fi
    if [ "$speedIndex" -eq 3 ]; then
    media="100BaseTX"
    mediaopt=""
    fi
    if [ "$speedIndex" -eq 4 ]; then
    media="100BaseTX"
    mediaopt="full-duplex"
    fi
    if [ "$speedIndex" -eq 5 ]; then
    media="1000BaseT"
    mediaopt=""
    fi
    
    # Set the ipaddr parameter with the static IP address or the dhcp parameter
    if [ "$dhcpStatus" -eq 0 ]; then
      ipAddr="$staticIpAddress"
    fi
    if [ "$dhcpStatus" -eq 1 ]; then
      ipAddr="dhcp"
    fi
    
    touch $cmd
    echo "\$config['interfaces']['wan']['enable'] = true;" >> $cmd
    echo "\$config['interfaces']['wan']['media']=\"$media\";" >> $cmd
    echo "\$config['interfaces']['wan']['mediaopt']=\"$mediaopt\";" >> $cmd
    echo "\$config['interfaces']['wan']['ipaddr']=\"$ipAddr\";" >> $cmd
    echo "\$config['interfaces']['wan']['subnet']=\"$netmask\";" >> $cmd
    if [ "$dhcpStatus" -eq 0 ]; then
    	echo "\$config['gateways']['gateway_item']['interface']=\"wan\";" >> $cmd
    	echo "\$config['gateways']['gateway_item']['gateway']=\"$firewallGateway\";" >> $cmd
    	echo "\$config['gateways']['gateway_item']['name']=\"WANGW\";" >> $cmd
    	echo "\$config['gateways']['gateway_item']['weight']=\"1\";" >> $cmd
    	echo "\$config['gateways']['gateway_item']['ipprotocol']=\"inet\";" >> $cmd
    
    	echo "\$config['interfaces']['wan']['gateway']=\"WANGW\";" >> $cmd
    fi
    echo "write_config();" >> $cmd 
    echo "exec;" >> $cmd 
    echo "unlink_if_exists(\"{\$g['tmp_path']}/config.cache\");" >> $cmd
    echo "clear_subsystem_dirty('interfaces');" >> $cmd
    echo "interface_bring_down('wan', false, \$ifcfgo);" >> $cmd
    echo "interface_configure('wan', true);" >> $cmd
    echo "services_snmpd_configure();" >> $cmd
    echo "setup_gateways_monitor();" >> $cmd
    echo "clear_subsystem_dirty('interfaces');" >> $cmd
    echo "exec;" >> $cmd
    echo "system_reboot_sync();" >> $cmd
    echo "exec;" >> $cmd
    echo "exit" >> $cmd 
    
    #Apply
    pfSsh.php < $cmd
    

    Doing this, I have has much new interfaces created in the web interface as I have

    echo "\$config['gateways']['gateway_item']
    

    lines in my script, but none with the desired configuration

    I hope you will be able to help me.

    Nicolas Kaminski


 

© Copyright 2002 - 2018 Rubicon Communications, LLC | Privacy Policy