Skip to content

ravijo/simple-telnet-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simple-telnet-client

Simplest Telnet Client Ever Made in C/C++

Dependencies

  1. libtelnet: Install it easily using the folllwing command:
    $ sudo apt install libtelnet-dev

Installation

  1. Make sure to download the repository. Use git clone https://github.com/ravijo/simple-telnet-client.git or download zip as per your convenience.
  2. Create build directory and go inside it using mkdir build && cd build
  3. Invoke cmake as cmake ..
  4. Build files using make

Debug

In order to see the request and response from the server, please set DEBUG_MODE as shown below:

// flag to print requests and responses in the console for debugging purposes
#define DEBUG_MODE true

Run

  1. Please set the IP address and port number of the telnet server as shown below:
    clientPtr = std::make_unique<simple_telnet_client::TelnetClient>("192.168.10.20", "23");
  2. Do not forget to compile after changes
  3. Invoke the client using ./client as shown below:
    $ ./client
    response: [Welcome
    ]
    response: [
    ]
    request: [INIT
    ]
    response: [OK
    ]
    request: [READ PT3
    ]
    response: [OK
    ]
    request: [READ PT4
    ]
    response: [OK
    ]
    request: [REM PT3
    ]
    response: [OK
    ]

Using libtelnet Installed on Custom Location

  1. If the libtelnet is installed on a custom locaton, we need to provide its path in CMakeLists.txt.
  2. First, let's find out where is our libtelnet
    $ whereis libtelnet
    libtelnet: /usr/lib/x86_64-linux-gnu/libtelnet.so /usr/lib/x86_64-linux-gnu/libtelnet.a /usr/include/libtelnet.h
  3. Now, using the above information, we need to modify CMakeLists.txt as shown below:
    set(TELNET_INCLUDE_DIR
      /usr/include
    )
    
    set(TELNET_LIBRARY
      /usr/lib/x86_64-linux-gnu/libtelnet.so
    )
    
    include_directories(include
      ${TELNET_INCLUDE_DIR}
    )
    
    add_executable(client
      src/telnet_client.cpp
      src/main.cpp
    )
    
    target_link_libraries(client
      ${TELNET_LIBRARY}
    )

Using Docker

The package can be installed using Docker for testing purposes. Please use the following commands to build and run the docker:

  1. Build Docker
    ravi@dell:~/simple-telnet-client$ docker build -t client .
    Sending build context to Docker daemon  92.67kB
    Step 1/8 : FROM ubuntu:20.04
     ---> 3bc6e9f30f51
    Step 2/8 : ENV TZ=Asia/Tokyo
     ---> f8e22625db16
    .
    .
    .
    Step 8/8 : WORKDIR /simple-telnet-client/build
     ---> efb6c90e291e
    Successfully built efb6c90e291e
    Successfully tagged client:latest
  2. Run Docker
    ravi@dell:~/simple-telnet-client$ docker run -it client
    root@0ebe27c82992:/simple-telnet-client/build# 
  3. CMake Porject
    root@0ebe27c82992:/simple-telnet-client/build# cmake ..
    -- The C compiler identification is GNU 9.4.0
    .
    .
    .
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /simple-telnet-client/build
  4. Make Project
    root@0ebe27c82992:/simple-telnet-client/build# make
    Scanning dependencies of target client
    [ 33%] Building CXX object CMakeFiles/client.dir/src/telnet_client.cpp.o
    [ 66%] Building CXX object CMakeFiles/client.dir/src/main.cpp.o
    [100%] Linking CXX executable client
    [100%] Built target client

References

  1. libtelnet