Telnet客户端连接被拒绝 - JAVA

时间:2015-09-29 06:13:28

标签: java telnet

我正在尝试创建一个到全局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);

有什么建议吗?(我在论坛中找不到相同的错误,我也是新问题:))

2 个答案:

答案 0 :(得分:0)

这是我的完整代码;

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 :(得分:0)

1.安装telnet在终端(应用程序/附件/终端)中使用此命令:

sudo apt-get install xinetd telnetd

2.使用您喜欢的文件编辑器以root权限编辑/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