3

When I execute netstat -a |find /C $portNumber in command prompt I get the total number of lines where the given port number exists.

I want to get that same count in any variable so that I can check whether the port is already occupied or free in NSIS.

I am excuting the below command and I tried in many ways, but I'm not able to get the output of the ExecDos::exec in a variable or in the stack.

ExecDos::exec "netstat -a |find /C '$portNumber'| $output"
Cosmin
  • 21,216
  • 5
  • 45
  • 60
user1234
  • 289
  • 6
  • 13
  • ... | $output is not a valid way to capture output unless $output expands to " > myfile.txt" etc. The execute plugins that support capturing stdout have specil syntax for it, check the plugin documentation. – Anders Jan 10 '12 at 08:41

1 Answers1

7

To enable shell behavior you have to execute cmd.exe /C yourcommand (Or expand %ComSpec% with ExpandEnvStrings but hardcoding cmd.exe is ok if you don't support Win9x)

Or you can try the ExecCmd plug-in which does this for you (But it has fewer options so you would have to redirect the output to a file)

nsExec::ExecToStack with the cmd prefix should also work...

Edit:

Here is a working example (I used nsExec since it part of the default install)

!include LogicLib.nsh
section
ExpandEnvStrings $0 %COMSPEC%
StrCpy $1 445 ;Port number
nsExec::ExecToStack '"$0" /C netstat -an|find /C ":$1"'
Pop $0
${If} $0 = 0
    Pop $0
    MessageBox mb_ok "Port count=$0"
${Else}
    ; Port not open...
${EndIf}
sectionend
Anders
  • 97,548
  • 12
  • 110
  • 164
  • I am writing this code for NSIS windows Installer. Context is I need to check whether the given port is free or not (not used by any application) So thought of using netstat -a and find the port in the result of netstat command. – user1234 Jan 10 '12 at 09:11
  • Can anyone please tel me how to check whether the port is binded to other application or not in my .nsi file. Help is highly appreciated!! – user1234 Jan 12 '12 at 05:18
  • I am really waiting for a help. Please throw me some light – user1234 Jan 12 '12 at 09:13
  • 1
    Did you try any of the things I suggested? – Anders Jan 12 '12 at 09:33
  • I am getting invalid command error for this line `code` nsExec::ExecToStack '"$0" /C netstat -an|find /C ":$1"' – user1234 Jan 12 '12 at 09:58
  • What is the exact compiler error? Did you install NSIS with the default plugins? (The installed plugins are listed early in the compiler output) – Anders Jan 12 '12 at 10:01
  • thank u for the help. I am getting compiler error like this **Invalid command: nsExec::ExecToStack ** for this code. `nsExec::ExecToStack '"$0" /C netstat -an|find /C ":$1"'` Should i include any plugin? – user1234 Jan 12 '12 at 10:02
  • That plugin should be part of the default install, try re-installing NSIS and make sure that plugin is checked on the components page... – Anders Jan 12 '12 at 10:04
  • can u pls tel me which dll represents nsExec? or is there any other option of achieving ur code using ExecDos ? – user1234 Jan 12 '12 at 10:06
  • which version of NSIS is the latest one? – user1234 Jan 12 '12 at 10:07
  • 1
    2.46 (How hard is it to just check the website?) – Anders Jan 12 '12 at 10:09
  • sorry, I just reinstalled and now its getting compiled and now verifying the port status – user1234 Jan 12 '12 at 10:15
  • thank u so much for the solution. I am really thankful to u. IS this way of port availability check is efficient? because when i do netstat - an in my command prompt, it also shows the foreign address port binding. if port is used at foreign address also this says port is not free. Could u pls comment on this. thank u once again!! – user1234 Jan 12 '12 at 10:34
  • A) Compare with the result of "netstat -n" B) Use something like netstat -an|find /V "ESTAB"|find /V "ACK"|find /C ":135" C) You would have to stop using the /C find switch and parse each returned line after pop'ing it – Anders Jan 12 '12 at 10:43