My second post...

Author Message
drfusion

  • Total Posts : 2
  • Scores: 0
  • Reward points : 0
  • Joined: 12/21/2010
  • Status: offline
My second post... Tuesday, December 21, 2010 4:30 AM (permalink)
0
Sorry guys, I didn't paste the correct code in the third window for the gateway changer. Here it is...
 
  <html>
<head>
<title>Gateway Changer Utility</title>
<HTA:APPLICATION
  APPLICATIONNAME="Gateway Changer Utility"
  ID="MyHTMLapplication"
  maximizeButton="no"
  VERSION="1.01"/>
</head> <script language="VBScript"> '----------A few parts of this script are based on various scripts found on the Internet.
'----------Many thanks to those that contributed. 
On Error Resume Next Dim strComputerName, PINGFlag, strFileName, intCount, strNicSubnet, intDidChangeGW
Dim strGatewaySubnet, strGatewayMetric, strGateway, intProblems, intNewLog 
'----------program load event
Sub Window_OnLoad
 'set initial event tracker values 
 intProblems = 0
 intNewLog = 0
 intDidChangeGW = 0
 'center the app window
   CenterWindow
   btnClearServerList.Disabled = True
   btnApplyChanges.Disabled = True
   txtMessage.Value = Now & ": Utility started." & vbCrLf
    
End Sub   Sub LoadServerList()  Dim strFileName
 btnClearServerList.Disabled = False
 
  'open dialog to find the server list
  Set ObjFSO = CreateObject("UserAccounts.CommonDialog")
  ObjFSO.Filter = "Text Files|*.txt|All Files|*.*"
  ObjFSO.FilterIndex = 1
  ObjFSO.InitialDir = ""
  InitFSO1 = ObjFSO.ShowOpen
  
  'if user clicked the cancel button
  If InitFSO1 = False Then
      Exit Sub
  Else
      strFileName = ObjFSO.FileName
  End If
 
  'clear the server list
  For Each objOption in lstServers.Options
         objOption.RemoveNode
     Next   ForReading = 1
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     Set objFile = objFSO.OpenTextFile (strFileName, ForReading)
     
     'read the text file, line by line and add servers to list
     Do Until objFile.AtEndOfStream
         strLine = objFile.ReadLine
         Set objOption = Document.createElement("OPTION")
         objOption.Text = strLine
         objOption.Value = strLine
         lstServers.Add(objOption)
    Loop
    objFile.Close
 
 'count the servers in the list
 intCount = lstServers.Options.Length
 
 txtMessage.Value = txtMessage.Value & Now & ": " & intCount & " servers were loaded." & vbCrLf
 txtMessage.scrollTop = txtMessage.scrollHeight
 
 btnApplyChanges.Disabled = False End Sub 
Sub ClearServerList()
 
 'this clears the server list and logs it
 For Each objOption in lstServers.Options
     objOption.RemoveNode
 Next
 btnClearServerList.Disabled = True
 txtMessage.Value = txtMessage.Value & Now & ": Server list has been cleared." & vbCrLf
 txtMessage.scrollTop = txtMessage.scrollHeight
 btnApplyChanges.Disabled = True
 
