啥是xinetd?
网上的解释是:
在linux中一些不长期使用的服务(不重要的服务?)没有被作为单独的守护进程在开机时启用,linux把这些服务监听端口全部由一个独立的进程xinetd集中监听,当收到相应的客户端请求之后,xinetd进程就临时启动相应服务并把相应端口移交给相应服务,客户端断开之后,相应的服务进程结束,xinetd继续监听。要开telnet服务器,只需配置xinetd即可。
也就是说是我们自己配置的服务。
我们先写一段python的服务代码:
xinetdserver.py:
#!/usr/bin/env python
import sys
print "Welcome."
print "Please enter a string: "
sys.stdout.flush()
line = sys.stdin.readline()
print "You entered %d characters." % len(line)
提供简单的服务...
zoo@zoo:~$ sudo gedit /etc/xinetd.conf
打开配置文件。加入如下服务配置信息:
service test
{
flags = NAMEINARGS
type = UNLISTED
port = 54321
socket_type = stream
protocol = tcp
wait = no
user = zoo
server = /home/zoo/xinetdserver.py
server_args = /home/zoo/xinetdserver.py
}
其中
type = UNLISTED 表示不在/etc/services 中的服务
port = 54321表示端口号
socket_type = stream 使用tcp
wait = no 表示tcp服务
user 表示运行的用户,我用的是zoo
server和server_args 表示服务的相关路径和参数
写完配置后保存。
使用:
zoo@zoo:~$ service xinetd restart
重启服务.
查看运行情况:
zoo@zoo:~$ netstat -a | grep 54321
tcp 0 0 *:54321 *:* LISTEN
tcp 0 0 localhost:54321 localhost:58958 TIME_WAIT
使用服务:
zoo@zoo:~$ telnet localhost 54321
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Welcome.
Please enter a string:
1234
You entered 6 characters.
Connection closed by foreign host.
只是不知道为啥结果不对...
PS: 一次不一定能做出来,如果不对就仔细检查一下吧...