From efc190093f8fb876ffec2a8e7fc9590c0ae21bc3 Mon Sep 17 00:00:00 2001 From: Katarina Durechova Date: Fri, 22 Aug 2014 12:48:38 +0100 Subject: [PATCH] Improve regexp for ip address in ping and ssh + accept hostname address only if ends with 2 alphabetic characters --- kippo/commands/ping.py | 12 +++++++++--- kippo/commands/ssh.py | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/kippo/commands/ping.py b/kippo/commands/ping.py index 2fed8c6..5a6057f 100644 --- a/kippo/commands/ping.py +++ b/kippo/commands/ping.py @@ -26,13 +26,19 @@ def start(self): self.exit() return - if re.match('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$', - self.host): + if re.match( + '^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$', + self.host + ): self.ip = self.host - else: + elif re.match('.*[a-z]{2}$', self.host, re.I): s = hashlib.md5(self.host).hexdigest() self.ip = '.'.join([str(int(x, 16)) for x in (s[0:2], s[2:4], s[4:6], s[6:8])]) + else: + self.writeln('ping: unknown host %s' % (self.host)) + self.exit() + return self.writeln('PING %s (%s) 56(84) bytes of data.' % \ (self.host, self.ip)) diff --git a/kippo/commands/ssh.py b/kippo/commands/ssh.py index 7659666..41bea00 100644 --- a/kippo/commands/ssh.py +++ b/kippo/commands/ssh.py @@ -34,12 +34,22 @@ def start(self): if args[0].count('@'): user, host = args[0].split('@', 1) - if re.match('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$', host): + if re.match( + '^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$', + host + ): self.ip = host - else: + elif re.match('.*[a-z]{2}$', host, re.I): s = hashlib.md5(host).hexdigest() self.ip = '.'.join([str(int(x, 16)) for x in (s[0:2], s[2:4], s[4:6], s[6:8])]) + else: + self.writeln( + 'ssh: Could not resolve hostname %s: Name or service not known' + % (host)) + self.exit() + return + self.host = host self.user = user