End Sub 
Sub ApplyChanges()  'make sure text is entered in 1st gateway and metric
 If txtGateway1.Value = "" Or txtMetric1.Value = "" Then
  MsgBox "Please fill in at least the first gateway and metric to continue.", vbCritical, "Error"
  Exit Sub
 End If
 
 'make sure the 1st metric is a number
 If IsNumeric(txtMetric1.Value) = False Then
  MsgBox "Please enter a numeric value for the first metric.", vbCritical, "Error"
  Exit Sub
 End If
 
 'check if only 1 gateway is entered
 If txtGateway2.Value = "" Then
  
 Else
  If txtMetric2.Value = "" Then
   MsgBox "You must enter a numeric metric value for the second gateway.", vbCritical, "Error"
   Exit Sub
  End If
  
  If IsNumeric(txtMetric2.Value) = False Then
   MsgBox "Please enter a numeric value for the second metric.", vbCritical, "Error"
   Exit Sub
  End If
 End If
 
 'issue warning
 strMsg = "The gateways on all servers listed will be changed. Do you want to continue?"
 intYesNo = MsgBox(strMsg, 4, "Confirm Task")  If intYesNo = 6 Then
  If intNewLog = 1 Then
   'execute this to create a new log file if this is the second time user clicked "Apply Changes"
   'resets tracking values
   txtMessage.Value = ""
   intProblems = 0
   intDidChangeGW = 0
   ExecuteChanges
  Else
   'if this is the first time user clicked "Apply Changes"
   ExecuteChanges
  End If
    
   If intProblems = 1 Then
    'once the script runs, if any problems were encountered, run this
    MsgBox "Script completed successfully. However, there were some errors. Please check the log file. NOTE: Servers remaining in the list were NOT changed by this script.", vbInformation, "Script Completed"
    txtMessage.Value = txtMessage.Value & Now & ": Script completed successfully." & vbCrLf
    txtMessage.scrollTop = txtMessage.scrollHeight
    txtMessage.Value = txtMessage.Value & Now & ": Check the log file for errors." & vbCrLf
    txtMessage.scrollTop = txtMessage.scrollHeight
    'write txtMessage contents to the log file
    WriteLogFile
   Else
    'if no errors, run this
    MsgBox "Script completed successfully.", vbInformation, "Script Completed"
    txtMessage.Value = txtMessage.Value & Now & ": Script completed successfully." & vbCrLf
    'write txtMessage contents to the log file
    WriteLogFile
   End If
  
  
 Else
  'run this if user chooses "No" when asked to continue
  txtMessage.Value = txtMessage.Value & Now & ": Gateway changes aborted." & vbCrLf
  txtMessage.scrollTop = txtMessage.scrollHeight
  Exit Sub
 End If
 
