BCA / B.Tech 8 min read

Invoking external commands within NS2

Invoking external commands within NS2 | Invoking external commands within NS2:


  • The use of external commands in NS2 makes the simulation more powerful and flexible. Through the exec command, you can easily add external scripts and processes, which provides the facility of data processing and analysis during the simulation.
  • However, this process needs to be used carefully so that the performance of the simulation is not negatively affected. The combination of NS2 and OTcl provides you with a rich and effective simulation environment, which helps in successfully adding external commands.
  • NS2 (Network Simulator 2) is a powerful tool used for network simulation. Sometimes, you may need to execute other external commands within NS2.
  • This process can be done by combining C++ and OTcl (Object-oriented Tcl) scripting within NS2. Here we will discuss various aspects of how external commands can be implemented in NS2.

on its various aspects.

Implementing external commands in NS2

1. Introduction to OTcl and C++

NS2 mainly uses two languages:

OTcl: This is a scripting language used for simulation setup in NS2.
C++: The main simulator code of NS2 is written in C++, which is important for performance and efficiency.

2. Use of external commands

To execute external commands in NS2, you generally need to use the exec command. This command allows you to execute any command in the terminal or shell.

3. Use of the exec command

By using the exec command in OTcl, you can run various external scripts or commands. Its syntax is as follows:

set result [exec command]
Here command is the command that you want to execute. The result will be stored in result.

4. Example

Let's see an example in which we execute an external command in NS2.

# Start of NS2 script
set ns [new Simulator]

# Creation of nodes
set node1 [$ns node]
set node2 [$ns node]

# Adding connection
$ns connect $node1 $node2

# Data sending process
$ns at 1.0 "$node1 send $node2 10MB"

# Executing the external command
set command_result [exec echo "Hello from NS2"]
puts $command_result

# Running the simulation
$ns run
In this example:

We ran the echo "Hello from NS2" command through exec.
By using puts $command_result, we print the result of the command.

5. Use of external scripts

You can run not only single commands, but also scripts. If you have a shell script that you want to run, you can do it in the same way:

# Running an external shell script
set script_result [exec ./my_script.sh]
puts $script_result

6. Saving command output to a file

If you want to save the output to a file, you can use >:

set result [exec echo "Hello from NS2" > output.txt]
This command will save "Hello from NS2" to the output.txt file.

7. Management of external processes

If you want to control an external process, such as stopping or restarting it, you need to remember that NS2 is primarily designed for simulation.
But through exec, you can manage other processes, although this can be more complex.

In this Chapter

Invoking external commands within NS2
Auto-Configuration and Anycast
Modified EUI-64 | What is Modified EUI-64?
Architecture of Mobile Computing
Wireless LAN in Hindi | What is Wireless LAN?
Wireless User Devices
MAC Protocol | What is MAC Protocol?
IEEE 802.11 | What is IEEE 802.11
Mobile IP | What is Mobile IP
Wireless TCP/IP
Unicast & Multicast Communication
Bluetooth | What is Bluetooth?
NS2 & NAM | What are NS2 and NAM?
Purpose & installation of NS2 & NAM
Background of NS2 & NAM
Architecture of NS2 & NAM
Interface of OTcl & C++
Trace Files & Formats
Protocol Support of NS2
Simulation Object of NS2 & NAM
Basic Syntax of NS2 & NAM
Node Creation of NS2 & NAM
Running NS2 & NAM
Finish Procedure NS2 & NAM
Nodes & Agents of NS2 & NAM
NS2 Commands
Creating Links in Wired Network
Setting Link Parameters
Sending Traffics Through NS2 Links
Routing Protocol Support of NS2
Scenarios in Wired Networks
Additional Parameters
Setting node positions
God object & topography
Protocol Support
Scenarios in Wireless Networks
What is the Internet? Difference Between Intranet and Extranet
Types of Internet
OSI Model and its Layers
Computer Network (CN) All Important Questions and Answers in English (MDSU)
BCA | Computer Network | 2025 Paper | MDSU Exam Paper
Importance of the Layer Model in Computer Networks
Network Classification
Network Topology
Network Switching & Components
Ethernet
Token Ring
Basic Networking Concepts & Cabling
What is a Computer in English?
Bridges in Computer Networks
Routers in Computer Networks
Gateways in Computer Networks
Public & Private Networks
FDMA (Frequency Division Multiple Access)
BCA | CN(Computer Network) Paper | 2023 MDSU Exam Paper
What is World Wide Web (WWW)
TDMA, SDMA, CDMA
Personal Communication System | What is PCS
IPv4 | What is IPv4?
Subnetting IPv4 Address
Casting in IPv4
Private IPv4 Addressing
IPv4 Address Scheme
IPv6 Addressing Scheme
Types of IPv6 Addressing