-
Notifications
You must be signed in to change notification settings - Fork 7
/
mock_irc.py
executable file
·52 lines (43 loc) · 1.3 KB
/
mock_irc.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
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
# SPDX-License-Identifier: GPL-3.0-or-later
# -*- coding: utf-8 -*-
import logging
import socket
import sys
def main():
if len(sys.argv) != 4:
print("\n Usage: " + sys.argv[0] + " <port> <logfile> <banner>")
print(
" eg.: "
+ sys.argv[0]
+ " 6667 irc_6667.log 'irc-hs NOTICE * :*** Looking up your hostname...'\n"
)
sys.exit(1)
port = sys.argv[1]
logfile = sys.argv[2]
banner = sys.argv[3]
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(levelname)s %(message)s",
filename=logfile,
filemode="a",
)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("", int(port)))
s.listen(5)
while 1:
try:
clientsock, clientaddr = s.accept()
clienthost = clientsock.getpeername()[0]
clientport = clientsock.getpeername()[1]
clientsock.send(banner)
resp = clientsock.recv(1024)
clientsock.close()
logging.info(clienthost + ":" + str(clientport) + " " + resp)
except KeyboardInterrupt:
sys.exit(1)
except Exception:
continue
if __name__ == "__main__":
main()