【问题标题】:Telnet Client connection refused - JAVATelnet 客户端连接被拒绝 - JAVA
【发布时间】:2015-09-29 06:13:27
【问题描述】:

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

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

【问题讨论】:

  • 您尝试连接的内容是否接受 telnet 连接?
  • 你的端口是一样的吗?当你重写代码?
  • 是同一个端口。此代码用于向服务器发送一些命令/获取响应。此代码在创建连接后旁边。当我使用 "telnet.connect("172.xx.xxx.xx", port);"一切正常:/。 out = new PrintWriter(telnet.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(telnet.getInputStream()));
  • 张贴完整代码,在某个地方有错字,或者您在另一个环境/系统上使用“connect(host, port)”?

标签: java telnet


【解决方案1】:

这是我的完整代码;

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;
        }       

【讨论】:

  • 感谢关注我解决了这个问题。我的同事更改了数据库主机记录,我只是在调试中看到...thx
【解决方案2】:

1.在终端(Applications/Accessories/Terminal)中使用此命令安装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

【讨论】:

    猜你喜欢
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 2019-02-28
    • 2023-04-01
    • 1970-01-01
    • 2014-01-10
    • 2021-10-16
    相关资源
    最近更新 更多