Autore Topic: Ottenere il proprio IP con Gambas  (Letto 1821 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.262
  • Ne mors quidem nos iunget
    • Mostra profilo
« Ultima modifica: 04 Ottobre 2020, 03:45:49 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline Top Fuel

  • Gran Maestro dei Gamberi
  • *****
  • Post: 960
    • Mostra profilo
Re: Ottenere il proprio IP con Gambas
« Risposta #1 il: 27 Novembre 2013, 00:06:48 »
Strano che non ti funziona.
Con me va bene:

massimo@Topfuel:~$ ip -4 addr show eth0 | grep inet
    inet 192.168.1.100/24 brd 192.168.1.255 scope global eth0
massimo@Topfuel:~$
Dear youtube administrators, your search bar is broken. When I type the letter "J" it appears justin bieber when it should appear Jimi Hendrix. Fix this, please.

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.262
  • Ne mors quidem nos iunget
    • Mostra profilo
Re: Ottenere il proprio IP con Gambas
« Risposta #2 il: 25 Agosto 2014, 12:13:18 »
Strano che non ti funziona.
Con me va bene:
Sul mio portatile noto che funziona.
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.262
  • Ne mors quidem nos iunget
    • Mostra profilo
Re:Ottenere il proprio IP con Gambas
« Risposta #3 il: 23 Novembre 2015, 16:41:48 »
...forse con questo codice può andare:
Codice: [Seleziona]
Private Const AF_INET As Integer = 2
Private Const SOCK_DGRAM As Integer = 2
Private Const SIOCGIFADDR As Integer = 35093

Library "libc:6"

' int socket (int __domain, int __type, int __protocol)
' Create a new socket of type TYPE in domain DOMAIN, using protocol PROTOCOL.
Private Extern socket(domain As Integer, type As Integer, protocol As Integer) As Integer

' int ioctl (int __fd, unsigned long int __request, ...)
' Perform the I/O control operation specified by REQUEST on FD.
Private Extern ioctl(__fd As Integer, request As Long, argum As Pointer) As Integer

' int Close (int __fd)
' Close the file descriptor FD.
Private Extern close_C(__fd As Integer) As Integer Exec "close"


Library "/tmp/libso"

' int Inetntoa(struct ifreq ifr)
Private Extern Inetntoa(po As Pointer, i As Integer) As String

Public Sub Main()

  Dim fd As Integer
  Dim bb As Byte[] = [101, 116, 104, 48]   ' eth0
  Dim ifreq As Pointer
  Dim s As String
  Dim st As Stream
 
   fd = socket(AF_INET, SOCK_DGRAM, 0)
 
' Crea un apposita libreria dinamica condivisa .so esterna per la gestione sicura di alcune Strutture e Funzioni:
   Creaso()
 
   ifreq = Alloc(48)
 
   st = Memory ifreq For Write
' Unisce l'indirizzo IP a "eth0":
   bb.Write(st, 0, bb.Count)
' Ottiene un indirizzo IP IPv4:
   Seek #st, 16
   Write #st, AF_INET As Integer
   st.Close

   ioctl(fd, SIOCGIFADDR, ifreq)
 
   s = Inetntoa(ifreq, fd)
 
   Print s

   close_C(fd)
   Free(ifreq)

End


Private Procedure Creaso()
 
' Salva il codice sorgente in C dell'apposita libreria esterna .so:
  File.Save("/tmp/libso.c", "#include <arpa/inet.h>\n" &
  "#include <net/if.h>\n\n" &
  "void Scrive_sa_family(struct ifreq * p, int afnet) {\n" &
  "   p->ifr_addr.sa_family = AF_INET;\n}" &
  "\n\n" &
   "char * Inetntoa(struct ifreq * p, int fd) {\n" &
  "   return inet_ntoa(((struct sockaddr_in *) &p->ifr_addr)->sin_addr);" &
  "\n}")
 
' Genera l'apposita libreria esterna .so:
  Shell "gcc -o /tmp/libso.so /tmp/libso.c -shared -fPIC" Wait
 
End





« Ultima modifica: 24 Novembre 2015, 11:54:19 da vuott »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »