pyinterfases.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #this is network Interfaces manager libery for CentOS in python
  2. import os
  3. import subprocess
  4. def get_uuid(name):
  5. #Use to get UUID to the network device
  6. p = subprocess.Popen(["uuidgen", name], stdout=subprocess.PIPE)
  7. q=str(p.communicate())
  8. q=q.replace("(b\'", "")
  9. q=q.replace("\\n', None)", "")
  10. q=q.replace(" ", "")
  11. return q
  12. def net_list():
  13. #list all Network interfaces .... its return list
  14. tru_list=[]
  15. list=os.listdir("/etc/sysconfig/network-scripts")
  16. for x in list:
  17. if "ifcfg-" in x:
  18. x=x.replace("ifcfg-", "")
  19. tru_list.append(x)
  20. return tru_list
  21. def interfase(name):
  22. #Display the information of the network interface
  23. # its need the interface name as a parameter
  24. # its return Dictionary
  25. netdec={
  26. "ip":"no",
  27. "gateway":"no",
  28. "netmask":"no"
  29. }
  30. filepath = "ifcfg-"+name
  31. with open(filepath) as fp:
  32. for cnt, line in enumerate(fp):
  33. if "dhcp" in line:
  34. netdec["ip"]="dhcp"
  35. netdec["gateway"]="dhcp"
  36. netdec["netmask"]="dhcp"
  37. if "IPADDR" in line:
  38. line=line.replace("IPADDR=", "")
  39. line=line.replace("\n", "")
  40. netdec["ip"]=line
  41. if "GATEWAY" in line:
  42. line=line.replace("GATEWAY=", "")
  43. line=line.replace("\n", "")
  44. netdec["gateway"]=line
  45. if "PREFIX" in line:
  46. line=line.replace("PREFIX=", "")
  47. line=line.replace("\n", "")
  48. netdec["netmask"]=line
  49. return netdec
  50. def add(name, ip, prefix, gateway):
  51. # add new Network interface
  52. if name=="lo":
  53. return 0
  54. with open('ifcfg-'+name, 'w') as the_file:
  55. if "dhcp" in ip:
  56. the_file.write('TYPE=Ethernet\n')
  57. the_file.write('BOOTPROTO=dhcp\n')
  58. the_file.write('DEFROUTE=yes\n')
  59. the_file.write('PEERDNS=yes\n')
  60. the_file.write('PEERROUTES=yes\n')
  61. the_file.write('IPV4_FAILURE_FATAL=no\n')
  62. the_file.write('IPV6INIT=yes\n')
  63. the_file.write('IPV6_AUTOCONF=yes\n')
  64. the_file.write('IPV6_DEFROUTE=yes\n')
  65. the_file.write('IPV6_PEERDNS=yes\n')
  66. the_file.write('IPV6_PEERROUTES=yes\n')
  67. the_file.write('IPV6_FAILURE_FATAL=no\n')
  68. the_file.write('IPV6_ADDR_GEN_MODE=stable-privacy\n')
  69. the_file.write('NAME='+name+'\n')
  70. the_file.write('UUID='+get_uuid(name)+'\n')
  71. the_file.write('DEVICE='+name+'\n')
  72. the_file.write('ONBOOT=yes\n')
  73. else:
  74. the_file.write('TYPE=Ethernet\n')
  75. the_file.write('BOOTPROTO=none\n')
  76. the_file.write('DEFROUTE=yes\n')
  77. the_file.write('IPV4_FAILURE_FATAL=no\n')
  78. the_file.write('IPV6INIT=yes\n')
  79. the_file.write('IPV6_AUTOCONF=yes\n')
  80. the_file.write('IPV6_DEFROUTE=yes\n')
  81. the_file.write('IPV6_FAILURE_FATAL=no\n')
  82. the_file.write('IPV6_ADDR_GEN_MODE=stable-privacy\n')
  83. the_file.write('NAME='+name+'\n')
  84. the_file.write('UUID='+get_uuid(name)+'\n')
  85. the_file.write('DEVICE='+name+'\n')
  86. the_file.write('ONBOOT=yes\n')
  87. the_file.write('DNS1=8.8.8.8\n')
  88. the_file.write('IPADDR='+ip+'\n')
  89. the_file.write('PREFIX='+prefix+'28\n')
  90. the_file.write('GATEWAY='+gateway+'\n')
  91. the_file.write('IPV6_PEERDNS=yes\n')
  92. the_file.write('IPV6_PEERROUTES=yes\n')
  93. the_file.write('IPV6_PRIVACY=no\n')
  94. return 0
  95. def add_v(name, ip, prefix):
  96. # name=name.replace(":", "_")
  97. # add new v Network interface
  98. with open('ifcfg-'+name, 'w') as the_file:
  99. # name=name.replace("_", ":")
  100. the_file.write('TYPE=Ethernet\n')
  101. the_file.write('BOOTPROTO=none\n')
  102. the_file.write('NAME='+name+'\n')
  103. the_file.write('DEVICE='+name+'\n')
  104. the_file.write('ONBOOT=yes\n')
  105. the_file.write('IPADDR='+ip+'\n')
  106. the_file.write('PREFIX='+prefix+'28\n')
  107. return 0
  108. def add_net(name, ip, prefix, gateway):
  109. if ":" in name:
  110. add_v(name, ip, prefix)
  111. else:
  112. add(name, ip, prefix, gateway)
  113. return 0
  114. def restart(name):
  115. #restart Network interface
  116. # its need the interface name as a parameter
  117. name=name.replace(":", "_")
  118. net="ifdown "+name+" && ifup "+name
  119. run=subprocess.check_output(net, shell=True)
  120. return 0
  121. def start(name):
  122. #start Network interface
  123. # its need the interface name as a parameter
  124. name=name.replace(":", "_")
  125. net="ifup "+name
  126. run=subprocess.check_output(net, shell=True)
  127. return 0
  128. def stop(name):
  129. #stop Network interface
  130. # its need the interface name as a parameter
  131. name=name.replace(":", "_")
  132. net="ifdown "+name
  133. run=subprocess.check_output(net, shell=True)
  134. return 0
  135. def remove(name):
  136. #remove Network interface
  137. # its need the interface name as a parameter
  138. os.remove("/etc/sysconfig/network-scripts/ifcfg-"+name