We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
pinging a domain crashes
From within cowrie (docker current 7 Dec 2018), when the you ping a domain (not an IP) a stack trace errors shown below.  Cowrie freezes., and since many bots use ping as info gathering/sandbox detection, cowry may miss everything subsequent to this command.

Environment:
Ubuntu 16.04
Python 3.5.2
docker pull cowrie/cowrie
sudo iptables -t nat -A PREROUTING -p tcp --dport 22 -j REDIRECT --to-port 2222
docker run -it -p 2222:2222 -p 2223:2223 cowrie/cowrie


```
2018-12-07T04:56:59+0000 [SSHChannel session (0) on SSHService b'ssh-connection' on HoneyPotSSHTransport,2,172.17.0.1] gett
ing shell
2018-12-07T04:57:07+0000 [SSHChannel session (0) on SSHService b'ssh-connection' on HoneyPotSSHTransport,2,172.17.0.1] CMD:
 **ping google.com**
2018-12-07T04:57:07+0000 [SSHChannel session (0) on SSHService b'ssh-connection' on HoneyPotSSHTransport,2,172.17.0.1] Comm
and found: ping google.com
2018-12-07T04:57:07+0000 [SSHChannel session (0) on SSHService b'ssh-connection' on HoneyPotSSHTransport,2,172.17.0.1] Unha
ndled Error
        Traceback (most recent call last):
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/context.py", line 122, in callWithContext
            return self.currentContext().callWithContext(ctx, func, *args, **kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/context.py", line 85, in callWithContext
            return func(*args,**kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/ssh/service.py", line 45, in packetReceived
            return f(packet)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/ssh/connection.py", line 249, in ssh_CHANNEL_D
ATA
            log.callWithLogger(channel, channel.dataReceived, data)
        --- <exception caught here> ---
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/log.py", line 103, in callWithLogger
            return callWithContext({"system": lp}, func, *args, **kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/log.py", line 86, in callWithContext
            return context.call({ILogContext: newCtx}, func, *args, **kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/context.py", line 122, in callWithContext
            return self.currentContext().callWithContext(ctx, func, *args, **kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/context.py", line 85, in callWithContext
            return func(*args,**kw)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/ssh/session.py", line 112, in dataReceived
            self.client.transport.write(data)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/ssh/session.py", line 163, in write
            self.proto.dataReceived(data)
          File "/cowrie/cowrie-git/src/cowrie/insults/insults.py", line 104, in dataReceived
            insults.ServerProtocol.dataReceived(self, data)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/insults/insults.py", line 537, in dataReceived
            self.terminalProtocol.keystrokeReceived(ch, None)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/recvline.py", line 225, in keystrokeReceived
            m()
          File "/cowrie/cowrie-git/src/cowrie/shell/protocol.py", line 325, in handle_RETURN
            return recvline.RecvLine.handle_RETURN(self)
          File "/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/recvline.py", line 292, in handle_RETURN
            self.lineReceived(line)
          File "/cowrie/cowrie-git/src/cowrie/shell/protocol.py", line 185, in lineReceived
            self.cmdstack[-1].lineReceived(line)
          File "/cowrie/cowrie-git/src/cowrie/shell/honeypot.py", line 106, in lineReceived
            self.runCommand()
          File "/cowrie/cowrie-git/src/cowrie/shell/honeypot.py", line 215, in runCommand
            self.protocol.call_command(pp, cmdclass, *cmd_array[0]['rargs'])
          File "/cowrie/cowrie-git/src/cowrie/shell/protocol.py", line 306, in call_command
            HoneyPotBaseProtocol.call_command(self, pp, cmd, *args)
          File "/cowrie/cowrie-git/src/cowrie/shell/protocol.py", line 194, in call_command
            obj.start()
          File "/cowrie/cowrie-git/src/cowrie/commands/ping.py", line 70, in start
            s = hashlib.md5(self.host).hexdigest()
        builtins.TypeError: Unicode-objects must be encoded before hashing
```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/cowrie/commands/ping.py`
Content:
```
1 # Copyright (c) 2009 Upi Tamminen <desaster@gmail.com>
2 # See the COPYRIGHT file for more information
3 
4 from __future__ import absolute_import, division
5 
6 import getopt
7 import hashlib
8 import random
9 import re
10 import socket
11 
12 from twisted.internet import reactor
13 
14 from cowrie.shell.command import HoneyPotCommand
15 
16 commands = {}
17 
18 
19 class command_ping(HoneyPotCommand):
20 
21     def valid_ip(self, address):
22         try:
23             socket.inet_aton(address)
24             return True
25         except Exception:
26             return False
27 
28     def start(self):
29         self.host = None
30         self.max = 0
31         self.running = False
32 
33         try:
34             optlist, args = getopt.gnu_getopt(self.args, "c:")
35         except getopt.GetoptError as err:
36             self.write('ping: %s\n' % (err,))
37             self.exit()
38             return
39 
40         for opt in optlist:
41             if opt[0] == '-c':
42                 try:
43                     self.max = int(opt[1])
44                 except Exception:
45                     self.max = 0
46                 if self.max <= 0:
47                     self.write('ping: bad number of packets to transmit.\n')
48                     self.exit()
49                     return
50 
51         if len(args) == 0:
52             for l in (
53                     'Usage: ping [-LRUbdfnqrvVaA] [-c count] [-i interval] [-w deadline]',
54                     '            [-p pattern] [-s packetsize] [-t ttl] [-I interface or address]',
55                     '            [-M mtu discovery hint] [-S sndbuf]',
56                     '            [ -T timestamp option ] [ -Q tos ] [hop1 ...] destination',
57             ):
58                 self.write('{0}\n'.format(l))
59             self.exit()
60             return
61         self.host = args[0].strip()
62 
63         if re.match('^[0-9.]+$', self.host):
64             if self.valid_ip(self.host):
65                 self.ip = self.host
66             else:
67                 self.write('ping: unknown host %s\n' % (self.host,))
68                 self.exit()
69         else:
70             s = hashlib.md5(self.host).hexdigest()
71             self.ip = '.'.join([str(int(x, 16)) for x in (s[0:2], s[2:4], s[4:6], s[6:8])])
72 
73         self.running = True
74         self.write('PING %s (%s) 56(84) bytes of data.\n' % (self.host, self.ip))
75         self.scheduled = reactor.callLater(0.2, self.showreply)
76         self.count = 0
77 
78     def showreply(self):
79         ms = 40 + random.random() * 10
80         self.write(
81             '64 bytes from {} ({}): icmp_seq={} ttl=50 time={:.1f} ms\n'.format(self.host, self.ip, self.count + 1, ms))
82         self.count += 1
83         if self.count == self.max:
84             self.running = False
85             self.write('\n')
86             self.printstatistics()
87             self.exit()
88         else:
89             self.scheduled = reactor.callLater(1, self.showreply)
90 
91     def printstatistics(self):
92         self.write('--- %s ping statistics ---\n' % (self.host,))
93         self.write('%d packets transmitted, %d received, 0%% packet loss, time 907ms\n' % (self.count, self.count))
94         self.write('rtt min/avg/max/mdev = 48.264/50.352/52.441/2.100 ms\n')
95 
96     def handle_CTRL_C(self):
97         if self.running is False:
98             return HoneyPotCommand.handle_CTRL_C(self)
99         else:
100             self.write('^C\n')
101             self.scheduled.cancel()
102             self.printstatistics()
103             self.exit()
104 
105 
106 commands['/bin/ping'] = command_ping
107 commands['ping'] = command_ping
108
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/src/cowrie/commands/ping.py b/src/cowrie/commands/ping.py
--- a/src/cowrie/commands/ping.py
+++ b/src/cowrie/commands/ping.py
@@ -67,7 +67,7 @@
                 self.write('ping: unknown host %s\n' % (self.host,))
                 self.exit()
         else:
