AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Getting Server IP (https://forums.alliedmods.net/showthread.php?t=94911)

Empowers 06-17-2009 03:34

Getting Server IP
 
I have 2 server ip-s. LAN ip(inside ip) and INTERNET ip(outside ip). I wanna to get INTERNET IP.
But when I do this:
PHP Code:

get_user_ip(0,ip,15

it gives me LAN Ip.

Any suggsetions?

xPaw 06-17-2009 03:37

Re: Getting Server IP
 
Just because you are running on lan, what you wanted to get more...

Empowers 06-17-2009 03:39

Re: Getting Server IP
 
I'm running internet game. But players from LAN can connect to my server throught other ip.

P.s I have sv_lan 0

Understand me?

hleV 06-17-2009 06:49

Re: Getting Server IP
 
You can try this.
Code:
// No port new szIP[16]; get_cvar_string("ip", szIP, 15);   // With port new szIP[22], szPort[6]; get_cvar_string("ip", szIP, 15); get_cvar_string("port", szPort, 5); format(szIP, 21, "%s:%s", szIP, szPort);

Sylwester 06-17-2009 07:55

Re: Getting Server IP
 
This is good if you have static ip, but if you have dynamic ip like me, then you can use something like this:
PHP Code:

#include <amxmodx>
#include <sockets>

new g_ip[32]
new 
g_socket

public plugin_init(){
    
register_plugin("Get server ip""1.0""Sylwester")
    
register_clcmd("say ip""get_server_ip")
}


public 
get_server_ip(id){
    new 
error
    
if(g_socket>0){
        
client_print(idprint_chat"Error: socket is in use")
        return 
PLUGIN_HANDLED
    
}

    
g_socket socket_open("www.whatsmyip.org"80SOCKET_TCPerror)
    if(
g_socket 0){
        
socket_send(g_socket"GET / HTTP/1.1^nHost: www.whatsmyip.org^n^n"64)
        
client_print(idprint_chat"Sending request...")
        
set_task(0.1"recv"id)
    }else{
        
client_print(idprint_chat"Socket error")
    }

    return 
PLUGIN_HANDLED
}


public 
recv(id){
    if(!
socket_change(g_socket100))
        
set_task(0.1"recv"id)
    else{
        new 
data[512], ipos
        socket_recv
(g_socketdata511)
        
pos containi(data"Your IP is ")
        if(
pos>-1){
            
pos += 11
            
while(data[pos+i]=='.' || (data[pos+i]>='0' && data[pos+i]<='9')){
                
g_ip[i] = data[pos+i
                
i++
            }
            
client_print(idprint_chat"Server ip: %s"g_ip)
        }
        
socket_close(g_socket)
        
g_socket 0
    
}


My plugin connects to one of the "what is my ip" websites and gets your ip. It will work as long as the website is online, the way it displays ip is not changed and you are not banned because of sending requests too often :P
If you have your own website you could use it for this plugin. Just make script that displays only ip on whole page (like www.whatismyip.org but that one is down for me now so I used other) and modify this plugin.

Mlk27 06-17-2009 08:24

Re: Getting Server IP
 
Query from checkip.dyndns.com much better

xtinct 04-21-2010 07:01

Re: Getting Server IP
 
Quote:

Originally Posted by Mlk27
Query from checkip.dyndns.com much better

There are then such lots of options, however We need to choose the efiicient and faster one.

Jelle 04-21-2010 15:37

Re: Getting Server IP
 
Quote:

Originally Posted by Sylwester (Post 850799)
This is good if you have static ip, but if you have dynamic ip like me, then you can use something like this:
PHP Code:

#include <amxmodx>
#include <sockets>

new g_ip[32]
new 
g_socket

public plugin_init(){
    
register_plugin("Get server ip""1.0""Sylwester")
    
register_clcmd("say ip""get_server_ip")
}


public 
get_server_ip(id){
    new 
error
    
if(g_socket>0){
        
client_print(idprint_chat"Error: socket is in use")
        return 
PLUGIN_HANDLED
    
}

    
g_socket socket_open("www.whatsmyip.org"80SOCKET_TCPerror)
    if(
g_socket 0){
        
socket_send(g_socket"GET / HTTP/1.1^nHost: www.whatsmyip.org^n^n"64)
        
client_print(idprint_chat"Sending request...")
        
set_task(0.1"recv"id)
    }else{
        
client_print(idprint_chat"Socket error")
    }

    return 
PLUGIN_HANDLED
}


public 
recv(id){
    if(!
socket_change(g_socket100))
        
set_task(0.1"recv"id)
    else{
        new 
data[512], ipos
        socket_recv
(g_socketdata511)
        
pos containi(data"Your IP is ")
        if(
pos>-1){
            
pos += 11
            
while(data[pos+i]=='.' || (data[pos+i]>='0' && data[pos+i]<='9')){
                
g_ip[i] = data[pos+i
                
i++
            }
            
client_print(idprint_chat"Server ip: %s"g_ip)
        }
        
socket_close(g_socket)
        
g_socket 0
    
}


My plugin connects to one of the "what is my ip" websites and gets your ip. It will work as long as the website is online, the way it displays ip is not changed and you are not banned because of sending requests too often :P
If you have your own website you could use it for this plugin. Just make script that displays only ip on whole page (like www.whatismyip.org but that one is down for me now so I used other) and modify this plugin.

When writing ip in my server with this running the server crashes and gives me 4 errors from WC3FT.

Sylwester 04-22-2010 11:15

Re: Getting Server IP
 
I'm not surprised that it's not working (content received from that website obviously changed), though I don't know why does your server crash when using this plugin. If you got some errors then why didn't you post them?

You can try this version. It works for me now:

PHP Code:

#include <amxmodx>
#include <sockets>

new g_ip[16]
new 
bool:g_got_ip
new g_socket

public plugin_init(){
    
register_plugin("Get server ip""1.1""Sylwester")
    
update_ip()
    
    
register_clcmd("say ip""get_server_ip")
}


public 
update_ip(){
    static 
error
    
if(g_socket>0){
        
log_amx("Error occurred while trying to retrieve server ip (socket is in use).")
        return
    }
    
    
//send http request to selected host
    
g_socket socket_open("checkip.dyndns.com"80SOCKET_TCPerror)
    if(
g_socket 0){
        
socket_send(g_socket"GET / HTTP/1.1^nHost: checkip.dyndns.com^n^n"64)
        
set_task(0.1"recv")
    }else{
        
log_amx("Error occurred while trying to retrieve server ip (%d)."error)
    }
}


public 
recv(id){
    if(!
socket_change(g_socket1))
        
set_task(0.1"recv"id)
    else{
        new 
data[256], ijdpos
        socket_recv
(g_socketdata255)

        
//analyze data retrieved from website
        
pos containi(data"<body>Current IP Address: ")
        if(
pos>-1){
            
pos += 26
            
while('0' <= data[pos+i] <= '9'){
                
g_ip[i] = data[pos+i]
                
i++
                if(
data[pos+i]=='.'){
                    
g_ip[i] = data[pos+i]
                    
j=++i
                    d
++
                }
            }
            if(
j==|| d!=3){
                
g_got_ip false
            
}else{
                
g_got_ip true
            
}
        }else{
            
g_got_ip false
        
}
        
socket_close(g_socket)
        
g_socket 0
    
}
}


public 
get_server_ip(id){
    if(!
g_got_ip){
        
client_print(idprint_chat"Server was unable to retrieve its external ip")
        return
    }
    
client_print(idprint_chat"Server external ip: %s"g_ip)




Jelle 04-22-2010 19:52

Re: Getting Server IP
 
Sorry for not posting the errors, but I closed the server again before copying them. What happened was I got 4 errors from WC3FT when typing ip. After that I got "Program is not responding" from windows. I try the other thing you posted tomorrow.


All times are GMT -4. The time now is 15:38.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.