-
Notifications
You must be signed in to change notification settings - Fork 0
/
telnet_client.hpp
70 lines (52 loc) · 1.39 KB
/
telnet_client.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2022 Ravi Joshi
// Date: 7 Aug 2022
#ifndef SIMPLE_TELNET_CLIENT__TELNET_CLIENT_HPP_
#define SIMPLE_TELNET_CLIENT__TELNET_CLIENT_HPP_
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <libtelnet.h>
#include <iostream>
#include <string>
#include <vector>
// flag to print requests and responses in the console for debugging purposes
#define DEBUG_MODE true
namespace simple_telnet_client
{
class TelnetClient
{
public:
TelnetClient(
const std::string & serverIP,
const std::string & serverPort,
const size_t timeOut = 1,
const size_t bufferSize = 2048);
// execute a command on the server
void execute(const std::string & command);
// receive the response from the server
std::string response();
~TelnetClient();
private:
int mSockFd;
std::string mReceivedMsg;
fd_set mReadFd;
fd_set mWriteFd;
struct timeval mTimeout;
telnet_t * mTelnet;
std::vector<char> mBuffer;
static void trampoline(
telnet_t * telnet,
telnet_event_t * event,
void * user_data);
int makeConnection(
const std::string & serverIP,
const std::string & serverPort);
void telnetEvent(telnet_event_t * event);
int sendAll(
const char * mBuffer,
size_t size);
void configureTimeout(const size_t seconds);
void configureReadWriteFd();
};
} // namespace simple_telnet_client
#endif // SIMPLE_TELNET_CLIENT__TELNET_CLIENT_HPP_