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
28 class LinuxNPing(LinuxApplication):
32 def _register_attributes(cls):
34 "Sets nping -c option. "
35 "Stop after a given number of rounds. ",
37 flags = Flags.ExecReadOnly)
40 "Sets nping -e option. "
41 "Set the network interface to be used.",
42 flags = Flags.ExecReadOnly)
44 delay = Attribute("delay",
45 "Sets nping --delay option. "
46 "Delay between probes ",
47 flags = Flags.ExecReadOnly)
49 rate = Attribute("rate",
50 "Sets nping --rate option. "
51 "Send probes at a given rate ",
52 flags = Flags.ExecReadOnly)
54 ttl = Attribute("ttl",
55 "Sets nping --ttl option. "
57 flags = Flags.ExecReadOnly)
60 "Sets nping -p option. "
63 flags = Flags.ExecReadOnly)
65 tcp = Attribute("tcp",
66 "Sets nping --tcp option. "
70 flags = Flags.ExecReadOnly)
72 udp = Attribute("udp",
73 "Sets nping --udp option. "
77 flags = Flags.ExecReadOnly)
79 icmp = Attribute("icmp",
80 "Sets nping --icmp option. "
84 flags = Flags.ExecReadOnly)
86 arp = Attribute("arp",
87 "Sets nping --arp option. "
91 flags = Flags.ExecReadOnly)
93 traceroute = Attribute("traceroute",
94 "Sets nping --traceroute option. "
98 flags = Flags.ExecReadOnly)
100 countinuous = Attribute("continuous",
101 "Run nping in a while loop",
104 flags = Flags.ExecReadOnly)
106 print_timestamp = Attribute("printTimestamp",
107 "Print timestamp before running nping",
110 flags = Flags.ExecReadOnly)
112 target = Attribute("target",
113 "nping target host (host that will be pinged)",
114 flags = Flags.ExecReadOnly)
116 cls._register_attribute(c)
117 cls._register_attribute(e)
118 cls._register_attribute(delay)
119 cls._register_attribute(rate)
120 cls._register_attribute(ttl)
121 cls._register_attribute(p)
122 cls._register_attribute(tcp)
123 cls._register_attribute(udp)
124 cls._register_attribute(icmp)
125 cls._register_attribute(arp)
126 cls._register_attribute(traceroute)
127 cls._register_attribute(countinuous)
128 cls._register_attribute(print_timestamp)
129 cls._register_attribute(target)
131 def __init__(self, ec, guid):
132 super(LinuxNPing, self).__init__(ec, guid)
133 self._home = "nping-%s" % self.guid
134 self._sudo_kill = True
137 if not self.get("command"):
138 self.set("command", self._start_command)
140 if not self.get("install"):
141 self.set("install", self._install)
143 if not self.get("env"):
144 self.set("env", "PATH=$PATH:/usr/sbin/")
146 if not self.get("depends"):
147 self.set("depends", "nmap")
149 super(LinuxNPing, self).deploy()
152 def _start_command(self):
154 if self.get("continuous") == True:
155 args.append("while true; do ")
156 if self.get("printTimestamp") == True:
157 args.append("""echo "`date +'%Y%m%d%H%M%S'`";""")
158 args.append("sudo -S nping ")
160 args.append("-c %s" % self.get("c"))
162 args.append("-e %s" % self.get("e"))
163 if self.get("delay"):
164 args.append("--delay %s" % self.get("delay"))
166 args.append("--rate %s" % self.get("rate"))
168 args.append("--ttl %s" % self.get("ttl"))
170 args.append("-p %s" % self.get("p"))
171 if self.get("tcp") == True:
173 if self.get("udp") == True:
175 if self.get("icmp") == True:
176 args.append("--icmp")
177 if self.get("arp") == True:
179 if self.get("traceroute") == True:
180 args.append("--traceroute")
182 args.append(self.get("target"))
184 if self.get("continuous") == True:
185 args.append("; done ")
187 command = " ".join(args)
193 install = "echo 'nothing to do'"
194 if self.node.use_rpm:
198 " if [ `uname -m` == 'x86_64' ]; then "
199 " wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.x86_64.rpm ;"
200 " else wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.i386.rpm ;"
203 " && sudo -S rpm -vhU nping.rpm ) ")
204 elif self.node.use_deb:
205 from nepi.resources.linux import debfuncs
206 install_alien = debfuncs.install_packages_command(self.node.os, "alien gcc")
210 " if [ `uname -m` == 'x86_64' ]; then "
211 " wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.x86_64.rpm ;"
212 " else wget -O nping.rpm http://nmap.org/dist/nping-0.6.25-1.i386.rpm ;"
215 " && %s && sudo alien -i nping.rpm ) " % install_alien)
217 return ("( nping --version || %s )" % install)
219 def valid_connection(self, guid):