Nodes creation in NS2

Add the following code to the new file and named it with the extension of tcl. i.e. filename.tcl . To create the file go to the terminal.
gedit filename.tcl

This above command will open a file with the name filename.tcl. Add the following code to the file and save the file.

Now, in order  to run the file go to the Terminal and write the following command.

ns filename.tcl


#Create a simulator object
set ns [new Simulator]

#Open the NAM trace file

set nf [open o.nam w]
$ns namtrace-all $nf

# create nodes

set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n7 [$ns node]
set n8 [$ns node]
set n10 [$ns node]

#Create links between the nodes

$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms RED
$ns duplex-link $n2 $n3 1.7Mb 20ms RED
$ns duplex-link $n4 $n5 2Mb 10ms RED
$ns duplex-link $n2 $n8 2Mb 10ms RED
$ns duplex-link $n4 $n10 2Mb 5ms DropTail

#Define a 'finish' procedure

proc finish {} {
        global ns nf
        $ns flush-trace
        #Close the NAM trace file
        close $nf
        #Execute NAM on the trace file
        exec nam o.nam &
        exit 0

#Call the finish procedure

$ns at 1.0 "finish"

#Run the simulation

$ns run

The following output will be generated after running the code.
Type the following in terminal.

ns filename.tcl



  1. please help us in executing this program in cygwin window .. actually i am not aware of the correct command instruction

    1. sorry for late reply. though there are ways available to execute it using cygwin, but they are not reliable, i wouldnt recommend using those, ways. There are easier ways to install ns2 on linux based environment just go through the following tutorials.

  2. hi sir,

    can i have the tcl syntax to know the node position and energy...,

    for ex: position of node 1 is: x:____ y:_____
    energy of node 1 is: ___________

    for all or for the required nodes...,

  3. sir ha bht talented...par install abitak kch ni kia

  4. sir ke apke knowledge ne to anni dal di ha mjhe bht pasnd aye han apke notes

    1. sir ha bht payare..>3


  5. sir a msg is displayed while executing the progam
    can't read "val(x)": no such variable
    while executing
    "$ns namtrace-all-wireless $namtrace $val(x) $val(y)"
    (file "wireless1.tcl" line 15)
    can help me.....

    1. me 2 same problem

    2. I am getting an error " can't read "i":no such variable while executing " for{set i 0}{$i<$val(nn)}{incr i}{"

  6. did you have any experience on MANETs? I need code for this network. I'm working on wormhole attacks on MANETs. can you give me some suggestion how to do it? if please send email to

  7. I tried installing TKN's 802.11e EDCA on ns 2.35(ubuntu 13.04)...followed all the steps in read me file..but on executing the example file getting error
    "invalid command name "Mac/802_11e".

    Please help.

  8. i need code for this working on blackhole attacks on manet .so can u give some suggestion? please

  9. hello everyone
    please someone tell me how to change traffic like frst i have find results using CBR traffic now i want to use PARETO bt i dnt knw how to change traffic and where, if someone knws then please help me ..........thnx

  10. any one can help me the ns2 source code or tcl,awk and xgraph for dynamic redundancy management of multipath routing using heterogenous wireless sensor networks.

  11. Sir plz help in creating a wormhole code for the network in ns2

  12. Sir plz help in creating a wormhole code for the network in ns2

  13. how to make a node execute some command and send the result to other node..? can someone help me please.. It'll be very helpful

  14. why I got an error on this ? help me please ! -_-

    set nf [open o.nam w]
    $ns namtrace-all $nf

  15. ns: finish: couldn't execute "nam": no such file or directory
    while executing
    "exec nam o.nam &"
    (procedure "finish" line 7)
    invoked from within

    ^^ anyone can help me on this error ??

  16. sir please send me tcl file of wormhole attack in my email

  17. how can I create node with loop.

  18. thank you so much for you sharing this good infomations
    thank you for the reply.

    goldenslot casino
    gclub casino