-1

I am trying to run the below batch script using PHP (it runs fine from command prompt) to change the IP using the argument passed

@echo off
setlocal enabledelayedexpansion
netsh int ip set address "EDU_LAN_Connection" static 172.31.%1.110 255.255.255.0 172.31.%1.1 1

In PHP:

 system("cmd /c C:\EDU_Project\change_ip.bat 6");

I get the following error when running the PHP script:

Invalid address parameter (172.31.61.1 1). It should be a valid IPv4 address. 

I am not sure how to use delayedexpansion with argument. Please help !

Chetan
  • 387
  • 1
  • 4
  • 11
  • 2
    I think you should look into how IPV4 addresses are formatted – Daryl Gill Jan 26 '15 at 20:50
  • you're trying to assign a class B address, which is all fine. A standard class B address would be (172.x.y.z), You're currently doing 172.x.y.z A, where A is. Is invalid for an IP construct, hence my original comment – Daryl Gill Jan 26 '15 at 20:55
  • This error is same as discussed in this question: http://stackoverflow.com/questions/21824179/ip-configuration-from-batch-file but I am not sure how to use arguments with delayedexpansion – Chetan Jan 26 '15 at 21:00

2 Answers2

1

Batch parameters are addressed with %1 only. Not %1%

Change your batchfile accordingly:

 netsh int ip set address "EDU_LAN_Connection" static 172.31.%1.110 255.255.255.0 172.31.%1.1 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thanks I made that change, script runs fine on command prompt but I still get this error when ran from PHP: Invalid address parameter (172.31.6.110 255.255.255.0 172.31.6.1). It should be a valid IPv4 address. Ping request could not find host 172.31.6.1 -n 2. Please check the name and try again. – Chetan Jan 26 '15 at 20:57
  • I have edited a typo in my answer. Try again please. – Stephan Jan 26 '15 at 21:09
  • Thanks for the help Stephan, yeah I already saw that typo it still does not work. Works fine on command prompt though. – Chetan Jan 26 '15 at 21:10
  • The error though has changed to this: Invalid address parameter (172.31.6.110 255.255.255.0 172.31.6.1). It should be a valid IPv4 address – Chetan Jan 26 '15 at 21:12
  • I can see no reason for this. But I notice, that the last "1" (for "metric") is missing. – Stephan Jan 26 '15 at 21:46
0

Instead of going for system function and passing an argument to the batch file. I ran the netsh command directly from PHP using shell_exec:

$var= 18;
shell_exec('netsh int ip set address name=\"EDU_LAN_Connection\" static 172.31.'.$var.'.110 255.255.255.0 172.31.'.$var.'.1 1');
Chetan
  • 387
  • 1
  • 4
  • 11