Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7994699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:12:01+00:00 2026-06-04T14:12:01+00:00

I have written a small program in C to get IP from remote user

  • 0

I have written a small program in C to get IP from remote user who log into SSH session

Code :

int main (){
    char * getIP ;
    getIP = getenv ("SSH_CLIENT");
    printf ("%s", getIP);
    printf("\n");
    return 0;
}

It works great and on output I have

shell# ./a.out

192.168.1.33 39840 22

But I would like to print only the IP address and not the rest of the string. I can’t figure out how cut the string to print only the first part (255.255.255.255)

I search whole day solution but without success :/

Thanks for any help : )

UPDATE !

I discovered that instead SSH_CLIENT you can use REMOTEHOST : )
But Larsks solution is very helpful and worth remembering

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:12:02+00:00Added an answer on June 4, 2026 at 2:12 pm

    You don’t even need strtok() to do this, since all you care about is the first string of characters before a space. Use strchr() to locate the first space, set it to NUL, and you’re done:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main (){
        char *getIP, *mark;
        int i;
        getIP = getenv ("SSH_CLIENT");
    
        // get the location of the first space character
        mark = strchr(getIP, ' ');
    
        // set it to 0 (ascii NUL)
        *mark = 0;
        printf ("%s\n", getIP);
        return 0;
    }
    

    You could actually collapse it a little bit by doing this:

    *(strchr(getIP, ' ')) = 0;
    

    Instead of:

    mark = strchr(getIP, ' ');
    *mark = 0;
    

    But that’s not quite as clear, and it would be harder to do proper error checking.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have visual studio 2008. I have a written a small C program. I
I have written a small program , to understand how futures work in c++0x.
I have written a small program and compiled it under Solaris/Linux platform to measure
I have written a small program using Borland's C++ builder, and along the way,
Let say I have written a small program that reads file_A and file_B as
I have written a very small C# program, that uses a very small SQL
I have written a small program displaying sounds and images on the screen when
I have written a small program: void showrecord() { char *a[]={ O_BILLABLE_ACCOUNT,O_CUSTOMER_TYPE_INDICATOR, O_A_PARTY_MSISDN_ID,O_A_PARTY_EQUIPMENT_NUMBER, O_A_PARTY_IMSI,O_A_PARTY_LOCATION_INFO_CELL_ID,
I have written a small C program that embeds Python. I'm setting it up
As a Christmas gift I have written a small program in Java to calculate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.