Skip to content

TCL Script for My NS-2 Project

This is my simple tcl script for our project research by using NS-2.34. In this project, we are going to combine 2 protocols, AODV+ and R-AODV,  our proposed protocol called AODV-UI.
Here is the tcl script.


set opt(namfile)         nam_aodv_out.nam
set opt(tracefile)
set val(chan)    Channel/WirelessChannel;    #channel type
set val(prop)    Propagation/TwoRayGround;   #radio-propagation model
set val(netif)   Phy/WirelessPhy;            #network interface type
set val(mac)     Mac/802_11;                 #MAC type
set val(ifq)     Queue/DropTail/PriQueue;    #interface queue type
set val(ifqlen)  50;                         #max nbr of packets in ifq
set val(ll)      LL;                         #link layer type
set val(ant)     Antenna/OmniAntenna;        #antenna type
set val(adhocRP) AODV;                       #routing protocol
set val(x)       1000;                        #x dimension of the topography
set val(y)       800;                        #y dimension of the topography
set val(stop)    600.0;                       #simulation time
set val(mobility) "mobil5_4"
set val(start-src)     1
set val(stop-src)     600
set nbrOfWirelessNodes   5
set nbrOfWiredNodes      4
set nbrOfGateways        2
#set val(gw_discovery) reactive;                #gateway discovery method

Download the full tcl script here.

