Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve regexp for ip address in ping and ssh #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions kippo/commands/ping.py
Original file line number Diff line number Diff line change
@@ -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))
14 changes: 12 additions & 2 deletions kippo/commands/ssh.py
Original file line number Diff line number Diff line change
@@ -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