Sign in to follow this  
Followers 0
wuthless

_FileReadToArray - Multiple entries to be processed

4 posts in this topic

Hello All,

First Off... I just started using AutoIT and Love it! It's made some of my work a lot easier. Thanks autoit!

Secondly, I'm a newbie so please be gentle.

Now on to my issue - I'm trying to accomplish the folllowing

- Read file1 and create and array

- Take each entry of the array and process them

- Take the processed entries and write them to file2.

Basically, I'm trying to take a list of IP addresses and subnet masks (file1) and convert them into a router configuration and output them into a file (File2).

The IP address conversion is thanks to somebody on the AutoIt forums. Many thanks whoever wrote it

My code is below

; This script will read a file and put it into an array

#include <Array.au3>
#include <file.au3>

Opt("WinTitleMatchMode", 2)  
$testfile = ("d:\scripts\ospf.txt")
$net_file = ("d:\scripts\iptest.txt")

; Deletes the output file if it already exists
If FileExists("d:\scripts\ospf.txt") Then
  FileDelete($testfile)
EndIf

$rtr_name = InputBox ("Router Name", "Which device are you working on?", "Some Network Device","" ,225, 100, 400, 300)
if $rtr_name = @error -1 Then;If the cancel button is pushed it will cancel the program
    MsgBox(0, "Exit Program", "OK.  Bye!")
    Exit
EndIf

; Creates the file and puts 'conf t & router ospf' in the config
FileWrite($testfile, "!" & @CR & "! " & $rtr_name & @CR & "!" & @CR & "conf t" & @CR & "!" & @CR & "router ospf 1" & @CR)

Dim $ip_netmask
; Displays Array before it's processed
_FileReadToArray($net_file, $ip_netmask)
_ArrayDisplay($ip_netmask, "IP Netmask")

; Breaks input into 2 different arrays (iparray and netmaskarray)
$ip = Stringsplit($ip_netmask[1], " ")

; 2 arrays created from the user input

$iparray = StringSplit($ip[1], ".")
$netmaskarray = StringSplit($ip[2], ".")

Dim $subnetaddarray[5]
Dim $invmaskarray[5]
Dim $broadcastaddarray[5]

For $i = 1 To $iparray[0]
   $subnetaddarray[$i] = BitAND($iparray[$i], $netmaskarray[$i])
   $invmaskarray[$i] = BitNOT($netmaskarray[$i] - 256)
   $broadcastaddarray[$i] = BitOR($subnetaddarray[$i], $invmaskarray[$i])
Next

$broadcastadd = $broadcastaddarray[1] & "." & $broadcastaddarray[2] & "." & $broadcastaddarray[3] & "." & $broadcastaddarray[4]
$subnetadd = $subnetaddarray[1] & "." & $subnetaddarray[2] & "." & $subnetaddarray[3] & "." & $subnetaddarray[4]
$invmask = $invmaskarray[1] & "." & $invmaskarray[2] & "." & $invmaskarray[3] & "." & $invmaskarray[4]
$iprange = $subnetaddarray[1] & "." & $subnetaddarray[2] & "." & $subnetaddarray[3] & "." & $subnetaddarray[4] + 1 & "-" & $broadcastaddarray[1] & "." & $broadcastaddarray[2] & "." & $broadcastaddarray[3] & "." & $broadcastaddarray[4] - 1
$hosts = ($invmaskarray[4] + 1) * ($invmaskarray[3] + 1) * ($invmaskarray[2] + 1) - 2

; Writes output to a file
FileWrite($testfile, " network " & $subnetadd & " " & $invmask & " " & "area 0" & @CR)

Exit

I am able to get the first entry to convert, but I can't seem to repeat the process until it gets to the last entry. Any help would be great.

Thanks!

Wuthless

Share this post


Link to post
Share on other sites



EDIT: ... Working on a rewrite, will post in a second.


AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)

Share this post


Link to post
Share on other sites

Alright, try this:

#include <array.au3>
#include <file.au3>

Global $a_ListTemp, $s_IPListPath, $a_IP[1], $a_Mask[1]

$s_IPListPath = FileOpenDialog("IP/Mask List", @ScriptDir, "All (*.*)")
_FileReadToArray($s_IPListPath, $a_ListTemp) ; read the file to an array

For $i = 1 To UBound($a_ListTemp) - 1 ; for each item in the array, lets split IP address and mask into their own arrays
    $a_SplitResult = StringSplit($a_ListTemp[$i], " ")
    _ArrayAdd($a_IP, $a_SplitResult[1])
    _ArrayAdd($a_Mask, $a_SplitResult[2])
Next

$rtr_name = InputBox ("Router Name", "Which device are you working on?", "Some Network Device","" ,225, 100, 400, 300)
if $rtr_name = @error -1 Then;If the cancel button is pushed it will cancel the program
    MsgBox(0, "Exit Program", "OK.  Bye!")
    Exit
EndIf

$s_ConfigPath = FileSaveDialog("Save Router Config", @ScriptDir, "All (*.*)")
FileWrite($s_ConfigPath, "!" & @CR & "! " & $rtr_name & @CR & "!" & @CR & "conf t" & @CR & "!" & @CR & "router ospf 1" & @CR) ; Create the file