End Sub 
Sub ExecuteChanges()
On Error Resume Next  Dim intPosition
 Dim strGatewayText
 Dim intNicPosition
 Dim strCName
 'set this to "1" to indicate that the next run will create a new log file
 intNewLog = 1
  
 'get subnet value from gateway IP address text
 strGatewayText = txtGateway1.Value
 intPosition = InStrRev(strGatewayText, ".", -1)
 strGatewaySubnet = Left(strGatewayText, intPosition - 1)
 
 
 'get subnet value from first server's IP address
 'test for connectivity of server
 'determine if each NIC matches the subnet of the gateway
 'change gateway of NICs that match
 
 For Each objItem in lstServers.Options
  'this code runs for each server in the list
  strCName = objItem.Value
  
  'write event to txtMessage
  txtMessage.Value = txtMessage.Value & Now & ": Attempting gateway change on - " & UCase(strCName) & vbCrLf
  txtMessage.scrollTop = txtMessage.scrollHeight
     
     'ping to see if server is online
     Set WshShell = CreateObject("WScript.Shell")
     PINGFlag = Not CBool(WshShell.run("ping -n 1 " & strCName,0,True))
   
  If PINGFlag = True Then
     
   txtMessage.Value = txtMessage.Value & Now & ": " & UCase(strCName) & " - ping successful." & vbCrLf
   txtMessage.scrollTop = txtMessage.scrollHeight
   txtMessage.Value = txtMessage.Value & Now & ": " & UCase(strCName) & " - Trying first NIC." & vbCrLf
   txtMessage.scrollTop = txtMessage.scrollHeight
   
   'server was online, now test if my user ID has access
   Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strCName & "\root\cimv2")
    If Err.Number <> 0 Then
     Err.Clear
     txtMessage.Value = txtMessage.Value & Now & ": " & UCase(strCName) & " - ACCESS DENIED." & vbCrLf
     txtMessage.scrollTop = txtMessage.scrollHeight
     txtMessage.Value = txtMessage.Value & Now & ": Trying next server..." & vbCrLf
     txtMessage.scrollTop = txtMessage.scrollHeight
     intProblems = 1
     Exit Sub
    End If  
   
   'since my ID has access, let's get started
   Set colNetCards = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
   
   'we will assign either 1 gateway or two gateways depending on users input
   If txtGateway2.Value = "" Then
    strGateway = Array(txtGateway1.Value) 
    strGatewayMetric = Array(txtMetric1.Value)
   Else
    strGateway = Array(txtGateway1.Value, txtGateway2.Value) 
    strGatewayMetric = Array(txtMetric1.Value, txtMetric2.Value)
   End If
    
    'let's loop through the NIC's and see if one matches the subnet of the gateways entered
    For Each objNetCard in colNetCards
     Dim strIP
     
     'convert array to string
     strIP = Join(objNetCard.IPAddress)
     
     'convert IP to just the class C subnet address
        intNicPosition = InStrRev(strIP, ".", -1)
        strNicSubnet = Left(strIP, intNicPosition - 1)
        
        'if this NIC's subnet matches the user entered subnet, make the change
        If strNicSubnet = strGatewaySubnet Then
         'the next line changes the gateway(s)
         errGateways = objNetCard.SetGateways(strGateway,strGatewayMetric)
         txtMessage.Value = txtMessage.Value & Now & ": " & UCase(strCName) & " - NIC gateway updated." & vbCrLf
         txtMessage.scrollTop = txtMessage.scrollHeight
         txtMessage.Value = txtMessage.Value & Now & ": " & UCase(strCName) & " - Trying next NIC." & vbCrLf
         txtMessage.scrollTop = txtMessage.scrollHeight
         intDidChangeGW = 1
        Else
         'the subnet didn't match, write that to txtMessage
         txtMessage.Value = txtMessage.Value & Now & ": " & UCase(strCName) & " - Subnets don't match." & vbCrLf
         txtMessage.scrollTop = txtMessage.scrollHeight
         txtMessage.Value = txtMessage.Value & Now & ": " & UCase(strCName) & " - Trying next NIC..." & vbCrLf
         txtMessage.scrollTop = txtMessage.scrollHeight
        End If
     
    Next
    'finished looping the NIC's
    txtMessage.Value = txtMessage.Value & Now & ": " & UCase(strCName) & " - No more NICs installed." & vbCrLf
    txtMessage.scrollTop = txtMessage.scrollHeight  
   
  Else
   'ping was not successful
   txtMessage.Value = txtMessage.Value & Now & ": " & UCase(strCName) & " - ping not successful." & vbCrLf
   txtMessage.scrollTop = txtMessage.scrollHeight
   'try the next server in the list
   txtMessage.Value = txtMessage.Value & Now & ": Trying next server..." & vbCrLf
   txtMessage.scrollTop = txtMessage.scrollHeight
   intProblems = 1
  End If
  
  'if a gateway change was made on this server, remove it from the list
  If intDidChangeGW = 1 Then
   For Each objOption in lstServers.Options
          If objOption.Value = strCName Then
           objOption.RemoveNode
          End If
      Next
      'reset the gateway change tracker
      intDidChangeGW = 0
     End If  Next
 
End Sub 
'----------center the main program window
Sub CenterWindow  On Error Resume Next
 
 strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor")
     For Each objItem in colItems
         intHorizontal = objItem.ScreenWidth
         intVertical = objItem.ScreenHeight
     Next
    intLeft = (intHorizontal - 575) / 2
    intTop = (intVertical - 725) / 2
    window.resizeTo 575,725
    window.moveTo intLeft, intTop End Sub 
Sub CloseApp()
 
 Self.close
 
 
End Sub 
Sub WriteLogFile()
 Dim strFileName
 Dim strYear
 Dim strMonth
 Dim strDay
 Dim strHour
 Dim strMinute
 Dim strSecond
 
 'pull together the current date and time
 strYear = Year(Now)
 strMonth = Month(Now)
 strDay = Day(Now)
 strHour = Hour(Now)
 strMinute = Minute(Now)
 strSecond = Second(Now)
 
 'add the current date and time to the log file name to make it unique
 strFileName = strYear & strMonth & strDay & strHour & strMinute & strSecond & "-gateway.log"
 
 Const ForAppending = 8  Set objFSO = CreateObject("Scripting.FileSystemObject")
 Set objTextFile = objFSO.OpenTextFile (strFileName, ForAppending, True)
 
  
    objTextFile.WriteLine(txtMessage.Value)  objTextFile.Close End Sub 
