如何从本地web浏览器连接到远程telnet服务器并发送预定义命令


How to connect from local web browser to remote telnet server and send predefined commands?

我有一个使用php/javascript运行的呼叫中心应用程序。当操作员在他的mac或windows或linux与web浏览器,如Firefox/Chrome/Opera/Internet explorer。我需要做一个实时连接到远程服务器发送命令,如"打call"。

但是我如何使用javascript telnet连接到远程服务器?这是可能的javascript,如果这样,如何和哪些浏览器?

感谢

注意:不使用

http://matthaynes.net/blog/2008/07/17/socketbridge-flash-javascript-socket-bridge/

1)http://stephengware.com/proj/javasocketbridge/

2)

后续:只有我捕获这个数据包,而使用Chrome?(Firefox/Opera/Midori/Safari,没有工作)。

# tcpdump -n -x -X -i any port 23
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes
16:37:00.446070 IP 1.164.45.143.56295 > 1.164.45.143.telnet: Flags [S], seq 205310205, win 32792, options [mss 16396,sackOK,TS val 37248521 ecr 0,nop,wscale 6], length 0
    0x0000:  4500 003c 96f4 4000 4006 a561 51a4 2d8f  E..<..@.@..aQ.-.
    0x0010:  51a4 2d8f dbe7 0017 0c3c c8fd 0000 0000  Q.-......<......
    0x0020:  a002 8018 fe94 0000 0204 400c 0402 080a  ..........@.....
    0x0030:  0238 5e09 0000 0000 0103 0306            .8^.........
16:37:00.446109 IP 1.164.45.143.telnet > 1.164.45.143.56295: Flags [R.], seq 0, ack 205310206, win 0, length 0
    0x0000:  4500 0028 0000 4000 4006 3c6a 51a4 2d8f  E..(..@.@.<jQ.-.
    0x0010:  51a4 2d8f 0017 dbe7 0000 0000 0c3c c8fe  Q.-..........<..
    0x0020:  5014 0000 0031 0000                      P....1..
16:37:00.446555 IP 1.164.45.143.56296 > 1.164.45.143.telnet: Flags [S], seq 196642802, win 32792, options [mss 16396,sackOK,TS val 37248521 ecr 0,nop,wscale 6], length 0
    0x0000:  4500 003c d9fb 4000 4006 625a 51a4 2d8f  E..<..@.@.bZQ.-.
    0x0010:  51a4 2d8f dbe8 0017 0bb8 87f2 0000 0000  Q.-.............
    0x0020:  a002 8018 fe94 0000 0204 400c 0402 080a  ..........@.....
    0x0030:  0238 5e09 0000 0000 0103 0306            .8^.........
16:37:00.446581 IP 1.164.45.143.telnet > 1.164.45.143.56296: Flags [R.], seq 0, ack 196642803, win 0, length 0
    0x0000:  4500 0028 0000 4000 4006 3c6a 51a4 2d8f  E..(..@.@.<jQ.-.
    0x0010:  51a4 2d8f 0017 dbe8 0000 0000 0bb8 87f3  Q.-.............
    0x0020:  5014 0000 41bf 0000                      P...A...

你能做的最好的javascript是一个AJAX请求使用XMLHttpRequest。如果您的telnet服务器可以在到达好的命令之前处理一堆垃圾命令,那么您就没有问题了。AJAX请求可以连接到telnet服务器,并使用POST机制发送纯文本命令。telnet服务器将首先收到HTTP报头,它将在POST正文中到达您的有效telnet命令之前忽略"无效命令"错误。

var xtel = new XMLHttpRequest();
// replace 123.123.123.123 with the correct IP address
xtel.open("POST", "http://123.123.123.123:23", true);
// you won't be able to get a proper response from the telnet server 
xtel.onreadystatechange = function () {return true;} 
xtel.send("valid command 1" + "'n" + "valid command 2" + "'n");

这将类似于…

<> telnet 123.123.123.123 23
<> GET / HTTP/1.1
<> ACCEPT: */*
<> HOST: 123.123.123.123
<>
<> valid command 1
<> valid command 2

但是,上面是一个拼凑,您最好的选择是使用php套接字http://php.net/manual/en/function.stream-socket-client.php来执行连接并发送命令。