Code for implementing the phptelnet function

Source: Internet
Author: User
Code for implementing the phptelnet function

This section describes the php telnet function instances.

File: telnet. php

  1. Error_reporting (-1 );

  2. // Telnet function

  3. Class Telnet {
  4. Var $ sock = NULL;
  5. Function telnet ($ host, $ port ){
  6. $ This-> sock = fsockopen ($ host, $ port );
  7. Socket_set_timeout ($ this-> sock, 2, 0 );
  8. }

  9. Function close (){

  10. If ($ this-> sock) fclose ($ this-> sock );
  11. $ This-> sock = NULL;
  12. }
  13. Function write ($ buffer ){
  14. $ Buffer = str_replace (chr (255), chr (255). chr (255), $ buffer );
  15. Fwrite ($ this-> sock, $ buffer );
  16. }
  17. Function getc (){
  18. Return fgetc ($ this-> sock );
  19. }

  20. Function read_till ($ what ){

  21. $ Buf = '';
  22. While (1 ){
  23. $ IAC = chr (1, 255 );
  24. $ DONT = chr (1, 254 );
  25. $ DO = chr (1, 253 );
  26. $ WONT = chr (1, 252 );
  27. $ WILL = chr (251 );
  28. $ TheNULL = chr (0 );
  29. $ C = $ this-> getc ();
  30. If ($ c = false) return $ buf;
  31. If ($ c ==$ theNULL ){
  32. Continue;
  33. }
  34. If ($ c = "1 "){
  35. Continue;
  36. }

  37. If ($ c! = $ IAC ){

  38. $ Buf. = $ c;
  39. If ($ what = (substr ($ buf, strlen ($ buf)-strlen ($ what )))){
  40. Return $ buf;
  41. }
  42. Else {
  43. Continue;
  44. }
  45. } // Bbs.it-home.org

  46. $ C = $ this-> getc ();

  47. If ($ c ==$ IAC ){
  48. $ Buf. = $ c;
  49. }
  50. Else if ($ c ==$ DO) | ($ c ==$ DONT )){
  51. $ Opt = $ this-> getc ();
  52. // Echo "we wont". ord ($ opt). "\ n ";
  53. Fwrite ($ this-> sock, $ IAC. $ WONT. $ opt );
  54. }
  55. Elseif ($ c ==$ WILL) | ($ c ==$ WONT )){
  56. $ Opt = $ this-> getc ();
  57. // Echo "we dont". ord ($ opt). "\ n ";
  58. Fwrite ($ this-> sock, $ IAC. $ DONT. $ opt );
  59. }
  60. Else {
  61. // Echo "where are we? C = ". ord ($ c)." \ n ";
  62. }
  63. }
  64. }
  65. }

  66. /*

  67. Usage
  68. Telnet call
  69. $ Telnet = new telnet ("192.168.0.1", 23 );
  70. Echo $ telnet-> read_till ("login :");
  71. $ Telnet-> write ("kongxx \ r \ n ");
  72. Echo $ telnet-> read_till ("password :");
  73. $ Telnet-> write ("KONGXX \ r \ n ");
  74. Echo $ telnet-> read_till (":> ");
  75. $ Telnet-> write ("ls \ r \ n ");
  76. Echo $ telnet-> read_till (":> ");
  77. Echo $ telnet-> close ();
  78. */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.