For $i = 1 To UBound($a_IP) - 1 ; for each ip/mask lets get the inverse and subnetted ip and write them to our config file.
$a_SubnetResult = _SubNet($a_IP[$i], $a_Mask[$i])
FileWrite($s_ConfigPath, " network " & $a_SubnetResult[1]& " " & $a_SubnetResult[3] & " area 0"&@CR)
Next

MsgBox(0, "Completed!", "The config file has been created."&@CR&$s_ConfigPath)


;$a_IPList = StringRegExp($s_IPListRaw, "([0-9]{1-3}.[0-9]{1-3}.[0-9]{1-3}.[0-9]{1-3})", 3)


Func _Subnet($sIp, $sNetmask)
    Dim $netmaskbinary
    Dim $subnetaddarray[5]
    Dim $invmaskarray[5]
    Dim $broadcastaddarray[5]
    Dim $sSubnetinfo[7]
    Dim $subnetadd
    Dim $invmask
    Dim $broadcastadd
    ; Reads IP and Netmask to an array
    $iparray = StringSplit($sIp, ".")
    $netmaskarray = StringSplit($sNetmask, ".")
    ; Validates IP address
    For $i = 1 To 4
        If Number($iparray[$i]) < 0 Or Number($iparray[$i]) > 255 Then
            SetError(1)
            Return (-1)
        EndIf
    Next
    ; Converts netmask into a decimal
    $netmaskdec = ($netmaskarray[1] * 16777216) + ($netmaskarray[2] * 65536) + ($netmaskarray[3] * 256) + $netmaskarray[4]
    ; Converts decimal netmask into binary (ex. 11111111111111111100000000000000)
    While $netmaskdec <> 0
        $binmod = Mod($netmaskdec, 2)
        $netmaskbinary = $binmod & $netmaskbinary
        $netmaskdec = Int($netmaskdec / 2)
    WEnd
    ; Determines the "slash" value of the netmask
    $maskslash = StringInStr($netmaskbinary, "0", 1) - 1
    ; Validates the "slash" value and netmask value
    If StringInStr(StringRight($netmaskbinary, 32 - $maskslash), "1") Then
        If $netmaskarray[4] = "255" Then
            $maskslash = 32
        Else
            SetError(1)
            Return (-1)
        EndIf
    EndIf
    ; Creates arrays conatining subnet address, wilcard, and broadcast addresses
    For $i = 1 To $iparray[0]
        $subnetaddarray[$i] = BitAND($iparray[$i], $netmaskarray[$i])
        $invmaskarray[$i] = BitNOT($netmaskarray[$i] - 256)
        $broadcastaddarray[$i] = BitOR($subnetaddarray[$i], $invmaskarray[$i])
    Next
    ; Creates strings conatining subnet address, wilcard, and broadcast addresses
    $subnetadd = $subnetaddarray[1] & "." & $subnetaddarray[2] & "." & $subnetaddarray[3] & "." & $subnetaddarray[4]
    $invmask = $invmaskarray[1] & "." & $invmaskarray[2] & "." & $invmaskarray[3] & "." & $invmaskarray[4]
    $broadcastadd = $broadcastaddarray[1] & "." & $broadcastaddarray[2] & "." & $broadcastaddarray[3] & "." & $broadcastaddarray[4]
    If $maskslash = 32 Then
        $iprange = $iparray[1] & "." & $iparray[2] & "." & $iparray[3] & "." & $iparray[4]
        $hosts = 1
    Else
        ; Determines the IP range for this subnet
        $iprange = $subnetaddarray[1] & "." & $subnetaddarray[2] & "." & $subnetaddarray[3] & "." & $subnetaddarray[4] + 1 & _
                "-" & $broadcastaddarray[1] & "." & $broadcastaddarray[2] & "." & $broadcastaddarray[3] & "." & $broadcastaddarray[4] - 1
        ; Calculates number of available hosts on this subnet
        $hosts = ($invmaskarray[4] + 1) * ($invmaskarray[3] + 1) * ($invmaskarray[2] + 1) * ($invmaskarray[1] + 1) - 2
    EndIf
    $sSubnetinfo[1] = $subnetadd
    $sSubnetinfo[2] = $broadcastadd
    $sSubnetinfo[3] = $invmask
    $sSubnetinfo[4] = $iprange
    $sSubnetinfo[5] = $hosts
    $sSubnetinfo[6] = $maskslash
    Return ($sSubnetinfo)
EndFunc   ;==>_Subnet

AutoIt Scripts:Aimbot: Proof of Concept - PixelSearching Aimbot with several search/autoshoot/lock-on techniques.Sliding Toolbar - Add a nice Sliding Toolbar to your next script. Click the link to see an animation of it in action!FontInfo UDF - Get list of system fonts, or search to see if a particular font is installed.Get Extended Property UDF - Retrieve a files extended properties (e.g., video/image dimensions, file version, bitrate of song/video, etc)

Share this post


Link to post
Share on other sites

This is exactly what I wanted. Thanks Simucal... you're my HERO!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0