63 thoughts on “TCL Script for My NS-2 Project”

  1. I can see you’re an expert on TCL Script for My NS-2 Project | Abdusy Syarif! I am starting up a website in the near future, and your post can be very useful for me. Thanks for all your input. With very best wishes x

  2. thanks a lot sir ,
    but we have problem while executing progrmm
    pls help me
    ravi@ravi-desktop:~$ ns aodv3.tcl
    num_nodes is set 7
    couldn’t read file “mobil5_4”: no such file or directory
    while executing
    “source.orig mobil5_4”
    (“uplevel” body line 1)
    invoked from within
    “uplevel source.orig [list $fileName]”
    invoked from within
    “if [$instance_ is_http_url $fileName] {
    set buffer [$instance_ read_url $fileName]
    uplevel eval $buffer
    } else {
    uplevel source.orig [list $fileName]
    (procedure “source” line 8)
    invoked from within
    “source $val(mobility)”
    (file “aodv3.tcl” line 130)
    thank you

      1. i ran the setdest function in ../cmu-scen-gen/setdest
        ./setdest -n 5 -p 2.0 -s 10.0 -t 100 -x 100 – y 150 > mobil5_4
        i got the following error:
        assertion “speed != 0.0” failed: file “”, line 595
        Aborted ( core dumped)
        plzz help

        1. hi
          can you tell me how to link files generated in setdest function with tcl script
          as i am getting same error as ravi above

  3. how to run setdest function, i installed ns2.34 in /user/local/
    so when i tried running .sh file getting errors like
    [root@localhost pr]# ./
    ‘movement’ is a directory
    /user/local/ns-allinone-2.23/ns-2.23/indep-utils/cmu-scen-gen/setdest/setdest does not exist or is not executable
    [root@localhost pr]#
    whats the solution for this

  4. hey i have been following your blog lately, you have done some great work in NS2. i am too working on it and i need some help. there is an energy model available in ns2, do u know the tcl script that i should run to get the results from it

  5. hi,
    can you tell me how to write tcl script for aodv that includes node movement or mobility,and cbr/tcp traffic and should show data transfer between source and destination node…

  6. #Create a simulator object
    set ns [new Simulator]
    #Open the nam trace file
    set nf [open out.nam w]
    $ns namtrace-all $nf
    #Define a ‘finish’ procedure
    proc finish {} {
    global ns nf
    $ns flush-trace
    #Close the trace file
    close $nf
    #Execute nam on the trace file
    exec nam out.nam &
    exit 0
    #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 n6 [$ns node]
    set n7 [$ns node]
    #Create a duplex link between the nodes
    $ns duplex-link $n0 $n1 1Mb 10ms DropTail
    $ns duplex-link $n1 $n2 1Mb 10ms DropTail
    $ns duplex-link $n2 $n3 1Mb 10ms DropTail
    $ns duplex-link $n0 $n4 1Mb 10ms DropTail
    $ns duplex-link $n0 $n5 1Mb 10ms DropTail
    $ns duplex-link $n5 $n6 1Mb 10ms DropTail
    $ns duplex-link $n6 $n7 1Mb 10ms DropTail
    $ns duplex-link $n7 $n3 1Mb 10ms DropTail
    $ns duplex-link $n4 $n5 1Mb 10ms DropTail
    $ns duplex-link-op $n0 $n1 orient right-down
    $ns duplex-link-op $n1 $n2 orient right
    $ns duplex-link-op $n2 $n3 orient right-up
    $ns duplex-link-op $n0 $n4 orient right
    $ns duplex-link-op $n0 $n5 orient right-up
    $ns duplex-link-op $n5 $n6 orient right-
    $ns duplex-link-op $n6 $n7 orient right-down
    $ns duplex-link-op $n7 $n3 orient right-down
    $ns duplex-link-op $n4 $n5 orient up
    #Create a UDP agent and attach it to node n0
    set udp0 [new Agent/UDP]
    $ns attach-agent $n0 $udp0
    # Create a CBR traffic source and attach it to udp0
    set cbr0 [new Application/Traffic/CBR]
    $cbr0 set packetSize_ 500
    $cbr0 set interval_ 0.005
    $cbr0 attach-agent $udp0
    #Create a UDP agent and attach it to node n1
    set udp1 [new Agent/UDP]
    $ns attach-agent $n1 $udp1
    # Create a CBR traffic source and attach it to udp1
    set cbr1 [new Application/Traffic/CBR]
    $cbr1 set packetSize_ 500
    $cbr1 set interval_ 0.005
    $cbr1 attach-agent $udp1
    set null0 [new Agent/Null]
    $ns attach-agent $n3 $null0
    $ns connect $udp0 $null0
    $ns connect $udp1 $null0
    $ns at 0.5 “$cbr0 start”
    $ns at 1.0 “$cbr1 start”
    $ns at 4.0 “$cbr1 stop”
    $ns at 4.5 “$cbr0 stop”
    $udp0 set class_ 1
    $udp1 set class_ 2
    $ns color 1 Blue
    $ns color 2 Red
    $ns color 2 Red
    #Call the finish procedure after 5 seconds of simulation time
    $ns at 50.0 “finish”
    #Run the simulation
    $ns run
    i have written this code…the route it follows is from 0 to 3 via node 1.
    now i want it send packets using the route 0 to 3 via node 5
    (i.e 0-5-6-7-3) do i do that..plzz help

  7. i can’t see any option of R-AODV, is it done in AODV or in the .cc file?
    And how the mechanism of switching between protocol?

      1. can you pass me the .cc code? is it two different file ( and and somehow you calling from one another.or it’s done in alone?
        appreciate if you can email me the code…i’m doing something similiar in OLSR+DTN protocol..

      2. can you send me the .cc file? do you:
        1. calling each other from and file (two seperate file)?
        2. Putting the mechanism in file?
        I want to know the way combining two routing protocols together..

  8. Hi everyone here!
    I need your help in my graduation project, which is about “GNDA: Detecing good neighbor nodes in ad hoc routing protocol”. I need the implementaion code (TCL) in ns-2, which is based on AODV. Any help will be appreciated!

  9. hi sir may i know how to implement mobile 5.4 package in ns2.34 please let me to know this detail .. reply me as soon as possible

  10. Hi! I need your urgent help. plz! help me to finish my GP. I need to implement the attached flowchart in AODV using ns2.34.

  11. I have to create a Gateway Node which will act as a bridge between two ad-hoc nodes whereas both are running different protocols. I will appreciate if you guide i.e how to achieve/create this type of Gateway node. Can we do in tcl script or have to create in C++ ?. Is there any implementation of Gateway available which can be modified to solve the above problem.
    Will really appreciate your help.
    Thanks in advanvce

      1. Hi Syarif,
        In the above script:
        1- mobile nodes are communicating with wired nodes via gateways and no communication between mobile nodes.
        2- In script, there is only one routing protocol i.e. AODV and doesn’t show communication between different mobile routing protocols?
        3- The functionality of basestation is not clear, does it transform the packets from source to destination type. Can you explain or any good material on its internal functionality.
        Objective: I want to communicate between AODV and DSR via Gateway/Router/Basestation (whatever its called in ns-2).
        Your help will be highly appreciated.

  12. hi,
    can you tell me how to write tcl script for aodv that includes node movement or mobility,and cbr/tcp traffic and should show packet loss between source and destination node…

  13. AODV+ .. salam dear brother . You are doing great job by sharing codes unlike others. Brother , can you mail me whole code for Aodv+ protocol . i need to enhance it , MAy be will enhance in terms of load .. If you having souce code in NS3 , that will be most useful else ns2 will also work..

  14. Hello sir,
    I am currently working on a project of detection and prevention of blackhole in clustered manet. It would be of great help if you could guide me as to where to start with and where can i get code for clustering the manet and implementing a blackhole attack?

    1. Sir i would be really grateful to you if you could help at the earliest…i have to submit the project by the 25th of this month. Kindly look into my query.

  15. Sir but i wanted the code for formation of a clustered manet first. Thank you for the link but it seems that author has only evaluated the performance under a static topology. i wanted it for a cluster based manet. or can you provide me the code for introducing a blackhole in a manet atleast?

  16. sir,i am a post graduate student and I’m working in ns2 to design a 3-D network topology.I want to check parameters like area and energy,How is it possible in ns2?ALso is there any way to change routing algorithm in ns2 for wired networks?

  17. Sir,I am a Mtech student and I’m doing a project on wireless mesh network.I need a running tcl script for designing wireless mesh network with mesh clients,routers and gateways. It would be of great help if you provide me the code because with this I will be able to proceed further with my project.

  18. Hi sir, we(B.Tech final year) are doing project based on clustering using AODV protocol in MANETS. Initially we tried to execute your code in ns2, but it is not executed. It is showing some errors. Can you please provide us if they are any other codes related to clustering using gateways.

  19. sivaranjani chandrasekar

    I run your program. I am getting error as num_nodes 7 and showing error in LL. what are the changes to be done to run the program without getting error

  20. dear sir can u help me in blackhole attack prevention in MANETs. i need a tcl script in blackhole attack prevention using BHAPSC algorithm… can u plz help me out sir.

  21. Sir, I am doing my project on UnderWater Acoustic Communication.
    We Successfully Installed AQUASIM… But How do we do Clustering among the Nodes?

  22. hello sir, i have used your tcl script. but destination can’t receive message. i got DROP_RTR_QFULL. can you please help me.

  23. Hi sir,
    It’s great to see you helping others with NS2. I’m a postgraduate student seeking for help in Energy model (optimization of energy) for linear topology. Do share if there are any relevant NS2 scripts which will benefit for linear topology.
    Thank you.

  24. Hello sir, i have run the above code but am not getting any output , the trace file contain nothing . help me to solve this problem.

  25. hii sir
    can u tell me tcl script for aodv-uu routing protocol based elliptic curve cryptograhpy algorithm

  26. Ajibesin Caleb Adejare

    Hi Sir or Ma. My name is Ajibesin Caleb Adejare doing my master program on manet routing protocols. I have problem in running my Tcl Script for mobile ad-hoc network. When i running your code above on my ns 2, it complains the following:
    num_nodes is set 7
    invalid command name “-llType”
    while executing
    “-llType $val(ll)
    file “p.tcl” line 86
    Pls, provide me with solution to the above. Thank you.

  27. Please help me out from this problem, i.e,
    Segmentation fault (core dumped) while running nam file in ns2
    how to resolve this

  28. Ajibesin Caleb Adejare

    When i use awk script for trace file, for throughput value i got -0.0. awk script for end to end delay, routing overhead, packet delivery ratio and average jitter complain of bug or give error message. Pls, what is solution?

  29. Ajibesin Caleb Adejare

    When i used perl script for trace file, it gives me zero for send packet, receive packet, drop packet and soon. Pls, what is solution?

  30. Ajibesin Caleb Adejare

    Another problem is that the tcl script i am using can only work for aodv and dsr but it can’t work for zrp. And i need a tcl script that can work for the three routing protocol. What is solution?

  31. Ajibesin Caleb Adejare

    Packet Sent:0
    Packet Received:0awk: pd.awk:21: ( FNR=34919) fatal: division by zero attempted
    pls, i need solution to this not the afformenttioned again. When i run my awk script for packet delivery ratio, i obtained zero and it complain division by zero attempted. Pls, provide with solution. Thank you very much.

  32. Ajibesin Caleb Adejare

    Pls, can you give me detail or ideal of how to install zrp on my ns 2. Thank you u for previous reply. I am grateful

Leave a Reply

Your email address will not be published. Required fields are marked *