2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.execution.resource import clsinit_copy
21 from nepi.resources.linux.application import LinuxApplication
22 from nepi.util.timefuncs import tnow
27 class LinuxNPing(LinuxApplication):
28 _rtype = "linux::NPing"
31 def _register_attributes(cls):
33 "Sets nping -c option. "
34 "Stop after a given number of rounds. ",
39 "Sets nping -e option. "
40 "Set the network interface to be used.",
43 delay = Attribute("delay",
44 "Sets nping --delay option. "
45 "Delay between probes ",
48 rate = Attribute("rate",
49 "Sets nping --rate option. "
50 "Send probes at a given rate ",
53 ttl = Attribute("ttl",
54 "Sets nping --ttl option. "
59 "Sets nping -p option. "
64 tcp = Attribute("tcp",
65 "Sets nping --tcp option. "
71 udp = Attribute("udp",
72 "Sets nping --udp option. "
78 icmp = Attribute("icmp",
79 "Sets nping --icmp option. "
85 arp = Attribute("arp",
86 "Sets nping --arp option. "
92 traceroute = Attribute("traceroute",
93 "Sets nping --traceroute option. "
99 countinuous = Attribute("continuous",
100 "Run nping in a while loop",
103 flags = Flags.Design)
105 print_timestamp = Attribute("printTimestamp",
106 "Print timestamp before running nping",
109 flags = Flags.Design)
111 target = Attribute("target",
112 "nping target host (host that will be pinged)",
113 flags = Flags.Design)
115 cls._register_attribute(c)
116 cls._register_attribute(e)
117 cls._register_attribute(delay)
118 cls._register_attribute(rate)
119 cls._register_attribute(ttl)
120 cls._register_attribute(p)
121 cls._register_attribute(tcp)
122 cls._register_attribute(udp)
123 cls._register_attribute(icmp)
124 cls._register_attribute(arp)
125 cls._register_attribute(traceroute)
126 cls._register_attribute(countinuous)
127 cls._register_attribute(print_timestamp)
128 cls._register_attribute(target)
130 def __init__(self, ec, guid):
131 super(LinuxNPing, self).__init__(ec, guid)
132 self._home = "nping-%s" % self.guid
133 self._sudo_kill = True
136 if not self.get("command"):
137 self.set("command", self._start_command)
139 if not self.get("install"):
140 self.set("install", self._install)
142 if not self.get("env"):
143 self.set("env", "PATH=$PATH:/usr/sbin/")
145 if not self.get("depends"):
146 self.set("depends", "nmap")
148 super(LinuxNPing, self).do_deploy()
151 def _start_command(self):
153 if self.get("continuous") == True:
154 args.append("while true; do ")
155 if self.get("printTimestamp") == True:
156 args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
157 args.append("sudo -S nping ")
159 args.append("-c %s" % self.get("c"))
161 args.append("-e %s" % self.get("e"))
162 if self.get("delay"):
163 args.append("--delay %s" % self.get("delay"))
165 args.append("--rate %s" % self.get("rate"))
167 args.append("--ttl %s" % self.get("ttl"))
169 args.append("-p %s" % self.get("p"))
170 if self.get("tcp") == True:
172 if self.get("udp") == True:
174 if self.get("icmp") == True:
175 args.append("--icmp")
176 if self.get("arp") == True:
178 if self.get("traceroute") == True:
179 args.append("--traceroute")
181 args.append(self.get("target"))
183 if self.get("continuous") == True:
184 args.append("; done ")
186 command = " ".join(args)
192 install = "echo 'nothing to do'"
193 if self.node.use_rpm:
197 " if [ `uname -m` == 'x86_64' ]; then "
198 " wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.x86_64.rpm ;"
199 " else wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.i386.rpm ;"
202 " && sudo -S rpm -vhU nping.rpm ) ")
203 elif self.node.use_deb:
204 from nepi.resources.linux import debfuncs
205 install_alien = debfuncs.install_packages_command(self.node.os, "alien gcc")
209 " if [ `uname -m` == 'x86_64' ]; then "
210 " wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.x86_64.rpm ;"
211 " else wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.i386.rpm ;"
214 " && %s && sudo alien -i nping.rpm ) " % install_alien)
216 return ("( nping --version || %s )" % install)
218 def valid_connection(self, guid):