It is quite simple to build a very basic client/server model using nc. On one console, start nc listening on a specific port for a connection. For example:
$ nc -l 1234
nc is now listening on port 1234 for a connection. On a second console (or a second machine), connect to the machine and port being listened on:
$ nc 127.0.0.1 1234
There should now be a connection between the ports. Anything typed at the second console will be concatenated to the first, and vice-versa. After the connection has been set up, nc does not really care which side is being used as a ‘server’ and which side is being used as a ‘client’. The connection may be terminated using an EOF (‘^D’).
The example in the previous section can be expanded to build a basic data transfer model. Any information input into one end of the connection will be output to the other end, and input and output can be easily captured in order to emulate file transfer.
Start by using nc to listen on a specific port, with output captured into a file:
# nc -l 1235 > filename.out
Using a second machine, connect to the listening nc process, feeding it the file which is to be transferred:
[other_server]# time nc 172.29.0.99 1235 < some_big_seven_gb.sql
Compare these results with the scp command :
The only way to improve the file transfer performance is to compress the file before sending using zip or bzip2
Let's assume you want to transfer a 66 MB SQL text file (revert_15_april.sql) from local server to remote server in less than 2 minutes.
(sending server 10.10.10.1)
tar jcf - revert_15_april.sql | nc -l 1234
nc 10.10.10.1 1234 | tar jxf -
It is also possible to send all the files from current folder just by replacing the file name with *
tar jcf - * | nc -l 1234
### Or the same can also be done using...
## start listening : ##
nc -l 8888 | tar jxf -
## cd to the directory that you want to copy and push all to server 172: ##
tar jcf - . | nc 220.127.116.11 8888
You can also send directory using NetCat to another server as a single file...
tar c test | nc -l 7878
nc 10.10.10.1 7878 > backup.tar
# to extract the file, use the standard tar command.
tar xvf backup.tar
Copy data from one server to another.
mysqldump db_name tbl_name | bzip2 -c | nc -l 1234
nc 10.10.10.14 1234 | bzip2 -cd | mysql -uroot -proot@123 db_name
Labels: linux tips
select distinct foo from table where bar=N can return no rows found where select foo
from table where bar=N returns rows.
There is something seriously gone wrong in the following explain plan:
mysql> explain select distinct c from distinct_fail where d=4\G
*************************** 1. row ***************************
Extra: Using where; Using index for group-by
1 row in set (0.00 sec)
The index "foo" looks so harmless but when used in a wrong way, can create havoc:
CREATE TABLE `distinct_fail` (
`a` int NOT NULL,
`b` int NOT NULL,
`c` int NOT NULL,
`d` int NOT NULL,
KEY `foo` (`c`,`d`,`a`,`b`),
KEY `bar` (`c`,`a`,`b`,`d`)
Labels: mysql tips
You can write PHP code within bash script and take advantage of both worlds!
echo This is the Bash section of the code.
/usr/local/bin/php -q << EOF
\$myVar = "PHP";
print("This is the \$myVar section of $amount the code.\n The value of the variable amount is $amount \n");
The output will look something like this...
# sh testphp.sh
This is the Bash section of the code.
This is the PHP section of 5 the code.
The value of the variable amount is 5
Labels: unix case study
You can create a calendar of 365 days starting from a given day. The days in each month will be grouped together, separated by comma and enclosed in apostrophe.
sh /root/calendar.sh '2002-01-01'
mysql -e"drop table if exists test.mycalendar;"
mysql -e"create table test.mycalendar (id int not null auto_increment, dateval date, primary key (id));"
for (( i = 0 ; i < 730 ; i++ ))
mysql -e"insert into test.mycalendar (dateval) select '$1' + interval $i day;"
mysql -e"select group_concat(concat('to_days(', '\'',dateval,'\')') order by dateval) as '' from test.mycalendar group by extract(year_month from dateval);" | sort
Labels: unix case study