</script> <body bgcolor="#ECE9D8">
<basefont size="2" color="black" face="arial"> <center>
<table cellspacing=0 cellpadding=3 bgcolor="black">
 <tr>
  <td bgcolor="#ECE9D8">Gateway Changer Utility</td>
 </tr>
</table>
</center><br><br> <center>
<table cellspacing=0 cellpadding=3 bgcolor="black">
 <tr>
  <td bgcolor="#ECE9D8" VALIGN="top"> <center>  
<table cellspacing=0 cellpadding=3 bgcolor="black">
 <tr>
  <td bgcolor="#ECE9D8"><input type="button" name="btnLoadServerList" id="btnLoadServerList" value="Load Server List" onclick="LoadServerList" style="width:150px;"</td>
 </tr>
 <tr>
  <td bgcolor="#ECE9D8"><br><input type="button" name="btnClearServerList" id="btnClearServerList" value="Clear Server List" onclick="ClearServerList" style="width:150px;"</td>
 </tr>
 <tr>
  <td bgcolor="#ECE9D8"><font size=2><br>Primary Gtwy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Metric</font></td>
 </tr>
 <tr>
  <td bgcolor="#ECE9D8"><input type="text" name="txtGateway1" id="txtGateway1" size="15">&nbsp;&nbsp;&nbsp;<input type="text" name="txtMetric1" id="txtMetric1" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
 </tr>
   <tr>
  <td bgcolor="#ECE9D8"><font size=2>Secondary Gtwy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Metric</font></td>
 </tr>
 <tr>
  <td bgcolor="#ECE9D8"><input type="text" name="txtGateway2" id="txtGateway2" size="15">&nbsp;&nbsp;&nbsp;<input type="text" name="txtMetric2" id="txtMetric2" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
 </tr>
</table>
</center>  
 
  </td>
  <td bgcolor="#ECE9D8" VALIGN="top"><select style="width:275px" name="lstServers" id="lstServers" size=15></td>
 </tr>
</table>
</center><br> <center>
<table cellspacing=0 cellpadding=3 bgcolor="black">
 <tr>
  <td bgcolor="#ECE9D8"><input type="button" name="btnApplyChanges" id="btnApplyChanges" value="Apply Changes" onclick="ApplyChanges" style="width:150px;"</td>
  <td bgcolor="#ECE9D8">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" name="btnClose" id="btnClose" value="Close" onclick="CloseApp" style="width:150px;"</td>
 </tr>
</table>
</center><br>   <center>
<table cellspacing=0 cellpadding=4 bgcolor="black">
 <tr>
  <td bgcolor="ECE9D8"><font size=2>Event Log:</font></td>
 </tr>
 <tr>
  <td bgcolor="ECE9D8"><textarea style="background-color:ECE9D8" name="txtMessage" id="txtMessage" rows="13" cols="28" style="width:475px" readonly></textarea></td>
 </tr>
</table>
</center> </basefont>   </body>
</html>   

 
thanks
<message edited by drfusion on Tuesday, December 21, 2010 9:50 AM>
 
#1

    Online Bookmarks Sharing: Share/Bookmark

    Jump to:

    Current active users

    There are 0 members and 1 guests.

    Icon Legend and Permission

    • New Messages
    • No New Messages
    • Hot Topic w/ New Messages
    • Hot Topic w/o New Messages
    • Locked w/ New Messages
    • Locked w/o New Messages
    • Read Message
    • Post New Thread
    • Reply to message
    • Post New Poll
    • Submit Vote
    • Post reward post
    • Delete my own posts
    • Delete my own threads
    • Rate post

    2000-2012 ASPPlayground.NET Forum Version 3.9