-1

I'm trying to run a tcl script on a Cisco router in GNS3;

SanJose1(tcl)#foreach address {
+>192.168.1.1
+>192.168.1.2
+>172.16.224.5
+>172.16.224.6
+>192.168.72.1
+>172.16.224.2
+>172.16.224.1
+>10.2.1.1
+>10.2.2.1
+>} { ping $address }

When I do this, nothing happens, no pings, no errors, it just returns to the router prompt. Am I running the code incorrectly? If it's correct then I can look into problems with GNS3

Thanks!

bordeltabernacle
  • 1,603
  • 5
  • 24
  • 46

1 Answers1

0

The foreach, as you write it in your question, appears correct. (If there was one more argument, you'd get the message from the question title, but your code sample shouldn't generate that.) It's more likely that the ping you are calling doesn't write anything visible, and instead just produces the result as a string. That's pretty common with Tcl code. I'd expect something like this to work, assuming my diagnosis is correct:

foreach address {
    192.168.1.1
    192.168.1.2
    172.16.224.5
    172.16.224.6
    192.168.72.1
    172.16.224.2
    172.16.224.1
    10.2.1.1
    10.2.2.1
} {
    puts [ping $address]
}

I've omitted the prompts and added indents and a few more newlines for clarity. If ping is really an external command, use exec ping $address instead (it's considered bad form to rely on the unknown command handling to bring external commands into your code, especially as it is disabled in scripted mode.) With an external command, you might also want to pass the -c option with a number so as to limit the number of requests used to a fairly small number, perhaps:

puts [exec ping -c 5 $address]

Be aware that the set of commands supported by a Tcl interpreter can be changed completely (by design) and that the commands listed in the documentation are really just a pre-supplied standard library. When Tcl is embedded in an environment like a router, it is quite possible that things have been changed around a fair bit. (It's not like it's hard for the router vendor to do it.) That means that surprising differences in commands most certainly can occur, and that you should check the documentation you've got and proceed with a little thought instead of just blindly trusting what I wrote.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • Donal, thankyou!! Looks like you are right, adding the 'puts' command made the ping results show up. I'm very new to coding (VERY), so I don't fully understand why, or everything you've mentioned, but I will endeavour to. For now, you've certainly helped my networking studies. I really appreciate it! Rob – bordeltabernacle Jan 30 '14 at 20:42