-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh_known_hosts.py
39 lines (31 loc) · 1.1 KB
/
ssh_known_hosts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import re
from thefuck.utils import for_app
commands = ('ssh', 'scp', 'root')
@for_app(*commands)
def match(command):
if not command.script:
return False
if not command.script.startswith(commands):
return False
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.output) for pattern in patterns)
def get_new_command(command):
return command.script
def side_effect(old_cmd, command):
offending_pattern = re.compile(
r'(?:Offending (?:key for IP|\S+ key)|Matching host key) in ([^:]+):(\d+)',
re.MULTILINE)
offending = offending_pattern.findall(old_cmd.output)
for filepath, lineno in offending:
with open(filepath, 'r') as fh:
lines = fh.readlines()
del lines[int(lineno) - 1]
with open(filepath, 'w') as fh:
fh.writelines(lines)
# Optional:
priority = 5
enabled_by_default = True