-            s = hashlib.md5(self.host).hexdigest()
+            s = hashlib.md5((self.host).encode("utf-8")).hexdigest()
             self.ip = '.'.join([str(int(x, 16)) for x in (s[0:2], s[2:4], s[4:6], s[6:8])])
 
         self.running = True

{"golden_diff": "diff --git a/src/cowrie/commands/ping.py b/src/cowrie/commands/ping.py\n--- a/src/cowrie/commands/ping.py\n+++ b/src/cowrie/commands/ping.py\n@@ -67,7 +67,7 @@\n                 self.write('ping: unknown host %s\\n' % (self.host,))\n                 self.exit()\n         else:\n-            s = hashlib.md5(self.host).hexdigest()\n+            s = hashlib.md5((self.host).encode(\"utf-8\")).hexdigest()\n             self.ip = '.'.join([str(int(x, 16)) for x in (s[0:2], s[2:4], s[4:6], s[6:8])])\n \n         self.running = True\n", "issue": "pinging a domain crashes\nFrom within cowrie (docker current 7 Dec 2018), when the you ping a domain (not an IP) a stack trace errors shown below.  Cowrie freezes., and since many bots use ping as info gathering/sandbox detection, cowry may miss everything subsequent to this command.\r\n\r\nEnvironment:\r\nUbuntu 16.04\r\nPython 3.5.2\r\ndocker pull cowrie/cowrie\r\nsudo iptables -t nat -A PREROUTING -p tcp --dport 22 -j REDIRECT --to-port 2222\r\ndocker run -it -p 2222:2222 -p 2223:2223 cowrie/cowrie\r\n\r\n\r\n```\r\n2018-12-07T04:56:59+0000 [SSHChannel session (0) on SSHService b'ssh-connection' on HoneyPotSSHTransport,2,172.17.0.1] gett\r\ning shell\r\n2018-12-07T04:57:07+0000 [SSHChannel session (0) on SSHService b'ssh-connection' on HoneyPotSSHTransport,2,172.17.0.1] CMD:\r\n **ping google.com**\r\n2018-12-07T04:57:07+0000 [SSHChannel session (0) on SSHService b'ssh-connection' on HoneyPotSSHTransport,2,172.17.0.1] Comm\r\nand found: ping google.com\r\n2018-12-07T04:57:07+0000 [SSHChannel session (0) on SSHService b'ssh-connection' on HoneyPotSSHTransport,2,172.17.0.1] Unha\r\nndled Error\r\n        Traceback (most recent call last):\r\n          File \"/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/context.py\", line 122, in callWithContext\r\n            return self.currentContext().callWithContext(ctx, func, *args, **kw)\r\n          File \"/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/context.py\", line 85, in callWithContext\r\n            return func(*args,**kw)\r\n          File \"/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/ssh/service.py\", line 45, in packetReceived\r\n            return f(packet)\r\n          File \"/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/ssh/connection.py\", line 249, in ssh_CHANNEL_D\r\nATA\r\n            log.callWithLogger(channel, channel.dataReceived, data)\r\n        --- <exception caught here> ---\r\n          File \"/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/log.py\", line 103, in callWithLogger\r\n            return callWithContext({\"system\": lp}, func, *args, **kw)\r\n          File \"/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/log.py\", line 86, in callWithContext\r\n            return context.call({ILogContext: newCtx}, func, *args, **kw)\r\n          File \"/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/context.py\", line 122, in callWithContext\r\n            return self.currentContext().callWithContext(ctx, func, *args, **kw)\r\n          File \"/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/python/context.py\", line 85, in callWithContext\r\n            return func(*args,**kw)\r\n          File \"/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/ssh/session.py\", line 112, in dataReceived\r\n            self.client.transport.write(data)\r\n          File \"/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/ssh/session.py\", line 163, in write\r\n            self.proto.dataReceived(data)\r\n          File \"/cowrie/cowrie-git/src/cowrie/insults/insults.py\", line 104, in dataReceived\r\n            insults.ServerProtocol.dataReceived(self, data)\r\n          File \"/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/insults/insults.py\", line 537, in dataReceived\r\n            self.terminalProtocol.keystrokeReceived(ch, None)\r\n          File \"/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/recvline.py\", line 225, in keystrokeReceived\r\n            m()\r\n          File \"/cowrie/cowrie-git/src/cowrie/shell/protocol.py\", line 325, in handle_RETURN\r\n            return recvline.RecvLine.handle_RETURN(self)\r\n          File \"/cowrie/cowrie-env/lib/python3.5/site-packages/twisted/conch/recvline.py\", line 292, in handle_RETURN\r\n            self.lineReceived(line)\r\n          File \"/cowrie/cowrie-git/src/cowrie/shell/protocol.py\", line 185, in lineReceived\r\n            self.cmdstack[-1].lineReceived(line)\r\n          File \"/cowrie/cowrie-git/src/cowrie/shell/honeypot.py\", line 106, in lineReceived\r\n            self.runCommand()\r\n          File \"/cowrie/cowrie-git/src/cowrie/shell/honeypot.py\", line 215, in runCommand\r\n            self.protocol.call_command(pp, cmdclass, *cmd_array[0]['rargs'])\r\n          File \"/cowrie/cowrie-git/src/cowrie/shell/protocol.py\", line 306, in call_command\r\n            HoneyPotBaseProtocol.call_command(self, pp, cmd, *args)\r\n          File \"/cowrie/cowrie-git/src/cowrie/shell/protocol.py\", line 194, in call_command\r\n            obj.start()\r\n          File \"/cowrie/cowrie-git/src/cowrie/commands/ping.py\", line 70, in start\r\n            s = hashlib.md5(self.host).hexdigest()\r\n        builtins.TypeError: Unicode-objects must be encoded before hashing\r\n```\r\n\n", "before_files": [{"content": "# Copyright (c) 2009 Upi Tamminen <desaster@gmail.com>\n# See the COPYRIGHT file for more information\n\nfrom __future__ import absolute_import, division\n\nimport getopt\nimport hashlib\nimport random\nimport re\nimport socket\n\nfrom twisted.internet import reactor\n\nfrom cowrie.shell.command import HoneyPotCommand\n\ncommands = {}\n\n\nclass command_ping(HoneyPotCommand):\n\n    def valid_ip(self, address):\n        try:\n            socket.inet_aton(address)\n            return True\n        except Exception:\n            return False\n\n    def start(self):\n        self.host = None\n        self.max = 0\n        self.running = False\n\n        try:\n            optlist, args = getopt.gnu_getopt(self.args, \"c:\")\n        except getopt.GetoptError as err:\n            self.write('ping: %s\\n' % (err,))\n            self.exit()\n            return\n\n        for opt in optlist:\n            if opt[0] == '-c':\n                try:\n                    self.max = int(opt[1])\n                except Exception:\n                    self.max = 0\n                if self.max <= 0:\n                    self.write('ping: bad number of packets to transmit.\\n')\n                    self.exit()\n                    return\n\n        if len(args) == 0:\n            for l in (\n                    'Usage: ping [-LRUbdfnqrvVaA] [-c count] [-i interval] [-w deadline]',\n                    '            [-p pattern] [-s packetsize] [-t ttl] [-I interface or address]',\n                    '            [-M mtu discovery hint] [-S sndbuf]',\n                    '            [ -T timestamp option ] [ -Q tos ] [hop1 ...] destination',\n            ):\n                self.write('{0}\\n'.format(l))\n            self.exit()\n            return\n        self.host = args[0].strip()\n\n        if re.match('^[0-9.]+$', self.host):\n            if self.valid_ip(self.host):\n                self.ip = self.host\n            else:\n                self.write('ping: unknown host %s\\n' % (self.host,))\n                self.exit()\n        else:\n            s = hashlib.md5(self.host).hexdigest()\n            self.ip = '.'.join([str(int(x, 16)) for x in (s[0:2], s[2:4], s[4:6], s[6:8])])\n\n        self.running = True\n        self.write('PING %s (%s) 56(84) bytes of data.\\n' % (self.host, self.ip))\n        self.scheduled = reactor.callLater(0.2, self.showreply)\n        self.count = 0\n\n    def showreply(self):\n        ms = 40 + random.random() * 10\n        self.write(\n            '64 bytes from {} ({}): icmp_seq={} ttl=50 time={:.1f} ms\\n'.format(self.host, self.ip, self.count + 1, ms))\n        self.count += 1\n        if self.count == self.max:\n            self.running = False\n            self.write('\\n')\n            self.printstatistics()\n            self.exit()\n        else:\n            self.scheduled = reactor.callLater(1, self.showreply)\n\n    def printstatistics(self):\n        self.write('--- %s ping statistics ---\\n' % (self.host,))\n        self.write('%d packets transmitted, %d received, 0%% packet loss, time 907ms\\n' % (self.count, self.count))\n        self.write('rtt min/avg/max/mdev = 48.264/50.352/52.441/2.100 ms\\n')\n\n    def handle_CTRL_C(self):\n        if self.running is False:\n            return HoneyPotCommand.handle_CTRL_C(self)\n        else:\n            self.write('^C\\n')\n            self.scheduled.cancel()\n            self.printstatistics()\n            self.exit()\n\n\ncommands['/bin/ping'] = command_ping\ncommands['ping'] = command_ping\n", "path": "src/cowrie/commands/ping.py"}], "after_files": [{"content": "# Copyright (c) 2009 Upi Tamminen <desaster@gmail.com>\n# See the COPYRIGHT file for more information\n\nfrom __future__ import absolute_import, division\n\nimport getopt\nimport hashlib\nimport random\nimport re\nimport socket\n\nfrom twisted.internet import reactor\n\nfrom cowrie.shell.command import HoneyPotCommand\n\ncommands = {}\n\n\nclass command_ping(HoneyPotCommand):\n\n    def valid_ip(self, address):\n        try:\n            socket.inet_aton(address)\n            return True\n        except Exception:\n            return False\n\n    def start(self):\n        self.host = None\n        self.max = 0\n        self.running = False\n\n        try:\n            optlist, args = getopt.gnu_getopt(self.args, \"c:\")\n        except getopt.GetoptError as err:\n            self.write('ping: %s\\n' % (err,))\n            self.exit()\n            return\n\n        for opt in optlist:\n            if opt[0] == '-c':\n                try:\n                    self.max = int(opt[1])\n                except Exception:\n                    self.max = 0\n                if self.max <= 0:\n                    self.write('ping: bad number of packets to transmit.\\n')\n                    self.exit()\n                    return\n\n        if len(args) == 0:\n            for l in (\n                    'Usage: ping [-LRUbdfnqrvVaA] [-c count] [-i interval] [-w deadline]',\n                    '            [-p pattern] [-s packetsize] [-t ttl] [-I interface or address]',\n                    '            [-M mtu discovery hint] [-S sndbuf]',\n                    '            [ -T timestamp option ] [ -Q tos ] [hop1 ...] destination',\n            ):\n                self.write('{0}\\n'.format(l))\n            self.exit()\n            return\n        self.host = args[0].strip()\n\n        if re.match('^[0-9.]+$', self.host):\n            if self.valid_ip(self.host):\n                self.ip = self.host\n            else:\n                self.write('ping: unknown host %s\\n' % (self.host,))\n                self.exit()\n        else:\n            s = hashlib.md5((self.host).encode(\"utf-8\")).hexdigest()\n            self.ip = '.'.join([str(int(x, 16)) for x in (s[0:2], s[2:4], s[4:6], s[6:8])])\n\n        self.running = True\n        self.write('PING %s (%s) 56(84) bytes of data.\\n' % (self.host, self.ip))\n        self.scheduled = reactor.callLater(0.2, self.showreply)\n        self.count = 0\n\n    def showreply(self):\n        ms = 40 + random.random() * 10\n        self.write(\n            '64 bytes from {} ({}): icmp_seq={} ttl=50 time={:.1f} ms\\n'.format(self.host, self.ip, self.count + 1, ms))\n        self.count += 1\n        if self.count == self.max:\n            self.running = False\n            self.write('\\n')\n            self.printstatistics()\n            self.exit()\n        else:\n            self.scheduled = reactor.callLater(1, self.showreply)\n\n    def printstatistics(self):\n        self.write('--- %s ping statistics ---\\n' % (self.host,))\n        self.write('%d packets transmitted, %d received, 0%% packet loss, time 907ms\\n' % (self.count, self.count))\n        self.write('rtt min/avg/max/mdev = 48.264/50.352/52.441/2.100 ms\\n')\n\n    def handle_CTRL_C(self):\n        if self.running is False:\n            return HoneyPotCommand.handle_CTRL_C(self)\n        else:\n            self.write('^C\\n')\n            self.scheduled.cancel()\n            self.printstatistics()\n            self.exit()\n\n\ncommands['/bin/ping'] = command_ping\ncommands['ping'] = command_ping\n", "path": "src/cowrie/commands/ping.py"}]}