From e5ce000399b6760fc885471ba404d97075a18bb8 Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 26 Aug 2015 20:03:33 +0200 Subject: [PATCH] Improve the `ssh_known_hosts` rule import time --- thefuck/rules/ssh_known_hosts.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/thefuck/rules/ssh_known_hosts.py b/thefuck/rules/ssh_known_hosts.py index df908d00..6d666fcb 100644 --- a/thefuck/rules/ssh_known_hosts.py +++ b/thefuck/rules/ssh_known_hosts.py @@ -1,25 +1,19 @@ import re -patterns = [ - r'WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!', - r'WARNING: POSSIBLE DNS SPOOFING DETECTED!', - r"Warning: the \S+ host key for '([^']+)' differs from the key for the IP address '([^']+)'", -] -offending_pattern = re.compile( - r'(?:Offending (?:key for IP|\S+ key)|Matching host key) in ([^:]+):(\d+)', - re.MULTILINE) - -commands = ['ssh', 'scp'] - def match(command, settings): if not command.script: return False - if not command.script.split()[0] in commands: + if not command.script.startswith(('ssh', 'scp')): return False - if not any([re.findall(pattern, command.stderr) for pattern in patterns]): - return False - return True + + patterns = ( + r'WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!', + r'WARNING: POSSIBLE DNS SPOOFING DETECTED!', + r"Warning: the \S+ host key for '([^']+)' differs from the key for the IP address '([^']+)'", + ) + + return any(re.findall(pattern, command.stderr) for pattern in patterns) def get_new_command(command, settings): @@ -27,6 +21,9 @@ def get_new_command(command, settings): def side_effect(old_cmd, command, settings): + offending_pattern = re.compile( + r'(?:Offending (?:key for IP|\S+ key)|Matching host key) in ([^:]+):(\d+)', + re.MULTILINE) offending = offending_pattern.findall(old_cmd.stderr) for filepath, lineno in offending: with open(filepath, 'r') as fh: