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 as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.resource import clsinit_copy
22 from nepi.resources.linux.application import LinuxApplication
23 from nepi.util.timefuncs import tnow
27 nping_sources = dict({
28 "i686": "http://nmap.org/dist/nping-0.6.25-1.i386.rpm",
29 "amd64": "http://nmap.org/dist/nping-0.6.25-1.x86_64.rpm",
33 class LinuxNPing(LinuxApplication):
37 def _register_attributes(cls):
39 "Sets nping -c option. "
40 "Stop after a given number of rounds. ",
42 flags = Flags.ExecReadOnly)
45 "Sets nping -e option. "
46 "Set the network interface to be used.",
47 flags = Flags.ExecReadOnly)
49 delay = Attribute("delay",
50 "Sets nping --delay option. "
51 "Delay between probes ",
52 flags = Flags.ExecReadOnly)
54 rate = Attribute("rate",
55 "Sets nping --rate option. "
56 "Send probes at a given rate ",
57 flags = Flags.ExecReadOnly)
59 ttl = Attribute("ttl",
60 "Sets nping --ttl option. "
62 flags = Flags.ExecReadOnly)
65 "Sets nping -p option. "
68 flags = Flags.ExecReadOnly)
70 tcp = Attribute("tcp",
71 "Sets nping --tcp option. "
75 flags = Flags.ExecReadOnly)
77 udp = Attribute("udp",
78 "Sets nping --udp option. "
82 flags = Flags.ExecReadOnly)
84 icmp = Attribute("icmp",
85 "Sets nping --icmp option. "
89 flags = Flags.ExecReadOnly)
91 arp = Attribute("arp",
92 "Sets nping --arp option. "
96 flags = Flags.ExecReadOnly)
98 traceroute = Attribute("traceroute",
99 "Sets nping --traceroute option. "
103 flags = Flags.ExecReadOnly)
105 countinuous = Attribute("continuous",
106 "Run nping in a while loop",
109 flags = Flags.ExecReadOnly)
111 print_timestamp = Attribute("printTimestamp",
112 "Print timestamp before running nping",
115 flags = Flags.ExecReadOnly)
117 target = Attribute("target",
118 "nping target host (host that will be pinged)",
119 flags = Flags.ExecReadOnly)
121 cls._register_attribute(c)
122 cls._register_attribute(e)
123 cls._register_attribute(delay)
124 cls._register_attribute(rate)
125 cls._register_attribute(ttl)
126 cls._register_attribute(p)
127 cls._register_attribute(tcp)
128 cls._register_attribute(udp)
129 cls._register_attribute(icmp)
130 cls._register_attribute(arp)
131 cls._register_attribute(traceroute)
132 cls._register_attribute(countinuous)
133 cls._register_attribute(print_timestamp)
134 cls._register_attribute(target)
136 def __init__(self, ec, guid):
137 super(LinuxNPing, self).__init__(ec, guid)
138 self._home = "nping-%s" % self.guid
139 self._sudo_kill = True
142 if not self.get("command"):
143 self.set("command", self._start_command)
145 if not self.get("install"):
146 self.set("install", self._install)
148 if not self.get("env"):
149 self.set("env", "PATH=$PATH:/usr/sbin/")
151 if not self.get("depends"):
152 self.set("depends", "nmap")
154 super(LinuxNPing, self).deploy()
157 def _start_command(self):
159 if self.get("continuous") == True:
160 args.append("while true; do ")
161 if self.get("printTimestamp") == True:
162 args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
163 args.append("sudo -S nping ")
165 args.append("-c %s" % self.get("c"))
167 args.append("-e %s" % self.get("e"))
168 if self.get("delay"):
169 args.append("--delay %s" % self.get("delay"))
171 args.append("--rate %s" % self.get("rate"))
173 args.append("--ttl %s" % self.get("ttl"))
175 args.append("-p %s" % self.get("p"))
176 if self.get("tcp") == True:
178 if self.get("udp") == True:
180 if self.get("icmp") == True:
181 args.append("--icmp")
182 if self.get("arp") == True:
184 if self.get("traceroute") == True:
185 args.append("--traceroute")
187 args.append(self.get("target"))
189 if self.get("continuous") == True:
190 args.append("; done ")
192 command = " ".join(args)
198 install = "echo 'nothing to do'"
199 if self.node.use_rpm:
203 " if [ `uname -m` == 'x86_64' ]; then "
204 " wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.x86_64.rpm ;"
205 " else wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.i386.rpm ;"
208 " && sudo -S rpm -vhU nping.rpm ) ")
209 elif self.node.use_deb:
210 from nepi.resources.linux import debfuncs
211 install_alien = debfuncs.install_packages_command(self.node.os, "alien gcc")
215 " if [ `uname -m` == 'x86_64' ]; then "
216 " wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.x86_64.rpm ;"
217 " else wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.i386.rpm ;"
220 " && %s && sudo alien -i nping.rpm ) " % install_alien)
222 return ("( nping || %s )" % install)
224 def valid_connection(self, guid):