Announcement

Collapse
No announcement yet.

Network Connectivity Script Help.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Network Connectivity Script Help.

    Help me out here, I'm having a 'Case of the Mondays' and I've got a lot on my plate... Can someone help me out with this...

    Basically, I want my batch script to do this:

    1. Start DHCP Client
    2. Ping Our Default Gateway
    IF PING FAILS KILL DHCP, and Send a Message.
    IF PING SUCCESSFUL SEND MESSAGE

    Below is my script: (Adjust and add code as needed)

    @echo off
    NET START "DHCP Client"
    ping xx.xx.xx.x
    IF errorlevel 1 NET STOP "DHCP Client"
    PAUSE

    ---------------------------------------------------------------------

    Thanks

  • #2
    Re: Network Connectivity Script Help.

    If I understand right, your problem is sending the e-mail?
    I am using BMAIL to send SMTP messages from my scripts. It's small, free and cute. I am not sure that it's the best, but, hey, it does the job excellent for me!

    I would do it this way:
    Code:
    @echo off
    NET START "DHCP Client"
    ping xx.xx.xx.x
    IF errorlevel 1 goto bad
    goto ok
    :bad
    NET STOP "DHCP Client"
    [send message BAD using bmail]
    goto end
    :ok
    [send message OK using bmail]
    BTW, using CLI commands to write a batch file isn't the best way. Some sort of script will be better, but I lack the knowledge to assist you in this direction. However, there are a lot of great guys here that can write such a script in a blink of an eye... Ahhhh... Guys? Anyone?
    Hope this helped.

    Sorin Solomon


    In order to succeed, your desire for success should be greater than your fear of failure.
    -

    Comment


    • #3
      Re: Network Connectivity Script Help.

      Here's a script that does pretty much the same thing. I wrote this using examples on this site plus those from MSDN. It took me about 15 min to look over the examples and 20 min to write and test it.

      If you can program you can write a script it just takes a little time to lookup what each method, object, function, etc. does.

      Edit - Changed script to start service again after 5 min and try again.

      Code:
      On Error Resume Next
      
      Const mstrRemote = "DCName"
      Const mstrLocal = "." ' use "." for local computer
      Const mstrService = "DHCP"
      
      While (True)
        If DCIsPingable Then 'Ping succeeded
          wscript.Sleep(10000) 'Sleep for 10 seconds [10000 milliseconds]
        Else 'Ping failed
          wscript.Sleep(10000) 'Wait 10 seconds then try again
            If DCIsPingable Then 
              wscript.Sleep(10000)        
            Else
              DisableNetworkAccess
              wscript.Sleep(300000) 'Wait 5 min
              EnableNetworkAccess 'Enable Access Then Check for DC
              wscript.Sleep(60000) 'Wait 1 min to acquire IP if not static
            End If 
        End If
      Wend
      
      Private Function DCIsPingable
      Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & mstrLocal & "\root\cimv2")
        Set colItems = objWMIService.ExecQuery("Select * from Win32_PingStatus Where Address = '" & mstrRemote & "'")
        For Each objItem in colItems
          If objItem.StatusCode = 0 Then 
            DCIsPingable = True
          else
            DCIsPingable = False      
          End If
        Next
      End Function
      
      Private Sub DisableNetworkAccess
      Set ObjWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & mstrLocal & "\root\cimv2") 
      Set ObjService = objWMIService.Get("Win32_Service.Name='" & mstrService & "'")  
      If ObjService.State = "Running" then
        ObjService.StopService 
      End if 
      End Sub
      
      Private Sub EnableNetworkAccess
      Set ObjWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & mstrLocal & "\root\cimv2") 
      Set ObjService = objWMIService.Get("Win32_Service.Name='" & mstrService & "'")  
      If Not ObjService.State = "Running" then
        ObjService.StartService 
      End if 
      End Sub
      Last edited by ahinson; 21st June 2007, 03:43. Reason: Fixed Typo
      Andrew

      ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

      Comment


      • #4
        Re: Network Connectivity Script Help.

        Hey Thanks, I'm going to give both of them a shot, I like the idea of the VBS script, because it wouldn't require user intervention, I will have to change it around so it reflects my Domain controller correct? I'll give it a shot. Also, will this Physically stop the DHCP Service, or what service will it stop?

        Comment


        • #5
          Re: Network Connectivity Script Help.

          Originally posted by U-Phorik View Post
          Hey Thanks, I'm going to give both of them a shot, I like the idea of the VBS script, because it wouldn't require user intervention, I will have to change it around so it reflects my Domain controller correct? I'll give it a shot. Also, will this Physically stop the DHCP Service, or what service will it stop?
          Yes you need to change mstrRemote = "DCName" to you DC netbios name or IP address.
          It will stop the DHCP client service. If you can use the service name or tag. i.e. DHCP or DHCP Client.
          Andrew

          ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

          Comment


          • #6
            Re: Network Connectivity Script Help.

            Ok, maybe I'm doing something wrong, but where it says 'mstrRemote = ' I entered in the name of a false domain controller in there for testing purposes. I then ran the script however, I wasn't able to physically see the DHCP Service Stop; am I doing something wrong? Thanks.

            Comment


            • #7
              Re: Network Connectivity Script Help.

              Originally posted by U-Phorik View Post
              Ok, maybe I'm doing something wrong, but where it says 'mstrRemote = ' I entered in the name of a false domain controller in there for testing purposes. I then ran the script however, I wasn't able to physically see the DHCP Service Stop; am I doing something wrong? Thanks.
              I fixed a typo in the script. I'm testing right now. I'll post any fixes to my original post.

              Edit - Tested and worked on Vista when run as Administrator.
              Make sure that DHCP is the name of the service in WinXP/2000. Run sc query | find "DHCP" to find the name of the service.
              Last edited by ahinson; 21st June 2007, 04:09.
              Andrew

              ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

              Comment


              • #8
                Re: Network Connectivity Script Help.

                Hey,

                Thanks for the continued help. I still cannot get it to work here, and I know I have access to kill the DHCP Client myself. It is definitely called "DHCP Client" on the PC. So I'm at a loss as to what it may be. I am going to attempt to run it on a PC off the network when I get home and see what happens, maybe I'm missing something I ran through it looking at the code, and from what I know/can see it looks good. However I'm no genius at this stuff yet, still learning it here and there. I'll continue to tool around with it. I'm happy to hear you got it running in Vista, so it tells me it's possibly something small. If you find anything further let me know. Thx!!

                Comment

                Working...
                X