Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions sshclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ bool SSHClient::connect(){
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();
Expand All @@ -49,6 +50,7 @@ bool SSHClient::openSession(){
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() << "打开会话成功";
Expand All @@ -62,6 +64,7 @@ bool SSHClient::userauth(){
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(sock);
close_connect();
return false;
Expand All @@ -74,6 +77,7 @@ bool SSHClient::userauth(){
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(sock);
close_connect();
return false;
Expand All @@ -90,12 +94,14 @@ bool SSHClient::open_channel(){

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;
}
Expand All @@ -106,6 +112,7 @@ bool SSHClient::open_channel(){
/* 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;
}
Expand All @@ -118,13 +125,18 @@ void SSHClient::setChannelRequestPtySize(int row,int column){
libssh2_channel_request_pty_size(channel, column, row);
}
void SSHClient::free_channel(){
libssh2_channel_free(channel);
if(channel!=NULL){
libssh2_channel_free(channel);
}
channel=NULL;
}

void SSHClient::close_session(){
libssh2_session_disconnect(session, "Session Shutdown, Thank you for playing");
libssh2_session_free(session);
if(session!=NULL){
libssh2_session_disconnect(session, "Session Shutdown, Thank you for playing");
libssh2_session_free(session);
}

}

void SSHClient::close_connect(){
Expand Down
6 changes: 3 additions & 3 deletions sshclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ class SSHClient : public QThread

struct sockaddr_in sin;

LIBSSH2_SESSION *session;
LIBSSH2_SESSION *session=NULL;

LIBSSH2_CHANNEL *channel;
LIBSSH2_CHANNEL *channel=NULL;

LIBSSH2_POLLFD *fds = NULL;

Expand All @@ -100,7 +100,7 @@ class SSHClient : public QThread


signals:
// void readChannelData(char* data);
void errorMsg(QString errMsg);
void readChannelData(char data);
void connectSuccess();
void authSuccess();
Expand Down
6 changes: 6 additions & 0 deletions webconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ void WebConsole::ssh2connect(const QString& jsMsg){
sshClient->pty_rows=rows;
sshClient->start();
connect(sshClient,SIGNAL(connectSuccess()),this,SLOT(connectSuccess()));

connect(sshClient,&SSHClient::errorMsg,this,[=](QString errMsg){
QMessageBox::warning(this,"错误提示",errMsg);
close();
});

connect(sshClient,&SSHClient::authSuccess,this,[=](){

});
Expand Down