Let’s discuss about the process that are required to write a significant script based on network simulator 3.
Firstly, the main script based on network simulator 3 is created and it is based on the python script file functioning with the file extension of .py and it is stored in the scratch folder.
![Creation of Ns3 Python Script](https://ns3simulation.com/wp-content/uploads/2023/01/Python-Script.png)
For your reference, our technical experts have highlighted some python script code based on ns3 because it is easy for your understanding.
- Creation of on and off application to send UDP datagrams based on the size of 210 bytes at the rate of 448 kb/s among the nodes
print "Create Applications."
port = 9 # Discard port(RFC 863)
appSource = ns.network.NodeList.GetNode(backboneNodes)
lastNodeIndex = backboneNodes + backboneNodes*(lanNodes - 1) + backboneNodes*(infraNodes - 1) - 1
appSink = ns.network.NodeList.GetNode(lastNodeIndex)
# Let's fetch the IP address of the last node, which is on Ipv4Interface 1
remoteAddr = appSink.GetObject(ns.internet.Ipv4.GetTypeId()).GetAddress(1,0).GetLocal()
onoff = ns.applications.OnOffHelper("ns3::UdpSocketFactory",
ns.network.Address(ns.network.InetSocketAddress(remoteAddr, port)))
apps = onoff.Install(ns.network.NodeContainer(appSource))
apps.Start(ns.core.Seconds(3))
apps.Stop(ns.core.Seconds(stopTime – 1))
- Mobility configuration
mobility = ns.mobility.MobilityHelper()
mobility.SetPositionAllocator("ns3::GridPositionAllocator",
"MinX", ns.core.DoubleValue(20.0),
"MinY", ns.core.DoubleValue(20.0),
"DeltaX", ns.core.DoubleValue(20.0),
"DeltaY", ns.core.DoubleValue(20.0),
"GridWidth", ns.core.UintegerValue(5),
"LayoutType", ns.core.StringValue("RowFirst"))
mobility.SetMobilityModel("ns3::RandomDirection2dMobilityModel",
"Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle(-500, 500, -500, 500)),
"Speed", ns.core.StringValue ("ns3::ConstantRandomVariable[Constant=2]"),
"Pause", ns.core.StringValue ("ns3::ConstantRandomVariable[Constant=0.2]"))
mobility.Install(backbone)
- Node creation process
backbone = ns.network.NodeContainer()
backbone.Create(backboneNodes)
• Configuration of packet transmission
ns.core.Config.SetDefault("ns3::OnOffApplication::PacketSize",
ns.core.StringValue("1472"))
ns.core.Config.SetDefault("ns3::OnOffApplication::DataRate",
ns.core.StringValue("100kb/s"))
- Package imports
import ns.applications
import ns.core
import ns.csma
import ns.internet
import ns.mobility
import ns.network
import ns.olsr
import ns.wifi
Now, we have to implement the below mentioned commands to execute the python script.
cd /home/research/ns-allinone-3.26/ns-3.26
sudo ./waf –pyrun scratch/Main.py
![Execution of Python Script](https://ns3simulation.com/wp-content/uploads/2023/01/Script-Execution.png)
In addition, our technical experts have specified the results that are acquired the implementation of the python scripts in the following.
![Result of Python Script Execution](https://ns3simulation.com/wp-content/uploads/2023/01/result-for-Script-Execution1.png)
![Execution Result in Log File](https://ns3simulation.com/wp-content/uploads/2023/01/result-for-Script-Execution2-1024x543.png)
The research scholars can reach us at time to clarify your difficulties based on the creation of ns3 scripts.