[英]Telnet Client connection refused - JAVA
我正在尝试创建到全局IP地址的telnet客户端(apache)连接。
如果我使用下面的方法,则可以建立连接。
private TelnetClient telnet = new TelnetClient();
telnet.connect("172.xx.xxx.xx", port);
但是,像下面这样写,我得到“连接被拒绝错误”。
private TelnetClient telnet = new TelnetClient();
String host = "172.xx.xxx.xx";
telnet.connect(host, port);
有什么建议吗?(我在论坛上找不到相同的错误,也是我刚问问题:))
这是我的完整代码;
public void connectionCreater(String host, int port,String uID,String pass,
String account, String password) {
try {
//telnet.connect("172.xx.xxx.xx", port); this is works.
telnet.connect(host, port);
out = new PrintWriter(telnet.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
telnet.getInputStream()));
if (readUntilThenExecute("login: ", uID + "\r")) {
if (readUntilThenExecute("Password: ", pass + "\r")) {
if (readUntilThenExecute("Enter User Name", account)) {
if (readUntilThenExecute("Enter Password", password)) {
//to do stuff }
}
}
}
} catch (IOException e) {
out.close();
try {
in.close();
} catch (IOException y) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public boolean readUntilThenExecute(String word, String command) {
try {
String result1 = "";
char[] incoming = new char[2048];
boolean check = true;
while (check) {
int lenght = in.read(incoming);
result1 = String.copyValueOf(incoming, 0, lenght);
System.out.println(result1);
if (result1.contains(word)) {
out.println(command);
check = false;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
1.在终端(应用程序/附件/终端)中使用以下命令安装telnet:
sudo apt-get install xinetd telnetd
2.使用您最喜欢的具有根权限的文件编辑器编辑/etc/inetd.conf,添加以下行:
telnet stream tcp nowait telnetd /usr/sbin/tcpd /usr/sbin/in.telnetd
3.编辑/etc/xinetd.conf,使其内容如下所示:
# Simple configuration file for xinetd
#
# Some defaults, and include /etc/xinetd.d/
defaults
{
# Please note that you need a log_type line to be able to use log_on_success
# and log_on_failure. The default is the following :
# log_type = SYSLOG daemon info
instances = 60
log_type = SYSLOG authpriv
log_on_success = HOST PID
log_on_failure = HOST
cps = 25 30
}
4.您可以通过编辑/ etc / services更改以下行来更改telnet端口号:
telnet 23/tcp
5,如果您对默认配置不满意,请编辑etc/xinetd.d/telnet
,添加以下内容:
# default: on
# description: The telnet server serves telnet sessions; it uses
# unencrypted username/password pairs for authentication.
service telnet
{
disable = no
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/sbin/in.telnetd
log_on_failure += USERID
}
根据需要添加以下行:
only_from = 192.168.120.0/24 #Only users in 192.168.120.0 can access to
only_from = .bob.com #allow access from bob.com
no_access = 192.168.120.{101,105} #not allow access from the two IP.
access_times = 8:00-9:00 20:00-21:00 #allow access in the two times
......
6,使用以下命令启动telnet服务器:
sudo /etc/init.d/xinetd start
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.