-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsshclient.cpp
More file actions
214 lines (187 loc) · 5.82 KB
/
sshclient.cpp
File metadata and controls
214 lines (187 loc) · 5.82 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "sshclient.h"
#include <QTimer>
SSHClient::SSHClient(QObject *parent)
{
}
SSHClient::SSHClient(QString hostName,QString port,QString username,const QString password){
this->hostaddr = inet_addr(hostName.toStdString().data());
this->port = htons(atoi(port.toStdString().data()));
this->username = username;
this->password = password;
if (libssh2_init (0) != 0) {
fprintf (stderr, "libssh2 initialization failed\n");
}
}
SSHClient::SSHClient(QString hostName,QString port,QString username,QString publicKeyPath,QString privateKeyPath,QString passPhrase){
this->hostaddr = inet_addr(hostName.toStdString().data());
this->port = htons(atoi(port.toStdString().data()));
this->username = username;
this->publicKeyPath = publicKeyPath;
this->privateKeyPath=privateKeyPath;
this->passPhrase=passPhrase;
this->authType=2;
if (libssh2_init (0) != 0) {
fprintf (stderr, "libssh2 initialization failed\n");
}
}
bool SSHClient::connect(){
qDebug() << "开始连接";
sock = socket (AF_INET, SOCK_STREAM, 0);
sin.sin_family = AF_INET;
sin.sin_port = port;
sin.sin_addr.s_addr = hostaddr;
if (::connect(sock, (struct sockaddr *) &sin, sizeof(struct sockaddr_in)) != 0) {
fprintf (stderr, "Failed to established connection!\n");
emit errorMsg("Failed to established connection!");
return false;
}
emit connectSuccess();
qDebug() << "连接成功";
return true;
}
bool SSHClient::openSession(){
qDebug() << "打开会话";
/* Open a session */
session = libssh2_session_init();
if (libssh2_session_startup (session, sock) != 0) {
fprintf(stderr, "Failed Start the SSH session\n");
emit errorMsg("Failed Start the SSH session");
return false;
}
qDebug() << "打开会话成功";
libssh2_session_set_timeout(session,0);
return true;
}
bool SSHClient::userauth(){
qDebug() << "开始认证";
std::string un=username.toStdString();
if(authType==1){
std::string p=password.toStdString();
if (libssh2_userauth_password(session, un.data(), p.data()) != 0) {
fprintf(stderr, "Failed to authenticate\n");
emit errorMsg("Failed to authenticate");
close_connect();
return false;
}
}
if(authType==2){
std::string pkf=publicKeyPath.toStdString();
std::string pvkf=privateKeyPath.toStdString();
std::string pp=passPhrase.toStdString();
if (libssh2_userauth_publickey_fromfile(session, un.data(),pkf.data(),pvkf.data(),pp.data()) != 0) {
fprintf(stderr, "Failed to authenticate\n");
emit errorMsg("Failed to authenticate");
close_connect();
return false;
}
}
emit authSuccess();
qDebug() << "认证成功";
return true;
}
bool SSHClient::open_channel(){
/* Open a channel */
channel = libssh2_channel_open_session(session);
if ( channel == NULL ) {
fprintf(stderr, "Failed to open a new channel\n");
emit errorMsg("Failed to open a new channel");
stop();
return false;
}
if (libssh2_channel_request_pty( channel, "xterm") != 0) {
fprintf(stderr, "Failed to request a pty\n");
emit errorMsg("Failed to request a pty");
stop();
return false;
}
if(pty_rows!=0&&pty_cols!=0){
libssh2_channel_request_pty_size(channel, pty_cols, pty_rows);
}
/* Request a shell */
if (libssh2_channel_shell(channel) != 0) {
fprintf(stderr, "Failed to open a shell\n");
emit errorMsg("Failed to open a shell");
stop();
return false;
}
emit openChannelSuccess();
return true;
}
void SSHClient::setChannelRequestPtySize(int row,int column){
libssh2_channel_request_pty_size(channel, column, row);
}
void SSHClient::free_channel(){
if(channel!=NULL){
libssh2_channel_free(channel);
}
channel=NULL;
}
void SSHClient::close_session(){
if(session!=NULL){
libssh2_session_disconnect(session, "Session Shutdown, Thank you for playing");
libssh2_session_free(session);
}
}
void SSHClient::close_connect(){
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
}
void SSHClient::exec(std::string shell){
int size=strlen(shell.c_str());
libssh2_channel_write(channel, shell.c_str(), size);
// libssh2_channel_write(channel,shell.c_str(),1);
}
void SSHClient::run(){
bool result=this->connect();
if(result){
result=openSession();
}
if(result){
result=userauth();
}
if(result){
result=open_channel();
}
if(!result){
return;
}
if ((fds = static_cast<LIBSSH2_POLLFD *>(malloc(sizeof(LIBSSH2_POLLFD)))) == NULL){
return;
}
fds[0].type = LIBSSH2_POLLFD_CHANNEL;
fds[0].fd.channel = channel;
fds[0].events = LIBSSH2_POLLFD_POLLIN | LIBSSH2_POLLFD_POLLOUT;
int nfds = 1;
char* buf = new char[READ_BUF_SIZE];
while (true) {
if (libssh2_channel_eof(channel) == 1){
free (fds);
emit readChannelData("断开连接");
break;
}
if (libssh2_poll(fds, nfds, 10) >0) {
ssize_t length = libssh2_channel_read(channel, buf, READ_BUF_SIZE);
// qDebug() << "实际读取长度:" << length;
if(length<=0){
continue;
}
QByteArray buffer(buf,length);
QString data = QString::fromUtf8(buffer);
emit readChannelData(data);
// fprintf(stdout, "%c", buf);
// fflush(stdout);
}
}
}
void SSHClient::stop(){
QTimer::singleShot(100,this,[&](){
free_channel();
close_session();
this->terminate();
});
close_connect();
libssh2_exit();
}