cancel
Showing results for 
Search instead for 
Did you mean: 

Get IP Address

Former Member
0 Kudos

Hello All.

Can anyone tell me a way to retrieve the client side IP address from within ABAP? I have tried using TERMINAL_IP_GET but that requires SY-UNAME. Is there one that just looks at the machine and retrieves the IP?

Thanks for your help.

Jeff

Accepted Solutions (0)

Answers (6)

Answers (6)

Former Member
0 Kudos

I found a way to do it.

Basically, I found some classes that allows me to call a URL and fill a RESPONSE object and it populates the header fields.

In this example I am calling Yahoo.com but in the end I would call something a bit more stable...perhaps the BSP that initiates the whole report call.

I have listed the code for it below.

=================================================

data: client type ref to if_http_client,

ip_address type string.

****Create the Call

call method cl_http_client=>create_by_url

exporting

url = 'http://www.yahoo.com'

  • ssl_id = ' '

importing

client = client

exceptions

others = 1.

****Make the call

client->send( ).

****Receive the Response Object

call method client->receive

exceptions

http_communication_failure = 1

http_invalid_state = 2

http_processing_failed = 3

others = 4.

ip_address = client->response->get_header_field( name = '~remote_addr' ).

Former Member
0 Kudos

Hi everyone.

Thanks for your responses. I've taken the time to investigate all of your ideas but unfortunately, none of them work for what I am doing. This ABAP gets executed from a web template, with numerous users logged on with a generic ID. When the ABAP is executed SY-UNAME is blank and it looks like I need to be logged into the GUI for these options to work. So now I'm thinking I may have to approach the problem from another angle.

If anyone has any other ideas, please share...otherwise I'll close this thread and come up with another approach.

Thanks to you all again.

Jeff

Former Member
0 Kudos

Hi again,

1. TERMINAL_ID_GET

This is the FM u require.

(No user name required,

it gives IP and machine name also)

2. Just copy paste in new program.

REPORT abc.

DATA : term LIKE usr41-terminal.

*----


CALL FUNCTION 'TERMINAL_ID_GET'

IMPORTING

terminal = term.

WRITE 😕 term.

regards,

amit m.

athavanraja
Active Contributor
0 Kudos

you can use

CL_GUI_FRONTEND_SERVICES=>GET_IP_ADDRESS

Regards

Raja

former_member188685
Active Contributor
0 Kudos

Hi,

There is one simple way to get the Terminal IP.

REPORT  ZTEST_IPGET                             .

DATA: BEGIN OF USR_TABL OCCURS 10.
        INCLUDE STRUCTURE UINFO.
DATA: END OF USR_TABL.

DATA:TH_OPCODE(1)  TYPE X.
DATA: IPADDR(30).

CONSTANTS: OPCODE_LIST  LIKE TH_OPCODE VALUE 2.

CALL 'ThUsrInfo' ID 'OPCODE' FIELD OPCODE_LIST
ID 'TAB' FIELD USR_TABL-*SYS*.



LOOP AT USR_TABL.

  CALL FUNCTION 'GWY_IPADR2STRING'
    EXPORTING
      IPADR  = USR_TABL-HOSTADR
    IMPORTING
      STRING = IPADDR.

  WRITE:/ USR_TABL-BNAME ,USR_TABL-TERM, IPADDR.
ENDLOOP.

the above code give user,termical id, ip address.

regards

vijay

athavanraja
Active Contributor
0 Kudos

Hi Jeff,

do you want the current users IP address or all logged on users (to the system) and their IP addresses.

if its the former you can just use

CL_GUI_FRONTEND_SERVICES=>GET_IP_ADDRESS

else.

use FM THUSRINFO

Regards

Raja

former_member188685
Active Contributor
0 Kudos

Hi,

you can use the FM <b>RFC_HOST_TO_IP0</b> or <b>RFC_HOST_TO_IP</b>

pass the host name it will give the IP address.

regards

vijay

Former Member
0 Kudos

Hi jeff,

1. I have written one independent form

using two FMs

We pass the user name

and it gets the IP ADDRESS Of it.

1. TH_USER_LIST

2. GWY_IPADR2STRING

2. just copy paste in new program.

REPORT abc.

DATA : ip(20) TYPE c.

*----


PARAMETERS : user TYPE sy-uname.

PERFORM getip USING user CHANGING ip.

WRITE 😕 ip.

*----


  • INDEPENDENT FORM

*----


FORM getip USING user CHANGING ip..

DATA : list LIKE TABLE OF uinfo WITH HEADER LINE.

CALL FUNCTION 'TH_USER_LIST'

TABLES

list = list.

READ TABLE list WITH KEY bname = user.

CALL FUNCTION 'GWY_IPADR2STRING'

EXPORTING

ipadr = list-hostadr

IMPORTING

string = ip.

ENDFORM. "getip

regards,

amit m.

Former Member
0 Kudos

Well, the program has to know for whom you want the data. What other critera do you want to give it?

Rob

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

I had this stuffed away in my bag of tricks. Maybe you will find it useful.



report zrich_0001 .

data: ip_address(50) type c.

start-of-selection.

  perform get_ip_address using ip_address.

write:/ ip_address.

*---------------------------------------------------------------------*
*       FORM get_ip_address                                           *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
*  -->  IP_ADD                                                        *
*---------------------------------------------------------------------*
form get_ip_address using ipaddress.

  data:
          begin of ip_help_x,                " ip hexadecimal
            a type x,
            b type x,
            c type x,
            d type x,
          end of ip_help_x,
          begin of ip_help_p,                " ip decimal
            a(4) type n,
            b(4) type n,
            c(4) type n,
            d(4) type n,
          end of ip_help_p,
          begin of ip_address,               " ip character
            a(4),
            b(4),
            c(4),
            d(4),
          end of ip_address,

          ip_addr_x like msxxlist-hostadr,

          ip_pattern like usr41-terminal.

  call function 'TH_USER_INFO'
       importing
            hostaddr = ip_addr_x
       exceptions
            others   = 1.

* move structure to hex-structure (byte-wise)
  move ip_addr_x to ip_help_x-a.
  move ip_addr_x+1 to ip_help_x-b.
  move ip_addr_x+2 to ip_help_x-c.
  move ip_addr_x+3 to ip_help_x-d.
* move hex to decimal (byte-wise)
  move-corresponding ip_help_x to ip_help_p.
* move decimal to char (without zero)
  write ip_help_p-a to ip_address-a no-zero.
  if ip_address-a is initial.
    ip_address-a = '0'.
  endif.
  write ip_help_p-b to ip_address-b no-zero.
  if ip_address-b is initial.
    ip_address-b = '0'.
  endif.
  write ip_help_p-c to ip_address-c no-zero.
  if ip_address-c is initial.
    ip_address-c = '0'.
  endif.
  write ip_help_p-d to ip_address-d no-zero.
  if ip_address-d is initial.
    ip_address-d = '0'.
  endif.
* add dots, condense and compare with terminal list.
  concatenate ip_address-a '.'
              ip_address-b '.'
              ip_address-c '.'
              ip_address-d into ip_pattern.
  condense ip_pattern no-gaps.

  ipaddress = ip_pattern.

endform.

Please make sure to award points for helpful answers and mark your post as solved when solved completely. Thanks and welcome to SDN!

Regards,

Rich Heilman