- NSIS Discussion
- External console command
Archive: External console command
ftctran
10th August 2008 08:40 UTC
External console command
I'm new with NSIS.
I want to check the MAC address but this dos exec give me error.
ExecDos::exec /TIMEOUT=2000 "$SYSDIR\ipconfig.exe /all | findstr 00-11-A3-03-B7-A1" "" "$EXEDIR\MAC.txt"
Pop $0 # return value
DetailPrint "Exit code: $0"
StrCmp $0 "error" goto End
"Exit code: error"
Please let me know why and how I can work around it.
Thanks very much for your help
FT
LoRd_MuldeR
10th August 2008 14:38 UTC
1. It's not a DOS executable, it's a console program ;)
2. I don't think you can run it this way, try like that:
!include StrFunc.nsh
${StrLoc}
...
Section
nsExec::ExecToStack /TIMEOUT=2000 '"$SYSDIR\ipconfig.exe" /all'
Pop $0 # return value
Pop $1 # console output
DetailPrint "Exit code: $0"
StrCmp $0 "error" goto End
${StrLoc} $0 "$1" "00-11-A3-03-B7-A1" ">"
StrCmp $0 "" MAC_NotFound MAC_Found
MacFound:
...
MAC_NotFound:
...
End:
SectionEnd
Anders
10th August 2008 15:03 UTC
Why would you hardcode something for a specific MAC, you know people can change their MAC right?
for atleast XP and later, there is a program called GetMAC, just use "for" to get the first token of the output of something like: getmac|find "\". for older systems without getmac, you would need to parse the output of ipconfig /all|find "Physical Address" with a "for" to get the token after ":" (I guess that would only work on english systems)
for /F %A in ('getmac^|find "\"')DO @echo %A
and
FOR /F "TOKENS=1-3 DELIMS=.: " %A IN ('IPCONFIG/ALL^|FIND/I "Physical"') DO
@ECHO %C
(for batch file use, you must use %%A instead of %A)
LoRd_MuldeR
10th August 2008 15:22 UTC
You could try like this:
!include StrFunc.nsh
${StrTok}
...
Section
nsExec::ExecToStack /TIMEOUT=2000 '"$SYSDIR\getmac.exe"'
Pop $0 # return value
Pop $1 # console output
DetailPrint "Exit code: $0"
StrCmp $0 "error" goto End
StrCpy $0 "0"
Loop:
${StrTok} $2 "$1" "$\n$\r$\t " "$0" "1"
StrCmp $2 "" MAC_NotFound
StrCmp $2 "00-11-A3-03-B7-A1" MAC_Found
IntOp $0 $0 + 1
Goto Loop
MacFound:
...
MAC_NotFound:
...
End:
SectionEnd
LoRd_MuldeR
10th August 2008 15:30 UTC
Test and seems to work fine:
!include StrFunc.nsh
${StrTok}
Caption "MAC Test"
OutFile "macTest.exe"
!define MAC_Address "00-11-A3-03-B7-A1"
Section
nsExec::ExecToStack /TIMEOUT=5000 '"$SYSDIR\getmac.exe" /FO CSV'
Pop $0 # return value
Pop $1 # console output
DetailPrint "Exit code: $0"
StrCmp $0 "error" End
StrCpy $0 "0"
Loop:
${StrTok} $2 "$1" '$\n$\r$\t",' "$0" "1"
# MessageBox MB_OK '"$2"'
StrCmp $2 "" MAC_NotFound
StrCmp $2 "${MAC_Address}" MAC_Found
IntOp $0 $0 + 1
Goto Loop
Mac_Found:
MessageBox MB_ICONINFORMATION "MAC was found!"
Goto End
MAC_NotFound:
MessageBox MB_ICONSTOP "MAC was not found!"
Abort
End:
SectionEnd
demiller9
10th August 2008 15:49 UTC
That's odd. My Windows XP doesn't have Getmac.exe. A quick search shows it is part of the Win2000 resource kit, and it seems it also comes with Vista.
Any solution using this exe will have to pack it into the installer.
Don
Anders
10th August 2008 15:59 UTC
Originally posted by demiller9
That's odd. My Windows XP doesn't have Getmac.exe. A quick search shows it is part of the Win2000 resource kit, and it seems it also comes with Vista.
That is weird, is it XP Home? My getmac version is 5.1.2600.0 (xpclient.010817-1148) (XP MCE2k5 SP2)
demiller9
10th August 2008 16:02 UTC
Yes, it is XP Home. It is currently SP3 (just recently upgraded from SP2).
Don
LoRd_MuldeR
10th August 2008 16:07 UTC
Version to work with ipconfig.exe is here:
!include StrFunc.nsh
${StrLoc}
Caption "MAC Test"
OutFile "macTest.exe"
!define MAC_Address "7A-79-05-EB-3D-66"
Section
nsExec::ExecToStack /TIMEOUT=5000 '"$SYSDIR\ipconfig.exe" /all'
Pop $0 # return value
Pop $1 # console output
DetailPrint "Exit code: $0"
StrCmp $0 "error" End
StrLen $0 $1
DetailPrint "Output length: $0 of ${NSIS_MAX_STRLEN}"
DetailPrint "Checking MAC address..."
${StrLoc} $0 "$1" "Physical Address. . . . . . . . . : ${MAC_Address}" ">"
StrCmp $0 "" MAC_NotFound MAC_Found
Mac_Found:
MessageBox MB_ICONINFORMATION "MAC was found!"
Goto End
MAC_NotFound:
MessageBox MB_ICONSTOP "MAC was not found!"
Abort
End:
SectionEnd
This should be used with the "Large strings" version of NSIS (
download), because the output of ipconfig.exe can easily exceed the default string length of 1024 chars. On my system (3 network adapters) for example it does...
EDIT: Updated code, previous code was too slow :p
ftctran
11th August 2008 06:40 UTC
You guys are awsome. Thanks a lot for your quick help, especially to LoRd_MuldeR.
I still wonder why the pipe '|' don't work. Is there a way to make it work with nsis?
For example:
C:\ipconfig.exe /all | findstr "MyPCName"
FT
Anders
11th August 2008 08:18 UTC
the pipe works, you just have to invoke the command line interpreter, search this forum for COMSPEC for examples probably (read COMSPEC env. then execute $COMSPEC /K foo|bar)
LoRd_MuldeR
11th August 2008 13:49 UTC
Still I wouldn't invoke the commandline interpreter as long as I can do it my NSIS script natively. I'd even prefer writing my own plugin, instead of relying on the commandline interpreter. It just isn't safe over different Windows versions..