title: Script to generate N number of valid ip addresses?
body: <p>I am new to TCL and trying to learn by doing some simple scripting, I have taken upon to write a simple script which generates valid ip address from a given starting ip address.</p>
  
  <p>I have managed to write one but have run into two problems,</p>
  
  <ol>
  <li>The last octet has a zero getting added in front of the number that is 192.168.1.025</li>
  <li>When i specify the starting ip something like this 250.250.5.1 it fails to generate proper ips,</li>
  </ol>
  
  <p>Below is my code:</p>
  
  <pre><code>proc generate {start_addr total_addr} {
      if {$total_addr == 0} {return}
      regexp {([0-9]+\.)([0-9]+\.)([0-9]+\.)([0-9]+)} $start_addr match a b c d
      set filename "output.txt"
      set fileId [open $filename "a"]
      puts $fileId $a$b$c$d
      close $fileId
      while {$a&lt;255 &amp;&amp; $b &lt;255 &amp;&amp; $c &lt;255 &amp;&amp; $d &lt; 255 } {
          set d [expr {$d + 1}];
          set filename "output.txt"
          set fileId [open $filename "a"]
          puts $fileId $a$b$c$d
          close $fileId
          set total_addr [expr {$total_addr - 1}];
          if {$total_addr == 1} {return}
          if {$total_addr &gt; 1 &amp;&amp; $d == 255} {
              set c [expr {$c + 1}];
              set d 1
              set filename "output.txt"
              set fileId [open $filename "a"]
              puts $fileId $a$b$c$d
              close $fileId
              set total_addr [expr {$total_addr - 1}];
          }
          if {$total_addr &gt; 1 &amp;&amp; $c==255 &amp;&amp; $d == 255} {
              set b [expr {$b + 1}];
              set c 1
              set d 1
              set filename "output.txt"
              set fileId [open $filename "a"]
              puts $fileId $a$b$c$d
              close $fileId
              set total_addr [expr {$total_addr - 1}];
          }
          if {$total_addr &gt; 1 &amp;&amp; $b == 255 &amp;&amp; $c == 255 &amp;&amp; $d == 255} {
              set a [expr {$a + 1}];
              set b 1
              set c 1
              set d 1
              set filename "output.txt"
              set fileId [open $filename "a"]
              puts $fileId $a$b$c$d
              close $fileId
              set total_addr [expr {$total_addr - 1}];
          }
      }
  }
  
  
  flush stdout
  puts "Please enter the starting IPv4 address with . as delimiter EX: 1.1.1.1"
  set start_addr [gets stdin]
  regexp {([0-9]+\.)([0-9]+\.)([0-9]+\.)([0-9]+)} $start_addr match a b c d
  if {$a &lt;= 255 &amp; $b &lt;= 255 &amp; $c &lt;= 255 &amp; $d &lt;= 255} {
      puts "this is a valid ip address"
  } else {
      puts "this not a valid ip address"
  }
  flush stdout
  puts "Please enter the total number of IPv4 address EX: 1000"
  set total_addr [gets stdin]
  set result [generate $start_addr $total_addr]
  </code></pre>