• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 Google Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import shutil
16from mobly.controllers import sniffer
17from mobly.controllers.sniffer_lib.local import local_base
18
19
20class Sniffer(local_base.SnifferLocalBase):
21  """This class defines a sniffer which uses tcpdump as its back-end
22    """
23
24  def __init__(self, config_path, logger, base_configs=None):
25    """See base class documentation
26        """
27    self._executable_path = None
28
29    super().__init__(config_path, logger, base_configs=base_configs)
30
31    self._executable_path = shutil.which("tcpdump")
32    if self._executable_path is None:
33      raise sniffer.SnifferError(
34          "Cannot find a path to the 'tcpdump' executable")
35
36  def get_descriptor(self):
37    """See base class documentation
38        """
39    return "local-tcpdump-{}".format(self._interface)
40
41  def get_subtype(self):
42    """See base class documentation
43        """
44    return "tcpdump"
45
46  def _get_command_line(self,
47                        additional_args=None,
48                        duration=None,
49                        packet_count=None):
50    cmd = "{} -i {} -w {}".format(self._executable_path, self._interface,
51                                  self._temp_capture_file_path)
52    if packet_count is not None:
53      cmd = "{} -c {}".format(cmd, packet_count)
54    if additional_args is not None:
55      cmd = "{} {}".format(cmd, additional_args)
56    return cmd
57