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.

Tags: , , , , , , ,

63 Responses to “TCL Script for My NS-2 Project”

  1. computer running slow Says:

    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

    Liked by 1 person

  2. ravi Says:

    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


    • abdusy Says:

      You need a file called mobile5_4. You can create it by using setdest function.


      • Anil Says:

        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


      • Narendra Says:

        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. abdusy syarif Says:

    Please choose, which one you will use in your simulation.


    ./setdest -v -n -p -M
    -t -x -y


    ./setdest -v -n -s -m -M
    -t -P -p -x -y


  4. Narendra Says:

    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


  5. abdusy syarif Says:

    please read the manual how to use ‘setdest’. After you create a mobility file using ‘setdest’ utility, you can copy / call the mobility file in your tcl script.


  6. Anil Says:

    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


  7. abdusy syarif Says:

    You can use a simple tcl script in ns-allinone-2.xx/ns-2.xx/tcl/ex/wireless-newnode-energy.tcl


  8. Narendra Says:

    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…


  9. Anil Says:

    #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


  10. yat Says:

    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?


    • abdusy syarif Says:

      It’s done in .cc. The mechanisme of R-AODV you can read from


      • yat Says:

        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..


      • yat Says:

        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..


  11. Hassabu M. Ahmed Says:

    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!


  12. vicky Says:

    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


  13. Hassabu M. Ahmed Says:

    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.


  14. Hassabu M. Ahmed Says:

    The flowchart (GNDA flowchart) is found here:


  15. Ali Says:

    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


    • abdusy syarif Says:

      Hi Ali, you could refer to AODV+ or AODV-UU.


      • Ali Ray Says:

        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.



  16. mamta Says:

    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…


  17. saqib Says:

    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..


  18. tom Says:

    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?


  19. tom Says:

    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?


  20. deepak Says:

    Sir, can i get any code inorder to form clusters based on aodv protocol?


  21. Neelu Says:

    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?


  22. durga Says:

    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.


  23. P.Sreedevi Says:

    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.


  24. sivaranjani chandrasekar Says:

    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


  25. Manjusha Says:

    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.


  26. Manjusha Says:

    sir plz do help me out with my project.. i would be really grateful if u could reply me as early as possible…


  27. Nivetha Says:

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


  28. Foram Patel Says:

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


  29. Nithin K.B Says:


    Can you provide the code for efficient neighbour discovery in AODV routing in ns2…

    Nithin K B


  30. SK Says:

    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.



  31. Yojana Singh Says:



  32. anitha Says:

    hello sir i need ns2 coding for load balanced clustering in wireless sensor network using genetic algorithm.can u please send that coding to my


  33. Homepage Says:

    … [Trackback]

    […] Read More here: […]


  34. Harpreet Kaur Says:

    Hello sir,i need code in ns2 tcl script for creating cluster and cluster head selection on the basis of node degree,can you pls mail me the code


  35. ilakkiya Says:

    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.


  36. kavi bharathi Says:

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


  37. Ajibesin Caleb Adejare Says:

    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.


  38. Prem Says:

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


  39. Ajibesin Caleb Adejare Says:

    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?


  40. Ajibesin Caleb Adejare Says:

    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?


  41. Ajibesin Caleb Adejare Says:

    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?


  42. Ajibesin Caleb Adejare Says:

    I mean it complain for zrp but it is not for aodv and dsr. Thank you for your respond.

    Liked by 1 person

  43. Ajibesin Caleb Adejare Says:

    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.


  44. Ajibesin Caleb Adejare Says:

    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


  45. caleb9483 Says:

    pls, how to simulate 10, 20, 30, 40 and 50 nodes simultaneously
    in manet pls


  46. mohammad Says:

    can you build an tcl file to encrypt AODV using diffie hellman and make hashing using md5


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: