Thread: systray app to get IP Address

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    170

    systray app to get IP Address

    I have a need to get the IP address of my PC and put it into the clipboard.

    More specifically I want to put the following into the clipboard:

    "export DISPLAY=xxx.xxx.xxx.xxx:0.0"

    where xxx.xxx.xxx.xxx is the IP of my PC.

    I use ssh to connect to unix boxes and need an easy way to set the display variable back to my PC.

    So I came up with this to run in the systray, i would click on the systray icon and it loads my buffer and then i paste it into my ssh window.

    I have the systray icon part figured out. I need help with getting my IP (and not 127.0.0.1) and pasting it and the rest of the text into the clipboard.

    Any advice on obtaining the IP address would be helpfull. I think I can figure out the clipboard part on my own, but tips are appreciated of coarse.

    Thanks in advance.

    --Bonkey
    Best Regards,

    Bonkey

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    Write a simple function or 3 to your program using the winblows socket layer to go out and grab a site like www.whatismyip.com and store the result to a string. Then its a simple matter of breaking the string up, and CHOP CHOP CHOP! Voila, IP. I do alot of download/chopchopchop data-mineing programs, and when i get some time (friday probablly) if you cant figure out how to get the chopping or downloading working right, drop me a line and we can swap sources or something.



    edit: this question is so common, i should just write and post a nice program to display it =P

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    I was thinking alont those same lines. But I was opening a pipe to a system call "ipconfig.exe" and chopping that up.

    Not working yet as the command:

    Code:
    spawnl( P_WAIT, "C:\\WINNT\\SYSTEM32\\ipconfig.exe","C:\\WINNT\\SYSTEM32\\ipconfig.exe",NULL) )
    does not return the text to my pipe. Instead it opens a window, runs the command and then closes. My attemp to read the pipe just hangs.
    Best Regards,

    Bonkey

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Since your talking to a unix box, why bother with a windows program at all? You can get your IP address on the unix board incredibly simply, so just write a bash script to export the display for you, containing the ip address you have connected to that box from.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    /me looks for the ShellScripting forum

    /me doesn't find it =P

    Yes, shellscripting can do alot of wonderful things but everybody here would probablly benefit alot more from a C++ program that can grab a webpage and chop it up.


    edit: EVEN BETTER IDEA! LETS WRITE A LuserIPd! It can idle on a few servers around the world on port... umm.... 412 or something, and when it recieves a connection it simply echos back the IP of the initiator of the connection! Im starting a thread for this in the tech forum.
    Last edited by Geolingo; 09-17-2003 at 07:04 PM.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    On WInXP or later

    GetAdaptersAddresses()
    from Iphlpapi.h and lib

    or

    getaddrinfo()

    older functions are

    gethostbyname() ect
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Why should we have to send a packet all the way around the world just to get our own IP address? Clearly novacain has an easier solution.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Originally posted by _Elixia_
    Since your talking to a unix box, why bother with a windows program at all? You can get your IP address on the unix board incredibly simply, so just write a bash script to export the display for you, containing the ip address you have connected to that box from.
    Not when using ssh. SSH does not update the wtmp file with the connection address. isuing a who -m will not give the IP address. You can see your IP Address in a netstat -a but you will see ALL of the ssh connections and will have no way to distinguish yours from any other.
    Best Regards,

    Bonkey

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Originally posted by novacain
    On WInXP or later

    GetAdaptersAddresses()
    from Iphlpapi.h and lib

    or

    getaddrinfo()

    older functions are

    gethostbyname() ect

    I am trying this, but I get an unresolved external. I am using this new vc++ studio .net and I can't find where to tell it to link in the library.

    Here is my code:
    Code:
    ULONG bufsize=255;
    	PULONG pOutBufLen=&bufsize;
    	_IP_ADAPTER_ADDRESSES *ipaddr=new _IP_ADAPTER_ADDRESSES[255];
    	PIP_ADAPTER_ADDRESSES pAdapterAddresses=ipaddr;
    		
    
    	GetAdaptersAddresses(AF_INET,0,NULL,pAdapterAddresses,pOutBufLen);
    
    	delete [] ipaddr;
    Anyone know where to tell it what to link at compile time? I know in the older versions there was a screen for that.
    Best Regards,

    Bonkey

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    I found the place to add the library. it is well hidden. right clicking on the text of my project name in the solution explorer was the only way I was able to find it.
    Best Regards,

    Bonkey

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    I tried GetAdaptersAddresses() with no luck. Not XP. I don't want to have to load the framework on all the PCs.

    I'm trying gethostbyname but I just get nothing in the hostent returned.

    Code:
    hostent *m_hostinfo;
    m_hostinfo=gethostbyname("localhost");
    When I debug it I get invalig info in the m_hostinfo variable. Am I not using it correctly?
    Best Regards,

    Bonkey

  12. #12
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    I have it ALMOST working. The results in the debuger shows

    Code:
    	sa_family	2	unsigned short
    -	sa_data	0x0031276a ""	char [14]
    	[0]	7 '␇'	char
    	[1]	0	char
    	[2]	-84 '¬'	char
    	[3]	19 '␓'	char
    	[4]	8 '␈'	char
    	[5]	112 'p'	char
    the 19 8 and 112 are correct but the first octet is missing. Do I need to do something special to parse this result?

    BTW here is the code for getting your IP address as I have found it to be:

    Code:
    WORD wVersionRequested;
    	WSADATA wsaData;
    	int err;
     
    	wVersionRequested = MAKEWORD( 2, 2 );
     
    	err = WSAStartup( wVersionRequested, &wsaData );
    	if ( err != 0 ) {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        return;
    	}
    
    	char host[255];
    	char *hostname=&host[0];
    	memset(hostname,NULL,255);
    	gethostname(hostname,255);
    
    	struct addrinfo *res = NULL;
        int             rc;
    
    	char port='7';
    
    	rc = getaddrinfo(hostname,&port,NULL,&res);
        if (rc != 0)
           fprintf(stderr, "Invalid address %s, getaddrinfo failed: %d\n", hostname, rc);
    Best Regards,

    Bonkey

  13. #13
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You can use gethostbyname(NULL).

    To print out a hostent structure:

    Code:
    	printf("Resolving host name %s\n", hostname); 
    
    	hostentry = gethostbyname( hostname ); 
    	if ( !hostentry ) { 
    		printf("Unknown %s, error: %u!\n", hostname, WSAGetLastError()); 
    		exit(-1); 
    	} 
    	printf("Resolving successful\n"); 
    
    	printf("Official name = %s\n", hostentry->h_name); 
    
    	printf("Aliases = "); 
    	// print all the alias names of the host 
    	for ( alias = hostentry->h_aliases; *alias; ++alias ) { 
    		printf("%s, ", *alias); 
    	} 
    	printf("\n"); 
    
    	printf("Address type = %u\n", hostentry->h_addrtype); 
    	printf("Address length = %u\n", hostentry->h_length );
    
    	// Check if the returned address is an IP address: length MUST always be 4 byte
    	if ( hostentry->h_addrtype != AF_INET || hostentry->h_length != 4 ) {
    		printf("No ip addresses for this host!\n");
    		exit(-1);
    	} 
    
    	printf("Ip address(es) = "); 
    	for ( p_ipaddr = (ULONG**) hostentry->h_addr_list; *p_ipaddr; ++p_ipaddr ) {
    		struct in_addr inaddr;
    		inaddr.S_un.S_addr = **p_ipaddr;
    		printf("%s ", inet_ntoa( inaddr ) ); 
    	}

  14. #14
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    char port='7';
    rc = getaddrinfo(hostname,&port,NULL,&res);

    servname
    [in] Pointer to a null-terminated string containing either a service name or port number.

    You are not passing a proper port string to getaddrinfo.

  15. #15
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Maybe then that you have an old version of ssh. When I ssh into my shell account here, this is what I get:

    bash-2.05$ who -m
    tribal.metalab.unc.edu!jamesp pts/13 Sep 18 18:30 (du-018-0066.claranet.co.uk)
    bash-2.05$

    Originally posted by bonkey
    Not when using ssh. SSH does not update the wtmp file with the connection address. isuing a who -m will not give the IP address. You can see your IP Address in a netstat -a but you will see ALL of the ssh connections and will have no way to distinguish yours from any other.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  2. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  3. how get ip address on 2 network cards
    By akesayyo in forum C Programming
    Replies: 8
    Last Post: 07-04-2005, 10:15 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. MSN Vital Information
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-22-2001, 08:55 PM