def pty_connect(device, action, creds=None, display_banner=None, ping_test=False, init_commands=None): """ Connect to a device and log in. Use SSHv2 or telnet as appropriate. :param device: A :class:`~trigger.netdevices.NetDevice` object. :param action: A Protocol object (not class) that will be activated when the session is ready. :param creds: is a 2-tuple (username, password). By default, .tacacsrc AOL credentials will be used. Override that here. :param display_banner: Will be called for SSH pre-authentication banners. It will receive two args, 'banner' and 'language'. By default, nothing will be done with the banner. :param ping_test: If set, the device is pinged and succeed in order to proceed. :param init_commands: A list of commands to execute upon logging into the device. """ d = defer.Deferred() # Only proceed if ping succeeds if ping_test: log.msg('Pinging %s' % device, debug=True) if not ping(device.nodeName): log.msg('Ping to %s failed' % device, debug=True) return None # SSH? log.msg('SSH TYPES: %s' % settings.SSH_TYPES, debug=True) if device.manufacturer in settings.SSH_TYPES: if hasattr(sys, 'ps1') or not sys.stderr.isatty() \ or not sys.stdin.isatty() or not sys.stdout.isatty(): # Shell not in interactive mode. pass else: if not creds and device.is_firewall(): creds = tacacsrc.get_device_password(str(device)) factory = TriggerSSHPtyClientFactory(d, action, creds, display_banner, init_commands) log.msg('Trying SSH to %s' % device, debug=True) reactor.connectTCP(device.nodeName, 22, factory) # or Telnet? else: factory = TriggerTelnetClientFactory(d, action, creds, init_commands=init_commands) log.msg('Trying telnet to %s' % device, debug=True) reactor.connectTCP(device.nodeName, 23, factory) return d
def is_reachable(self): """Do I respond to a ping?""" return network.ping(self.nodeName)
def test_ping(): """Validate network.ping functionality""" assert ping("localhost") assert not ping("unresolvable_test_host")