0

I have an SSH server through which I can access remote computers. After connecting to the SSH server, I create several tunnels to the remote computer with different ports to access different services and then just maintain the connection. We used to use Putty to connect to the server and port forwarding, but I need to automate this. The server is forbidden to run the shell ( -N parameter in putty).

I wrote a function using libssh, but for some reason I manage to connect to the SSH server and the tunnel is also created correctly, but when I want to test on the local port the communication via putty, I get the message "network error: connection refused" after a while

I am attaching the code and would be grateful for any help.

void CSSHClass::OpenSSHTunnel()
{
ssh_session session;
ssh_channel channel;

ssh_init();

session = ssh_new();
if (session == NULL)
{
    return;
}
int port = 22;

ssh_options_set(session, SSH_OPTIONS_HOST, "tran.some.com");
ssh_options_set(session, SSH_OPTIONS_PORT, &port);
ssh_options_set(session, SSH_OPTIONS_USER, "user");

int connectStatus = ssh_connect(session);
if (connectStatus != SSH_OK)
{
    // Chyba při připojování k serveru
    ssh_free(session);
    return;
}

int authStatus = ssh_userauth_password(session, NULL, "password");
if (authStatus != SSH_AUTH_SUCCESS)
{
    ssh_disconnect(session);
    ssh_free(session);
    return;
}

channel = ssh_channel_new(session);
if (channel == NULL)
{
    ssh_disconnect(session);
    ssh_free(session);
    return;
}

int tunnelStatus = ssh_channel_open_forward(channel, "some.any", 22, "127.0.0.1", 1389);
if (tunnelStatus != SSH_OK)
{
    ssh_channel_free(channel);
    ssh_disconnect(session);
    ssh_free(session);
    return;
}

ssh_channel_free(channel);
ssh_disconnect(session);
ssh_free(session);

ssh_finalize();
}

0 Answers0