Simplest Telnet Client Ever Made in C/C++
- libtelnet: Install it easily using the folllwing command:
$ sudo apt install libtelnet-dev
- Make sure to download the repository. Use
git clone https://github.com/ravijo/simple-telnet-client.git
or download zip as per your convenience. - Create build directory and go inside it using
mkdir build && cd build
- Invoke
cmake
ascmake ..
- Build files using
make
In order to see the request and response from the server, please set DEBUG_MODE
as shown below:
simple-telnet-client/include/simple_telnet_client/telnet_client.hpp
Lines 17 to 18 in 84a0565
- Please set the IP address and port number of the telnet server as shown below:
simple-telnet-client/src/main.cpp
Line 14 in 84a0565
- Do not forget to compile after changes
- 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 ]
- If the
libtelnet
is installed on a custom locaton, we need to provide its path inCMakeLists.txt
. - 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
- 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} )
The package can be installed using Docker for testing purposes. Please use the following commands to build and run the docker:
- 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
- Run Docker
ravi@dell:~/simple-telnet-client$ docker run -it client root@0ebe27c82992:/simple-telnet-client/build#
- 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
- 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