-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudpnat.py
More file actions
65 lines (62 loc) · 2.13 KB
/
Copy pathudpnat.py
File metadata and controls
65 lines (62 loc) · 2.13 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import socket
import sys
import logging
import os
import datetime
import select
def daemon(pidfile) :
try:
if os.fork() > 0 :
sys.exit(0)
except OSError, e:
logging.error("try to start daemon failed!")
try:
pid = open(pidfile,'w')
pid.write('%d\n' % (os.getpid()))
pid.flush()
pid.close()
except IOError,e:
logging.error('cannot open this file:%s' % (pidfile))
sys.exit(1)
for f in sys.stdout, sys.stderr:
f.flush()
si = open(os.devnull, 'r')
so = open(os.devnull, 'a+')
se = open(os.devnull, 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
def trans(port,host) :
try:
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock_send = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind(("0.0.0.0",port))
except:
print("bind addreess failed,please check!")
sys.exit(1)
readlist = []
readlist.append(sock)
s_addr = (host,port)
while True :
try:
can_read,can_write,can_x = select.select(readlist,[],[],100)
if len(can_read) > 0 :
for s in can_read :
data,addr = s.recvfrom(1024)
sock_send.sendto(data,s_addr)
except (socket.error,socket.timeout),e:
logging.error(e)
pass
except select.error,e:
logging.error(e)
if __name__ == '__main__' :
logfile = 'syslog-%s.log'%(datetime.datetime.now().strftime('%y%m%d%H%M%S'))
logging.basicConfig(level=logging.INFO,
format='%(levelname)s %(asctime)s %(filename)s[line:%(lineno)d] -> %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename=logfile,
filemode='w')
daemon('udpnat.pid')
trans(55555,'local.changer.site')