Insert IP address into MySQL int field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert IP address into MySQL int field
# 1  
Old 10-26-2008
Insert IP address into MySQL int field

Greets all,

I'm using Perl trying to insert an IP address from a log file into an INT field in a MYSQL table. So it needs to be converted into an int first. I thought the pack() function could do this for me, but I'm not sure I get the right thing. I also need to be able to extract it correctly later. Am I doing it right? If so, what code do I need to extract it?

Code:
($ip,$url) = <some regular expression>
$ipaddr=pack("I",split(/\./,$ip);
$dbh->do("insert into entry values ( $url, $ipaddr );");

# 2  
Old 10-26-2008
From perldoc -f pack:
Quote:
N An unsigned long in "network" (big-endian) order.
So you could do
Code:
use Socket; 
$ipaddr_decimal = unpack ("N", inet_aton ("192.168.1.1"));
$ipaddr = inet_ntoa(pack ("N", $ipaddr_decimal));

# 3  
Old 10-26-2008
Thanks. This works well. But is there a 'perl' way of doing the inet_aton conversion? That seems a little 'heavy'.
# 4  
Old 10-27-2008
IMHO inet_aton() is pretty lightweight, I think there is no other embedded way of doing this. Maybe you could create a sub to transform an IPv4 address into network byte order but that's just the point of inet_aton().
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Need help on Insert data to mySQL database

Hi guys, I would like to seek help on inserting data whenever the switch is on or off to my sensor mySQL database in phpMyAdmin from my control.php. I'm using Raspberry PI as my hardware and follow a few tutorials to create my own Web Control Interface, it works perfectly without insert method.... (1 Reply)
Discussion started by: aoiregion
1 Replies

2. Shell Programming and Scripting

Need help on Insert data to phpMyAdmin mySQL database from Shell Script

Sorry to disturb you, I would like to seek help on inserting data whenever the switch is on or off to my phpMyAdmin mySQL database from my Shell Script. I'm using Raspberry PI as my hardware and I have follow this LINK: instructables.com/id/Web-Control-of-Raspberry-Pi-GPIO/?ALLSTEPS to create my... (4 Replies)
Discussion started by: aoiregion
4 Replies

3. Programming

MySQL select user with the same IP address

Dear community, I woul like to make a query to output all the users having the same IP address. The table is somethig like: name logged_ip ==== ========= user1 127.0.0.1 user2 127.0.0.2 user3 127.0.0.3 user4 127.0.0.1 user5 127.0.0.2 user6 127.0.0.5I used this query... (4 Replies)
Discussion started by: Lord Spectre
4 Replies

4. Programming

how to compare value of mysql field

#include <stdio.h> #include <fcntl.h> #include <signal.h> #include <unistd.h> #include <mysql.h> #include <string.h> #include <stdlib.h> #include <sys/time.h> #include <mysql.h> int main(int argc, char **argv) { MYSQL *conn; MYSQL_RES *result; MYSQL_ROW row; ... (0 Replies)
Discussion started by: slackman
0 Replies

5. UNIX and Linux Applications

Online insert MySQL rows by perl-script

Hello, Met a problem when I tried to insert rows to MySQL database from an old book that fits my learning level (MySQL and Perl for the Web, by Paul DuBois, 2001). First, under mysql console I created a database: webdb and the table: todo. Then I draft the perl-cgi script to have online page.... (0 Replies)
Discussion started by: yifangt
0 Replies

6. Programming

[SQL] Insert content file to mysql

dear all, i want to insert string in file to mysql i just want how to do that cause i am poor in sql languages ... so this file like this DATA.txt doni|student|westjava|123412|lombok| iwan|student|westjava|1234412|utankayu| rio|student|westjava|12342|cempedak| so i want insert DATA.txt to... (2 Replies)
Discussion started by: zvtral
2 Replies

7. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

8. Shell Programming and Scripting

insert EOD if value in Field 1 change

How to create simple awk program data such as below: input: 1 23 1 34 1 12 2 10 2 11 2 12 3 11 3 12 3 13 expected output: 1 23 1 34 1 12 EOD 2 10 2 11 2 12 EOD (2 Replies)
Discussion started by: jmaskar
2 Replies

9. Shell Programming and Scripting

How to insert data into MYSql database from a text file

Hi, Need to get help from you guys about this issue. I need to insert data into MySql database from a text file which is located in other server. The text file is something look like below: Date | SubscriberNo | Call Duration 20/7/07 | 123456788 | 20 20/7/07 | 123412344 | 30 The... (4 Replies)
Discussion started by: shirleyeow
4 Replies

10. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies
Login or Register to Ask a Question