Casio
Casio

Reputation: 13

How to get client ip adress using libssh

I am using libssh to make my own ssh server (some kind of honeypot). I would like to save the ip address of connected client into my logfile. How to get this IP address? Programming in c++. Thanks in advance guys!

Upvotes: 1

Views: 807

Answers (2)

vojtamicka
vojtamicka

Reputation: 21

Here is little function I'm using.

string getClientIp(ssh_session session) {

    struct sockaddr_storage tmp;
    struct sockaddr_in *sock;
    unsigned int len = 100;
    char ip[100] = "\0";

    getpeername(ssh_get_fd(session), (struct sockaddr*)&tmp, &len);
    sock = (struct sockaddr_in *)&tmp;
    inet_ntop(AF_INET, &sock->sin_addr, ip, len);

    string ip_str = ip;

    return ip_str;
}

It is based on function "get_client_ip" fromhttps://github.com/PeteMo/sshpot/blob/master/auth.c where is complete SSH honeypot implementation.

Upvotes: 2

asn
asn

Reputation: 838

s = ssh_get_fd(session);
getpeername(s, ...);

Upvotes: -1

Related Questions