Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. !include "ServiceLib.nsh"
  2. !include "LogicLib.nsh"
  3.  
  4. Name "test"
  5. OutFile "test.exe"
  6. XPStyle on
  7. ShowInstDetails show
  8. RequestExecutionLevel admin
  9.  
  10. Page instfiles
  11.  
  12. Function IsPortOpen
  13.  
  14.   Exch $R0 # port to check
  15.   Exch
  16.   Exch $R1
  17.   Push $0
  18.   Push $1
  19.   Push $2
  20.  
  21.   System::Call 'iphlpapi::Get$R1Table(*i.r0, *i .r1, i 1) i .r2'
  22.   ${If} $2 != 122 # ERROR_INSUFFICIENT_BUFFER
  23.     StrCpy $R0 ""
  24.     Pop $2
  25.     Pop $1
  26.     Pop $0
  27.     Exch $R1
  28.     Exch
  29.     Exch $R0
  30.     Return
  31.   ${EndIf}
  32.  
  33.   System::Alloc $1
  34.   Pop $0
  35.  
  36.   System::Call 'iphlpapi::Get$R1Table(ir0, *i r1, i 1) i .r2'
  37.   ${If} $2 != 0 # NO_ERROR
  38.     System::Free $0
  39.     StrCpy $R0 ""
  40.     Pop $2
  41.     Pop $1
  42.     Pop $0
  43.     Exch $R1
  44.     Exch
  45.     Exch $R0
  46.     Return
  47.   ${EndIf}
  48.  
  49.   Push $3
  50.   Push $4
  51.   Push $5
  52.  
  53.   System::Call *$0(i.r2)
  54.   IntOp $2 $2 - 1
  55.   ${For} $3 0 $2
  56.     IntOp $4 $0 + 4  # skip dwNumEntries
  57.     ${If} $R1 == "Tcp"
  58.       IntOp $5 $3 * 20 # sizeof(MIB_TCPROW)
  59.       IntOp $4 $4 + $5 # skip to entry
  60.       System::Call *$4(i,i,i.r4,i,i)
  61.     ${Else}
  62.       IntOp $5 $3 * 8 # sizeof(MIB_UDPROW)
  63.       IntOp $4 $4 + $5 # skip to entry
  64.       System::Call *$4(i,i.r4)
  65.     ${EndIf}
  66.     System::Call ws2_32::ntohs(ir4)i.r4
  67.     ${If} $4 = $R0
  68.       StrCpy $R0 "open"
  69.       ${Break}
  70.     ${EndIf}
  71.   ${Next}
  72.  
  73.   ${If} $R0 != "open"
  74.     StrCpy $R0 "closed"
  75.   ${EndIf}
  76.  
  77.   System::Free $0
  78.  
  79.   Pop $5
  80.   Pop $4
  81.   Pop $3
  82.   Pop $2
  83.   Pop $1
  84.   Pop $0
  85.   Exch $R1
  86.   Exch
  87.   Exch $R0
  88. FunctionEnd
  89.  
  90. Section ""
  91.   Push "Tcp"
  92.   Push "80"
  93.   Call IsPortOpen
  94.   Pop $0 ; "open" or "closed" or anything else for error
  95.   ${If} $0 == "open"
  96.     DetailPrint "Apache running, stopping..."
  97.     !insertmacro SERVICE "stop" "apache2.2" ""
  98.     Pop $0
  99.     ${If} $0 == "true"
  100.       DetailPrint "Apache stopped"
  101.       loop:
  102.       Push "Tcp"
  103.       Push "80"
  104.       Call IsPortOpen
  105.       Pop $0 ; "open" or "closed" or anything else for error
  106.       ${If} $0 == "open"
  107.         Sleep 50
  108.         Goto loop
  109.       ${EndIf}
  110.       DetailPrint "Port 80 is closed."
  111.     ${Else}
  112.       DetailPrint "Failed to stop Apache"
  113.     ${EndIf}
  114.   ${Else}
  115.     DetailPrint "Port 80 is closed"
  116.   ${EndIf}
  117. Sectionend
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement