lamiastella

paramiko without netem

Dec 18th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. import os
  2. import paramiko
  3. import sys
  4.  
  5. def get_private_key():
  6.     # or choose the location and the private key file on your client
  7.     private_key_file = os.path.expanduser("/home/ubuntu/.ssh/id_rsa")
  8.     return paramiko.RSAKey.from_private_key_file(private_key_file, password='')
  9.  
  10.  
  11. def get_ssh(myusername, myhostname, myport):
  12.     ssh = paramiko.SSHClient()
  13.     ssh.load_system_host_keys()
  14.     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  15.     #ssh.connect(myhostname, username=myusername, port=myport, pkey = private_key)
  16.     ssh.connect(myhostname, username=myusername, port=myport)
  17.     return ssh
  18.      
  19. def block_exec(ssh, command):
  20.     stdin, stdout, stderr = ssh.exec_command(command)
  21.     exit_status = stdout.channel.recv_exit_status()
  22.     print command, exit_status
  23.     #print "stderr is %s" % stderr
  24.     print stderr.read()
  25.     return
  26.    
  27. def clear_bw_config2(ssh, interface):
  28.     block_exec(ssh, "sudo tc qdisc del dev %s root" % interface)
  29.     block_exec(ssh, "sudo tc qdisc del dev %s ingress" % interface)
  30.     block_exec(ssh, "sudo tc class del dev %s root" % interface)
  31.     block_exec(ssh, "sudo tc filter del dev %s root" % interface)
  32.  
  33.  
  34. def exec_bw_config2(ssh, interface, bandwidth, ip, subnetmasklength):
  35.     clear_bw_config2(ssh, interface)
  36.     # create a qdisc (queuing discipline), 12 is default class
  37.     cmd1 = "sudo tc qdisc add dev %s root handle 1: htb default 12" % interface
  38.     print cmd1
  39.     block_exec(ssh, cmd1)
  40.  
  41.     # define the performance for default class
  42.     cmd2 = "sudo tc class add dev %s parent 1: classid 1:1 htb rate %dmbps ceil %dmbps" % (interface, bandwidth, 2*bandwidth )
  43.     print cmd2
  44.  
  45.     block_exec(ssh, cmd2)
  46.  
  47.  
  48.     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)
  49.     print filter_cmd
  50.     block_exec(ssh, filter_cmd)
  51.  
  52. def main():
  53.     myhosts = ["10.0.1.192", "10.0.1.191", "10.0.1.190"]
  54.     username="ubuntu"
  55.     port=22
  56.     #key = get_private_key()
  57.     for host in myhosts:
  58.         ssh = get_ssh(username, host, port)
  59.         clear_bw_config2(ssh, "eth0")
  60.         #del_cmd="sudo tc qdisc del dev eth0 root"
  61.         #block_exec(ssh, del_cmd)
  62.         exec_bw_config2(ssh, "eth0", int(sys.argv[1]) , "10.0.1.0", 24)
  63.  
  64.     # iterate over hosts here
  65.     # for everyhost,
  66.     # 1. create ssh connection
  67.     # 2. run the exec_bw_config with params
  68.     return
  69.  
  70. if __name__ == '__main__':
  71.     main()
Advertisement
Add Comment
Please, Sign In to add comment