SCP related tips and issue resolve

Today, I had trouble to send a file to remote target board with the command following:


jiafei427@CKUBU:~/tmp$ scp /home/jiafei427/tmp/slrclub_cert.crt root@10.177.247.35:/var/tmp/usr/ck
sh: scp: cannot execute - No such file or directory
lost connection

Damn, had no clue what that meant, and googled like a forever.

used “-vvv” option, but nothing that I can recognize.

 

Finally found solution by myself, and actually that was a bit too easy.

Solution is simply put a “scp” binary into the target board.

scpย will use the binary both in client and server side. ๐Ÿ˜ฆ

(didn’t know that..)

 

Here put more examples about scp:

What is Secure Copy?

scpย allows files to be copied to, from, or between different hosts. It usesย sshย for data transfer and provides the same authentication and same level of security asย ssh.

Examples

Copy the file “foobar.txt” from a remote host to the local host

$ scp your_username@remotehost.edu:foobar.txt /some/local/directory

Copy the file “foobar.txt” from the local host to a remote host

$ scp foobar.txt your_username@remotehost.edu:/some/remote/directory

Copy the directory “foo” from the local host to a remote host’s directory “bar”

$ scp -r foo your_username@remotehost.edu:/some/remote/directory/bar

Copy the file “foobar.txt” from remote host “rh1.edu” to remote host “rh2.edu”

$ scp your_username@rh1.edu:/some/remote/directory/foobar.txt \
your_username@rh2.edu:/some/remote/directory/

Copying the files “foo.txt” and “bar.txt” from the local host to your home directory on the remote host

$ scp foo.txt bar.txt your_username@remotehost.edu:~

Copy the file “foobar.txt” from the local host to a remote host using port 2264

$ scp -P 2264 foobar.txt your_username@remotehost.edu:/some/remote/directory

Copy multiple files from the remote host to your current directory on the local host

$ scp your_username@remotehost.edu:/some/remote/directory/\{a,b,c\} .
$ scp your_username@remotehost.edu:~/\{foo.txt,bar.txt\} .

scpย Performance

By defaultย scpย uses the Triple-DES cipher to encrypt the data being sent. Using the Blowfish cipher has been shown to increase speed. This can be done by using optionย -c blowfishย in the command line.

$ scp -c blowfish some_file your_username@remotehost.edu:~

It is often suggested that theย -Cย option for compression should also be used to increase speed. The effect of compression, however, will only significantly increase speed if your connection is very slow. Otherwise it may just be adding extra burden to the CPU. An example of using blowfish and compression:

$ scp -c blowfish -C local_file your_username@remotehost.edu:~

 

 

Ref.

http://www.hypexr.org/linux_scp_help.php

 

 

 

 

 

 

 

 

 

Advertisements

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ดํ•ดํ•˜๊ธฐ

์œ ๋‹‰์Šค๋‚˜ ๋ฆฌ๋ˆ…์Šค ํ™˜๊ฒฝ์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํฌ๊ฒŒ ์„ธ๊ฐ€์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.

– ์ •์  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
– ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
– ๋™์  ์ ์žฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

์ •์  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” .a๋กœ ๋๋‚˜๋Š” ํŒŒ์ผ๋กœ ๋นŒ๋“œ ์‹œ ์‹คํ–‰ ํŒŒ์ผ์— ํฌํ•จ๋˜๊ฒŒ ๋œ๋‹ค. ์ •์  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ externํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์„ ์–ธ๋œ ํ—ค๋” ํŒŒ์ผ๊ณผ ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” .so๋กœ ๋๋‚˜๋Š” ํŒŒ์ผ๋กœ ๋นŒ๋“œ ์‹œ ์‹คํ–‰ ํŒŒ์ผ์— ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ์—ฌ๋Ÿฌ ํ”„๋กœ๊ทธ๋žจ์ด ๊ณต์œ ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๋งŒ๋“ค ๊ฒฝ์šฐ ๋””์Šคํฌ ๊ณต๊ฐ„์„ ์•„๋‚„์ˆ˜ ์žˆ๊ณ  ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋ฒ„๊ทธ๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋งŒ ์žฌ๋ฐฐํฌํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ์„ธ์Šค์— ์˜ํ•ด ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œ๋˜๋ฉด, ํ•ด๋‹น ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋˜๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํ–‰๋˜์—ˆ์„ ๋•Œ ์ด์ „ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋กœ๋”ฉํ•ด ๋†“์€ ๋‚ด์šฉ์„ ๊ณต์œ ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ๋˜ํ•œ ์ ˆ์•ฝ๋œ๋‹ค.
๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ externํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์„ ์–ธ๋œ ํ—ค๋” ํŒŒ์ผ๊ณผ ํ•ด๋‹น ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์‹คํ–‰ ํŒŒ์ผ์ด ์‹ค์ œ๋กœ ์‹œ์Šคํ…œ์—์„œ ์‹คํ–‰๋˜๋Š” ์‹œ์ ์— ํ”„๋กœ๊ทธ๋žจ ๋ง์ปค ๋กœ๋”๊ฐ€ /lib, /usr/lib, ๊ทธ๋ฆฌ๊ณ  LD_LIBRARY_PATH๋“ฑ์—์„œ ํ•ด๋‹น ํŒŒ์ผ์˜ ์กด์žฌ์œ ๋ฌด๋ฅผ ํ™•์ธํ•˜๊ณ  ์ด๋ฅผ ์‚ฌ์šฉ๋˜๊ฒŒ ๋œ๋‹ค. ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์•ž์„œ ๋งํ•œ๋ฐ”์™€ ๊ฐ™์ด ๋งํฌ๋˜๋Š” ์‹œ์ (ํ•ด๋‹น ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋นŒ๋“œํ•˜๋Š” ์‹œ์ )์— ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ—ค๋”์™€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ( .so)ํŒŒ์ผ์ด ํ•„์š”ํ•˜๋‹ค. ์™œ ์‹คํ–‰ํŒŒ์ผ์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ์ด ํ•„์š”ํ•œ ๊ฒƒ์ธ๊นŒ? ์ด๋Š” ๋นŒ๋“œ๋˜๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๋  ๋•Œ ์ฐพ์•„์•ผ ํ•  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ด๋ฆ„์ด ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ ์•ˆ์— ์ ํ˜€์žˆ๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ ์ด๋ฅผ soname์ด๋ผ๊ณ  ํ•˜๊ณ  ์ด soname์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํŒŒ์ผ๋ช…ํ•˜๊ณ  ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์„œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ปดํŒŒ์ผํ•  ๋•Œ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด โ€“fPIC (Position Independent Code)์˜ต์…˜์ด ํ•„์š”ํ•˜๋‹ค.

gcc โ€“fPIC โ€“c MyLibrary.c
gcc โ€“shared โ€“Wl,-soname libMyLibrary.so.1 โ€“o libMyLibrary.so.1.2.3 MyLibrary.o

๋™์  ์ ์žฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์‹คํ–‰ ์ค‘์— ๋™์ ์œผ๋กœ ๋กœ๋”ฉ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฉฐ, ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์‹œ๊ฐ€ ์•„๋‹Œ ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ํฌํ•จ๋œ ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋˜๋Š” ์‹œ์ ์— ๋ถˆ๋ ค์™€ ์‚ฌ์šฉ๋˜๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๋™์  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์€ ์ฒซ ์‹คํ–‰์ด ์ •์  ํ˜น์€ ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋ณด๋‹ค ๋น ๋ฅด๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค. ๋˜ํ•œ ๋™์  ์ ์žฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์‹คํ–‰ ์‹œ ๋™์ ์œผ๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ง์ ‘ ์ •๋ณด๋ฅผ ์–ป์–ด์™€ ์‹คํ–‰ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋™์  ์ ์žฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋นŒ๋“œํ•˜๋Š” ์‹œ์ ์—์„œ๋Š” ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ํ—ค๋” ํŒŒ์ผ์ด๋‚˜ ์‹ฌ์ง€์–ด ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž์ฒด๋„ ํ•„์š” ์—†๋‹ค. ๋ฆฌ๋ˆ…์Šค์—์„œ๋Š” ์ผ๋ฐ˜ ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋™์ ์œผ๋กœ ์ ์žฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ๋™์  ์ ์žฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๊ฐ™๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌ๋ถ„๋  ๋ฟ์ด๋‹ค. ์ฆ‰ soํŒŒ์ผ์„ ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ๋™์  ์ ์žฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ ๋™์  ์ ์žฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์—์„œ dlopen, dlsym๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ์‹ฌ๋ณผ์„ ๋ถˆ๋Ÿฌ์˜ค๋„๋ก ์ฒ˜๋ฆฌํ•˜๋ฉด ๋œ๋‹ค.

๋Œ€๋ถ€๋ถ„์˜ unix/linux ์‹œ์Šคํ…œ์—์„œ LD_LIBRARY_PATH๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๋ชจ๋“  ์‹œ์Šคํ…œ์—์„œ ๊ทธ๋Ÿฐ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋ฆฌ๋ˆ…์Šค์—์„œ๋Š” LD_LIBRARY_PATH ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, /etc/ld.so.conf ํŒŒ์ผ๊ณผ ldconfig์„ ์‚ฌ์šฉํ•˜์—ฌ /etc/ld.so.cache ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋” ๊ถŒ์žฅ๋œ๋‹ค.

๋˜๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” rpath๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. rpath๋Š” ์‹คํ–‰ ํŒŒ์ผ์„ compileํ• ๋•Œ ์ง€์ •ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

gcc -Wall -o myexefile -Wl,rpath,. main.o -L. -lMyLibrary

์œ„ ์˜ˆ์ œ์—์„œ๋Š” rpath๋ฅผ ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ [.]๋กœ ์„ค์ •ํ•˜์˜€์œผ๋ฉฐ ์ด๋Š” ์‹คํ–‰ํ•  ๋•Œ MyLibrary๋ฅผ ์ฐพ๋Š” ์œ„์น˜์— ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•œ ๊ฒƒ์ด๋‹ค.ย  -L์˜ต์…˜์€ ๋งํฌ์‹œ์— MyLibrary๋ฅผ ์ฐพ๋Š” ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๋Š” ์˜ต์…˜์œผ๋กœ ์—ญ์‹œ [.] ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ง€์ •ํ•˜์˜€๋‹ค. ๊ตณ์ด -l ์˜ต์…˜์œผ๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ง€์ •ํ•  ํ•„์š” ์—†์ด ๊ฒฝ๋กœ๋ฅผ ํฌํ•จํ•œ ํŒŒ์ผ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ๋„ ๋œ๋‹ค.

gcc -Wall -o myexefile -Wl,rpath,. main.o ./libMyLibrary.so.1.2.3

 

๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋ณดํ†ต ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…๋ช…๋œ๋‹ค.

libMyLibrary.so.1.2.3

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์šฉ๋„์— ๋”ฐ๋ผ ๋ช‡๊ฐ€์ง€ ์„œ๋กœ ๋‹ค๋ฅธ ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ ํ˜ผ๋ˆ๋˜๊ธฐ ์‰ฌ์šฐ๋‹ˆ ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

1. ๋ง์ปค ์ด๋ฆ„.(linker name)

๋ง์ปค ์ด๋ฆ„์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ ๋ช…์—์„œ ๋ฒ„์ ผ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž๋ฅผ ๋บ€ so๊นŒ์ง€์˜ ์ด๋ฆ„์ด๋‹ค. ์ฆ‰ ์œ„์—์„œ๋Š”libMyLibrary.so๊นŒ์ง€๊ฐ€ linker name์ด ๋œ๋‹ค. ๋ง์ปค ์ด๋ฆ„์€ ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋นŒ๋“œ ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์ด๋ฆ„์ด๋‹ค. ๋” ์ •ํ™•ํžˆ ๋งํ•˜๋ฉด, ํ”„๋กœ๊ทธ๋žจ์ด ๋นŒ๋“œ๋  ๋•Œ ํ•ด๋‹น ํ”„๋กœ๊ทธ๋žจ์ด ์‚ฌ์šฉํ•˜๋Š” ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ๋ง์ปค ์ด๋ฆ„์„ ๊ฐ€์ง„ ํŒŒ์ผ์ด ๋นŒ๋“œํ•˜๋Š” ์‹œ์Šคํ…œ์— ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด ์–ด๋–ค ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด libMyLibrary.so.1.2.3์„ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํŒŒ์ผ ํ•  ๊ฒฝ์šฐ ๋นŒ๋“œ ์˜ต์…˜์— -lMyLibrary๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋•Œ ๊ธฐ๋ณธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ฒฝ๋กœ๋‚˜ ํ˜น์€ โ€“L ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ง€์ •ํ•œ ๋””๋ ‰ํ† ๋ฆฌ์— libMyLibrary.so ํŒŒ์ผ, ์ฆ‰ ๋ง์ปค ์ด๋ฆ„์œผ๋กœ ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. (๋ณดํ†ต์€ libMyLibrary.so.1.2.3์— ๋Œ€ํ•œ ์‹ฌ๋ณผ๋ง ๋งํฌ๋กœ ์ด ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ์ด์œ ๋Š”ย ํ˜ธ์ŠคํŠธย ๊ฐœ๋ฐœย ํ™˜๊ฒฝ์— ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์—ฌ๋Ÿฌ ๋ฒ„์ ผ์ด ์กด์žฌํ•  ๊ฒฝ์šฐ ์ด๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. ) ํฌ๋กœ์Šค ์ปดํŒŒ์ผ์„ ํ•˜๋Š” ํ™˜๊ฒฝ์—์„œ๋Š” ๋นŒ๋“œ์‹œ์—๋งŒ ๋ง์ปค ์ด๋ฆ„์„ ๊ฐ€์ง„ ํŒŒ์ผ์ด ํ•„์š”ํ•˜๋ฉฐ, ํƒ€๊ฒŸ์—์„œ ์‹คํ–‰ ์‹œ์—๋Š” ์ด ํŒŒ์ผ์ด ํ•„์š”์น˜ ์•Š๋‹ค. ์ฆ‰ ๊ฐœ๋ฐœํ•˜๋Š” ํ˜ธ์ŠคํŠธ ์ปดํ“จํ„ฐ์—๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ ์ด๋ฆ„์ด ๋ง์ปค ์ด๋ฆ„์œผ๋กœ ๋˜์–ด ์žˆ๊ฑฐ๋‚˜ ์ด๋ฅผ ์‹ฌ๋ณผ๋ง ๋งํฌ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ/๋ง์ปค๋Š” ๋ง์ปค ์ด๋ฆ„์„ ๊ฐ€์ง„ ํŒŒ์ผ์—์„œ ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ soname์„ ์ฝ์–ด์˜ค๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋นŒ๋“œ์‹œ ์‹คํ–‰ ํŒŒ์ผ์— ํฌํ•จ๋˜์ง€๋Š” ์•Š์ง€๋งŒ soname์„ ์ฝ์–ด์™€์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋นŒ๋“œํ•˜๋Š” ํ˜ธ์ŠคํŠธ ์‹œ์Šคํ…œ์— ์—†์œผ๋ฉด ๋นŒ๋“œ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค.

2. soname

์ดย ์ด๋ฆ„์€ย ๋กœ๋”๊ฐ€ย ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผย ์‹คํ–‰ํ• ย ๋•Œย ์ฐพ๋Š”ย ์ด๋ฆ„์œผ๋กœย ํƒ€๊ฒŸ์—๋Š”ย soname์„ย ๊ฐ€์ง„ย ํŒŒ์ผ์ดย ๋ฐ˜๋“œ์‹œย ์žˆ์–ด์•ผย ํ•œ๋‹ค. ๋•Œ๋•Œ๋กœ ์ด ํŒŒ์ผ์€ ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋กœ ๋‹ค๋ฅธ ํŒŒ์ผ๋กœ ๋งํฌ๋˜์–ด ์žˆ๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ soname์€ย so๋’ค์—ย ์ˆซ์ž ํ•˜๋‚˜๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋งŒ๋“ ๋‹ค.ย ย ์˜ˆ๋ฅผ ๋“ค์–ดย  libMyLibrary.so.1 ๊ฐ€ ์œ„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ soname์œผ๋กœ ์ ํ•ฉํ•˜๋‹ค.ย ์•„๋ž˜์™€ย ๊ฐ™์€ย ๋ช…๋ น์œผ๋กœย  soname์„ ์ง€์ •ํ• ย ์ˆ˜ย ์žˆ๋‹ค.

gcc โ€“shared โ€“Wl,-soname libMyLibrary.so.1 โ€“o libMyLibrary.so.1.2.3 MyLibrary.o

so๋’ค์— ์˜ค๋Š” ์ฒซ๋ฒˆ์งธ ์ˆซ์ž๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ Major version number๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ ํ˜ธํ™˜์„ฑ์ด ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒฝ์šฐ์— ์ฆ๊ฐ€์‹œํ‚จ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋นŒ๋“œํ•˜๊ณ  ๋˜ ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋นŒ๋“œํ•˜๋ฉด ํ•ด๋‹น ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์‹œ ๋กœ๋”์— ์˜ํ•ด์„œ soname์„ ๊ฐ€์ง„ ํŒŒ์ผ์„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ฒฝ๋กœ์—์„œ ์ฐพ๊ฒŒ ๋œ๋‹ค. ์ฆ‰ ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋  ๋•Œ์—๋Š” soname์„ ๊ฐ€์ง„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ์ด ํ•„์š”ํ•˜๋‹ค. ํฌ๋กœ์Šค ์ปดํŒŒ์ผ์„ ํ•˜๋Š” ํ™˜๊ฒฝ์—์„œ๋Š” ๋นŒ๋“œ ์‹œ์—๋Š” soname์„ ๊ฐ€์ง„ ํŒŒ์ผ์ด ์•„๋‹Œ ๋ง์ปค์ด๋ฆ„์„ ๊ฐ€์ง€๋Š” ํŒŒ์ผ(์ผ๋ฐ˜์ ์œผ๋กœ xxx.so๋กœ ๋๋‚˜๋Š” ํŒŒ์ผ)์ด ํ•„์š”ํ•˜๊ณ  ์‹คํ–‰์‹œ์—๋Š” soname์„ ๊ฐ€์ง€๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(ํ˜น์€ ์ด์— ๋Œ€ํ•œ ์‹ฌ๋ณผ๋ฆญ ๋งํฌ )๊ฐ€ ํ•„์š”ํ•œ ๊ฒƒ์ด๋‹ค.

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ full name์—์„œ ๋‘๋ฒˆ์งธ ์ˆซ์ž๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ minor version number๋กœ ํ˜ธํ™˜์„ฑ์—๋Š” ๋ณ€๊ฒฝ์ด ์—†์ง€๋งŒ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋“ฑ์ด ์ถ”๊ฐ€๋˜๋Š” ๊ฒฝ์šฐ์— ๋ณ€๊ฒฝ๋˜๋ฉฐ, ์„ธ๋ฒˆ์งธ ์ž๋ฆฌ๋Š” release ๋ฒ„์ „์œผ๋กœ ๋ฒ„๊ทธ ์ˆ˜์ • ๋“ฑ์œผ๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ƒˆ๋กœ ๋ฆด๋ฆฌ์ฆˆ๋˜๋Š” ๊ฒฝ์šฐ์— ๋ณ€๊ฒฝ๋œ๋‹ค.

๋”ฐ๋ผ์„œ ํฌ๋กœ์Šค ์ปดํŒŒ์ผ ํ™˜๊ฒฝ์—์„œ, ํ˜ธ์ŠคํŠธ์—์„œ ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ libMyLibrary.so.1.2.3 ๋ฅผ ๋งŒ๋“ค์–ด ํƒ€๊ฒŸ ์‹œ์Šคํ…œ์— ๋„ฃ์„ ๊ฒฝ์šฐ ํƒ€์ผ“ ์‹œ์Šคํ…œ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด soft link file์„ ๋งŒ๋“ค์–ด soname์„ ๊ฐ€์ง„ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

ln โ€“s libMyLibrary.so.1.2.3 libMyLibrary.so.1
or
mv libMyLibrary.so.1.2.3 libMyLibrary.so.1

๋˜ํ•œ ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ๋Š” ๋ชจ๋“  ๊ฐœ๋ฐœ์ž๋Š” ์ž์‹ ์˜ ๋นŒ๋“œ ํ™˜๊ฒฝ์— ์•„๋ž˜์™€ ๊ฐ™์ด ๊ณต์œ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ง์ปค ์ด๋ฆ„์„ ๊ฐ€์ง€๋„๋ก ์„ค์ •ํ•˜์—ฌ์•ผ ํ•œ๋‹ค. (์ฆ‰ .so.1 ํŒŒ์ผ์€ ํ˜ธ์ŠคํŠธ(๋นŒ๋“œ)์‹œ์Šคํ…œ์—์„œ๋Š” ํ•„์š” ์—†๋‹ค)

ln โ€“s libMyLibrary.so.1.2.3 libMyLibrary.so
or
mv libMyLibrary.so.1.2.3 libMyLibrary.so

ํŠน์ • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ soname์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๋ช…๋ น์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

objdump -p libMyLibrary.so.1.2.3 | grep SONAME

๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€…
http://wiki.kldp.org/wiki.php/DocbookSgml/Program-Library-HOWTO#DL-LIBRARIES

 

 

 

 

 

Ref.:

http://dooeui.blogspot.kr/search?updated-max=2009-06-07T08:26:00%2B09:00&max-results=10&start=30&by-date=false

 

 

 

 

How to extract kernel configuration from android kernel or boot.img and build related kernel for the device

To build a new kernel for specified device, you need to make a configuration:

For example, all of configuration fiiles: KCONFIG will be placed at related folders.

When you type the command:

make def_config

or

make menuconfig

to configure the kernel.

Afterall, all the configuration will be written to the one file “.config”ย at <kernel_source_dir>

 

So if you wanna build a specified kernel for specified device, the simpliest way to get the kernel configuration is to extract it from the original kernel.

Usually, you will not be able to download the kernel image itself.

but it’s easy to get the boot.img which is consisted with kernel and ramdisk images.

 

After you get the boot.img

Decompress it:

$ unmkbootimg -i boot.img
kernel written to ‘kernel‘ (6682776 bytes)
ramdisk written to ‘ramdisk.cpio.gz’ (913311 bytes)

To rebuild this boot image, you can use the command:
mkbootimg –base 0 –pagesize 2048 –kernel_offset 0x80208000 –ramdisk_offset 0x82200000 –second_offset 0x81100000 –tags_offset 0x80200100 –cmdline ‘console=ttyHSL0,115200,n8 androidboot.hardware=flo user_debug=31 msm_rtb.filter=0x3F ehci-hcd.park=3 vmalloc=340M’ –kernel kernel –ramdisk ramdisk.cpio.gz -o boot.img

You will have kernel and ramdisk in your current folder.

 

Then you need a script namedย extract-ikconfig

you can download it from following link:

https://github.com/torvalds/linux/blob/master/scripts/extract-ikconfig

$ chmod +x extract-ikconfig

$ extract-ikconfig kernel > kernel_config

 

then you can putย the kernel_config to the <kernel_source_dir> renaming it with “.config” and compile the kernel for the device.

 

For detailed steps of compiling the kernel, remaking the boot.img and flash to the device, you can follow my previous articles.

 

^___________________^

 

 

 

 

 

find specified target in linux with command

you can filter out messages to stderr. I prefer to redirect them to stdout like this.

#find / -name artย  2>&1 | grep -v “Permission denied”

Explanation:

In short, all regular output goes to standard output (stdout). All error messages to standard error (stderr).

grep usually finds/prints the specified string, the -v inverts this, so it finds/prints every string that doesn’t contain “Permission denied”. All of your output from the find command, including error messages usually sent to stderr (file descriptor 2) go now to stdout(file descriptor 1) and then get filtered by the grep command.

#find -type f ./ | xargs grep shit

“-type f ” will also forbid showing the searching target is directory.

 

if you want grep something, following will be pretty useful,

For BSD or GNU grep you can use -B num to set how many lines before the match and -A numfor the number of lines after the match.

grep -B 3 -A 2 foo README.txt

If you want the same number of lines before and after you can use -C num.

grep -C 3 foo README.txt

This will show 3 lines before and 3 lines after.

 

E.g.:

find / | xargs grep aoa 2>&1 | grep -v “Permission denied”

Port forwarding on Linux

SSH:

this is called GatewayPorts in SSH. An excerpt from ssh_config(5):

GatewayPorts
        Specifies whether remote hosts are allowed to connect to local
        forwarded ports.  By default, ssh(1) binds local port forwardings
        to the loopback address.  This prevents other remote hosts from
        connecting to forwarded ports.  GatewayPorts can be used to specโ€
        ify that ssh should bind local port forwardings to the wildcard
        address, thus allowing remote hosts to connect to forwarded
        ports.  The argument must be โ€œyesโ€ or โ€œnoโ€.  The default is โ€œnoโ€.

And you can use localhost instead of M in the forwarding, as you’re forwarding to the same machine as you’re SSH-ing to — if I understand your question correctly.

So, the command will become this:

ssh -g -L 8001:localhost:8000 -f -N user@remote-server.com
OR
ssh -g -L 2222:localhost:8888 -N -o GatewayPorts=yes hostname-of-M

(/usr/bin/ssh -g -vvvvv -L 0.0.0.0:29906:localhost:1337 localhost -N )

and will look like this in netstat -nltp:

tcp        0      0    0.0.0.0:2222   0.0.0.0:*  LISTEN  5113/ssh

Now anyone accessing this machine at port 2222 TCP will actually talk to localhost:8888 as seen in machine M. Note that this is not the same as plain forwarding to port 8888 of M.

OR

he command for forwarding port 80 from your local machine (localhost) to the remote host on port 8000 is:

ssh -R 8000:localhost:80 oli@remote-machine

This requires an additional tweak on the SSH server, add the lines to /etc/ssh/sshd_config:

Match User oli
   GatewayPorts yes

Next, reload the configuration by server executing sudo reload ssh.

The setting GatewayPorts yes causes SSH to bind port 8000 on the wildcard address, so it becomes available to the public address of remote-machine (remote-machine:8000).

If you need to have the option for not binding everything on the wildcard address, change GatewayPorts yes to GatewayPorts clientspecified. Because ssh binds to the loopback address by default, you need to specify an empty bind_address for binding the wildcard address:

ssh -R :8000:localhost:80 oli@remote-machine

The : before 8000 is mandatory if GatewayPorts is set to clientspecified and you want to allow public access to remote-machine:8000.

Relevant manual excerpts:

ssh(1)

-R [bind_address:]port:host:hostport
Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side. This works by allocating a socket to listen to port on the remote side, and whenever a connection is made to this port, the connection is forwarded over the secure channel, and a connection is made to host port hostport from the local machine. By default, the listening socket on the server will be bound to the loopback interface only. This may be overridden by specifying a bind_address. An empty bind_address, or the address โ€˜*โ€™, indicates that the remote socket should listen on all interfaces. Specifying a remote bind_address will only succeed if the server’s GatewayPorts option is enabled (see sshd_config(5)).

sshd_config(5)

GatewayPorts
Specifies whether remote hosts are allowed to connect to ports forwarded for the client. GatewayPorts can be used to specify that sshd should allow remote port forwardings to bind to non-loopback addresses, thus allowing other hosts to connect. The argument may be ‘no’ to force remote port forwardings to be available to the local host only, ‘yes’ to force remote port forwardings to bind to the wildcard address, or ‘clientspecified’ to allow the client to select the address to which the forwarding is bound. The default is ‘no’.

See also:

 

Change_IPTABLE:

There is another way. You may set up port forwarding from S:2222 to W:8888 with iptables. Single command:

iptables -t nat -A PREROUTING -p tcp --dport 2222 \
         -j DNAT --to-destination 1.2.3.4:8888

where 1.2.3.4 is M’s IP address. It is called NAT (Network Address Translation).

 

Another Sample:

iptables -t nat -I PREROUTING 1 -p 6 –dport 9222 -j DNATย  –to 10.0.1.1:1337
iptables -t nat -I POSTROUTING 1 -p 6 -d 10.0.1.1 –dport 1337 -j SNAT –to-source 10.0.1.2

 

e.g.:

I would like do some NAT in iptables. So that, all the packets coming to 192.168.12.87 and port 80 will be forwarded to 192.168.12.77 port 80.

 

#!/bin/sh

echo 1 > /proc/sys/net/ipv4/ip_forward

iptables -F
iptables -t nat -F
iptables -X

iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.12.77:80
iptables -t nat -A POSTROUTING -p tcp -d 192.168.12.77 --dport 80 -j SNAT --to-source 192.168.12.87

 

 

 

Using NetCat:

A naive try would be something like this:

$ nc -l 8082 | nc remote_host 80

Yes, it does forward the request from local port 8082 to remote_host:80, but the response is dumped to stdout, not routed back to the client as expected.

Using a named pipe makes it work:

$ mkfifo backpipe
$ nc -l 8082 0<backpipe | nc remote_host 80 1>backpipe

Use tee to get a glimpse of the response through the pipe (I wasn’t able to find a way to dump the request):

$ nc -k -l 8082 0<backpipe | nc localhost 80 | tee backpipe
HTTP/1.1 200 OK
Date: Fri, 30 Sep 2011 22:11:27 GMT
Server: Apache/2.2.16 (Unix)
Last-Modified: Sat, 20 Nov 2004 20:16:24 GMT
ETag: "2d0945-2c-3e9564c23b600"
Accept-Ranges: bytes
Content-Length: 44
Content-Type: text/html

<html><body><h1>It works!</h1></body></html>

The GNU netcat has a different syntax than the stock nc. It also supports different switches.

  1. To listen to port 1234:
    $ netcat -l -p 1234
    
  2. To make bash a server on port 1234:
    $ netcat -l -p 1234 -e /bin/bash
    
  3. Forward local port 8082 to remote port 80:
    $ ./netcat -L 192.168.80.143:80 -p 8082
    
  4. Port forwarding with hex dump:
    $ ./netcat -L 192.168.80.143:80 -p 8082 -x
    Received 174 bytes from the socket
    00000000  47 45 54 20  2F 20 48 54  54 50 2F 31  2E 31 0D 0A  GET / HTTP/1.1..
    00000010  55 73 65 72  2D 41 67 65  6E 74 3A 20  63 75 72 6C  User-Agent: curl
    00000020  2F 37 2E 32  31 2E 30 20  28 78 38 36  5F 36 34 2D  /7.21.0 (x86_64-
    00000030  72 65 64 68  61 74 2D 6C  69 6E 75 78  2D 67 6E 75  redhat-linux-gnu
    00000040  29 20 6C 69  62 63 75 72  6C 2F 37 2E  32 31 2E 30  ) libcurl/7.21.0
    00000050  20 4E 53 53  2F 33 2E 31  32 2E 31 30  2E 30 20 7A   NSS/3.12.10.0 z
    00000060  6C 69 62 2F  31 2E 32 2E  35 20 6C 69  62 69 64 6E  lib/1.2.5 libidn
    00000070  2F 31 2E 31  38 20 6C 69  62 73 73 68  32 2F 31 2E  /1.18 libssh2/1.
    00000080  32 2E 34 0D  0A 48 6F 73  74 3A 20 36  37 2E 31 33  2.4..Host: 67.13
    00000090  30 2E 36 39  2E 31 34 33  3A 38 30 38  32 0D 0A 41  0.69.143:8082..A
    000000A0  63 63 65 70  74 3A 20 2A  2F 2A 0D 0A  0D 0A        ccept: */*....  
    Sent 276 bytes to the socket
    00000000  48 54 54 50  2F 31 2E 31  20 32 30 30  20 4F 4B 0D  HTTP/1.1 200 OK.
    00000010  0A 44 61 74  65 3A 20 46  72 69 2C 20  33 30 20 53  .Date: Fri, 30 S
    00000020  65 70 20 32  30 31 31 20  32 32 3A 33  32 3A 30 35  ep 2011 22:32:05
    00000030  20 47 4D 54  0D 0A 53 65  72 76 65 72  3A 20 41 70   GMT..Server: Ap
    00000040  61 63 68 65  2F 32 2E 32  2E 31 36 20  28 55 6E 69  ache/2.2.16 (Uni
    00000050  78 29 0D 0A  4C 61 73 74  2D 4D 6F 64  69 66 69 65  x)..Last-Modifie
    00000060  64 3A 20 53  61 74 2C 20  32 30 20 4E  6F 76 20 32  d: Sat, 20 Nov 2
    00000070  30 30 34 20  32 30 3A 31  36 3A 32 34  20 47 4D 54  004 20:16:24 GMT
    00000080  0D 0A 45 54  61 67 3A 20  22 32 64 30  39 34 35 2D  ..ETag: "2d0945-
    00000090  32 63 2D 33  65 39 35 36  34 63 32 33  62 36 30 30  2c-3e9564c23b600
    000000A0  22 0D 0A 41  63 63 65 70  74 2D 52 61  6E 67 65 73  "..Accept-Ranges
    000000B0  3A 20 62 79  74 65 73 0D  0A 43 6F 6E  74 65 6E 74  : bytes..Content
    000000C0  2D 4C 65 6E  67 74 68 3A  20 34 34 0D  0A 43 6F 6E  -Length: 44..Con
    000000D0  74 65 6E 74  2D 54 79 70  65 3A 20 74  65 78 74 2F  tent-Type: text/
    000000E0  68 74 6D 6C  0D 0A 0D 0A  3C 68 74 6D  6C 3E 3C 62  html....<html><b
    000000F0  6F 64 79 3E  3C 68 31 3E  49 74 20 77  6F 72 6B 73  ody><h1>It works
    00000100  21 3C 2F 68  31 3E 3C 2F  62 6F 64 79  3E 3C 2F 68  !</h1></body></h
    00000110  74 6D 6C 3E                                         tml>           
    

CKDLEE Simply say, it’ll be like this:

On the server:
mkfifopipe_name
nc -l -pport_number<pipe_nameย  |ย program_name>pipe_name

On the client:
ncย server_machine_nameย port_number

1. <“while true” should be added in shell>
jiafei427@CKUBU:~/platform_external_netcat-master$ mkfifo nidie
jiafei427@CKUBU:~/platform_external_netcat-master$ netcat -v -l 8888 0<nidie | netcat -v 127.0.0.1 9999 1>nidie
Listening on [0.0.0.0] (family 0, port 8888)
Connection to 127.0.0.1 9999 port [tcp/*] succeeded!
Connection from [127.0.0.1] port 8888 [tcp/*] accepted (family 2, sport 48926)
wocao
nidaye

2.
jiafei427@CKUBU:~/platform_external_netcat-master$ netcat 127.0.0.1 8888
wocao
nidaye

 

Ref.:

http://askubuntu.com/questions/50064/reverse-port-tunnelling

http://www.xinotes.net/notes/note/1529/

http://superuser.com/questions/607783/can-i-pipe-redirect-a-console-application-through-netcat-so-it-can-be-used-remot

Linux bash tiny-tips

Killin’ the process via its id in Linux:
#ps -aux | grep IVI_ConnectionManager | awk ‘NR==1{print $2}’ | xargs kill

 

Launch two different shell in one script:

#/bin/bash

command_1 && command_2

(Notice the & sign at the end of each line. This will cause the shell to fork that process into the background and continue execution. Note how it’s different from &&, which is basically an and sign, command_1 && command_2 will executecommand_1 and if it exits with success, only then run command_2, while command_1 & command_2 will start the second right after the first.)

 

Get input repeatedly from the shell:

Value 127 (non-zero) indicates command cyberciti failed to execute. You can use exit status in shell scripting too. You can store result of exit status in variable. Consider following shell script:

#!/bin/bash
echo -n "Enter user name : "
read USR
cut -d: -f1 /etc/passwd | grep "$USR" > /dev/null
OUT=$?
if [ $OUT -eq 0 ];then
   echo "User account found!"
else
   echo "User account does not exists in /etc/passwd file!"
fi

Save and execute the script as follows:
$ chmod +x script.sh
$ ./script.sh

Output:

Enter user name : jradmin
User account does not exists in /etc/passwd file

Try it one more time:
$ ./script.sh
Output:

Enter user name : vivek
User account found

 

you can filter out messages to stderr. I prefer to redirect them to stdout like this.

find / -name artย  2>&1 | grep -v “Permission denied”

Explanation:

In short, all regular output goes to standard output (stdout). All error messages to standard error (stderr).

grep usually finds/prints the specified string, the -v inverts this, so it finds/prints every string that doesn’t contain “Permission denied”. All of your output from the find command, including error messages usually sent to stderr (file descriptor 2) go now to stdout(file descriptor 1) and then get filtered by the grep command.

This assumes you are using the bash/sh shell.

Under tcsh/csh you would use

find / -name art |& grep ….

 

Shell:

jiafei427@CKUBU:~/workspace/kernel/modules/test$ cat infinity_touch.sh
i=0
while [ 1 ]
do
echo “wocao”
sleep 0.01
i=i+1
done

That will repeat print wocao with sleeping 0.01 second

 

create folder with certain format of date

mkdir "$(date +"%d-%m-%Y")"
cd "$(date +"%d-%m-%Y")"

In the extreme case a day passes between the first and the second statement, that won’t work. Change it to:

d="$(date +"%d-%m-%Y")"
mkdir "$d"
cd "$d"

Explanation: Theย $(...)ย returns the output from the subcommands as a string, which we store in the variableย d.

 

Getting shell variables intoย awkย may be done in several ways. Some are better than others.


This is the best way to do it. It uses theย -vย option: (P.S. use a space afterย -vย or it will be less portable. E.g.,ย awk -v var=ย notย awk -vvar)

variable="line one\nline two"
awk -v var="$variable" 'BEGIN {print var}'
line one
line two

This should be compatible with mostย awkย and variable is available in theย BEGINย block as well:

 

 

Read full link from symbolic link file:

readlink -f `which command`

Ifย commandย is in yourย $PATHย variable , otherwise you need to specify the path you know.

You can useย awkย with aย systemย callย readlinkย to get the equivalent of anย lsย output with full symlink paths. For example:

ls | awk '{printf("%s ->", $1); system("readlink -f " $1)}'

Will display e.g.

thin_repair ->/home/user/workspace/boot/usr/bin/pdata_tools
thin_restore ->/home/user/workspace/boot/usr/bin/pdata_tools
thin_rmap ->/home/user/workspace/boot/usr/bin/pdata_tools
thin_trim ->/home/user/workspace/boot/usr/bin/pdata_tools
touch ->/home/user/workspace/boot/usr/bin/busybox
true ->/home/user/workspace/boot/usr/bin/busybox

 

awkย  system output to certain variable:

To run a system command inย awkย you can either useย system()ย orย cmd | getline.

I preferย cmd | getlineย because it allows you to catch the value into a variable:

$ awk 'BEGIN {"date" |  getline mydate; close("date"); print "returns", mydate}'
returns Thu Jul 28 10:16:55 CEST 2016

More generally, you can set the command into a variable:

awk 'BEGIN {
       cmd = "date -j -f %s"
       cmd | getline mydate
       close(cmd)
     }'

Note it is important to useย close()ย to prevent getting a “makes too many open files” error if you have multiple results (thanksย mateuscbย for pointing this out in comments).

 

Note: Coprocess is GNU awk specific. Anyway another alternative is using getline

cmd = "strip "$1
while ( ( cmd | getline result ) > 0 ) {
  print  result
} 
close(cmd)
or sth. like this:
awk 'BEGIN{"date"|getline d; print "Current date is:" , d }'

 

list all symbolic links in a directory:

Parsingย lsย is aย Bad Ideaยฎ, prefer a simpleย findย in that case:

find . -type l -ls

To only process the current directory:

find . -maxdepth 1 -type l -ls

 

 

How to find and list all the symbolic links created for a particular file?

Here is an example:

find -L /dir/to/start -xtype l -samefile ~/Pictures

or, maybe better:

find -L /dir/to/start -xtype l -samefile ~/Pictures 2>/dev/null

to get rid of some errors likeย Permission denied,ย Too many levels of symbolic links, orย File system loop detectedย whichย findย throws them when doesn’t have the right permissions or other situations.

  • -Lย – Follow symbolic links.
  • -xtype lย – File is symbolic link
  • -samefile nameย – File refers to the same inode asย name. Whenย -Lย is in effect, this can include symbolic links.

 

 

 

1. Replacing all occurrences of one string with another in all files in the current directory:

These are for cases where youย knowย that the directory contains only regular files and that you want to process all non-hidden files. If that is not the case, use the approaches in 2.

Allย sedย solutions in this answer assume GNUย sed. If using FreeBSD or OS/X, replaceย -iย withย -i ''. Also note that the use of theย -iย switch with any version ofย sedย has certain filesystemย security implicationsย and is inadvisable in any script which you plan to distribute in any way.

  • Non recursive, files in this directory only:
    sed -i -- 's/foo/bar/g' *
    perl -i -pe 's/foo/bar/g' ./* 

    (theย perlย one will fail for file names ending inย |ย or space)).

  • Recursive, regular files (including hidden ones) in this and all subdirectories
    find . -type f -exec sed -i 's/foo/bar/g' {} +

    If you are using zsh:

    sed -i -- 's/foo/bar/g' **/*(D.)

    (may fail if the list is too big, seeย zargsย to work around).

    Bash can’t check directly for regular files, a loop is needed (braces avoid setting the options globally):

    ( shopt -s globstar dotglob;
        for file in **; do
            if [[ -f $file ]] && [[ -w $file ]]; then
                sed -i -- 's/foo/bar/g' "$file"
            fi
        done
    )

    The files are selected when they are actual files (-f) and they are writable (-w).

2. Replace only if the file name matches another string / has a specific extension / is of a certain type etc:

  • Non-recursive, files in this directory only:
    sed -i -- 's/foo/bar/g' *baz*    ## all files whose name contains baz
    sed -i -- 's/foo/bar/g' *.baz    ## files ending in .baz
  • Recursive, regular files in this and all subdirectories
    find . -type f -name "*baz*" -exec sed -i 's/foo/bar/g' {} +

    If you are using bash (braces avoid setting the options globally):

    ( shopt -s globstar dotglob
        sed -i -- 's/foo/bar/g' **baz*
        sed -i -- 's/foo/bar/g' **.baz
    )

    If you are using zsh:

    sed -i -- 's/foo/bar/g' **/*baz*(D.)
    sed -i -- 's/foo/bar/g' **/*.baz(D.)

    Theย --ย serves to tellย sedย that no more flags will be given in the command line. This is useful to protect against file names starting withย -.

  • If a file is of a certain type, for example, executable (seeย man findย for more options):
    find . -type f -executable -exec sed -i 's/foo/bar/g' {} +

    zsh:

    sed -i -- 's/foo/bar/g' **/*(D*)

3. Replace only if the string is found in a certain context

  • Replaceย fooย withย barย only if there is aย bazย later on the same line:
    sed -i 's/foo\(.*baz\)/bar\1/' file

    Inย sed, usingย \( \)ย saves whatever is in the parentheses and you can then access it withย \1. There are many variations of this theme, to learn more about such regular expressions, seeย here.

  • Replaceย fooย withย barย only ifย fooย is found on the 3d column (field) of the input file (assuming whitespace-separated fields):
    gawk -i inplace '{gsub(/foo/,"baz",$3); print}' file

    (needsย gawkย 4.1.0 or newer).

  • For a different field just useย $Nย whereย Nย is the number of the field of interest. For a different field separator (:ย in this example) use:
    gawk -i inplace -F':' '{gsub(/foo/,"baz",$3);print}' file

    Another solution usingย perl:

    perl -i -ane '$F[2]=~s/foo/baz/g; $" = " "; print "@F\n"' foo 

    NOTE: both theย awkย andย perlย solutions will affect spacing in the file (remove the leading and trailing blanks, and convert sequences of blanks to one space character in those lines that match). For a different field, useย $F[N-1]ย whereย Nย is the field number you want and for a different field separator use (theย $"=":"ย sets the output field separator toย :):

    perl -i -F':' -ane '$F[2]=~s/foo/baz/g; $"=":";print "@F"' foo 
  • Replaceย fooย withย barย only on the 4th line:
    sed -i '4s/foo/bar/g' file
    gawk -i inplace 'NR==4{gsub(/foo/,"baz")};1' file
    perl -i -pe 's/foo/bar/g if $.==4' file

4. Multiple replace operations: replace with different strings

  • You can combineย sedย commands:
    sed -i 's/foo/bar/g; s/baz/zab/g; s/Alice/Joan/g' file

    Be aware that order matters (sed 's/foo/bar/g; s/bar/baz/g'ย will substituteย fooย withย baz).

  • or Perl commands
    perl -i -pe 's/foo/bar/g; s/baz/zab/g; s/Alice/Joan/g' file
  • If you have a large number of patterns, it is easier to save your patterns and their replacements in aย sedย script file:
    #! /usr/bin/sed -f
    s/foo/bar/g
    s/baz/zab/g
  • Or, if you have too many pattern pairs for the above to be feasible, you can read pattern pairs from a file (two space separated patterns, $pattern and $replacement, per line):
    while read -r pattern replacement; do   
        sed -i "s/$pattern/$replacement/" file
    done < patterns.txt
  • That will be quite slow for long lists of patterns and large data files so you might want to read the patterns and create aย sedย script from them instead. The following assumes aย <space>delimiter separates a list ofย MATCH<space>REPLACEย pairs occurring one-per-line in the fileย patterns.txtย :
    sed 's| *\([^ ]*\) *\([^ ]*\).*|s/\1/\2/g|' <patterns.txt |
    sed -f- ./editfile >outfile

    The above format is largely arbitrary and, for example, doesn’t allow for aย <space>ย in either ofย MATCHย orย REPLACE. The method is very general though: basically, if you can create an output stream which looks like aย sedย script, then you can source that stream as aย sedย script by specifyingย sed‘s script file asย -stdin.

  • You can combine and concatenate multiple scripts in similar fashion:
    SOME_PIPELINE |
    sed -e'#some expression script'  \
        -f./script_file -f-          \
        -e'#more inline expressions' \
    ./actual_edit_file >./outfile

    A POSIXย sedย will concatenate all scripts into one in the order they appear on the command-line. None of these need end in aย \newline.

  • grepย can work the same way:
    sed -e'#generate a pattern list' <in |
    grep -f- ./grepped_file
  • When working with fixed-strings as patterns, it is good practice to escape regular expressionย metacharacters. You can do this rather easily:
    sed 's/[]$&^*\./[]/\\&/g
         s| *\([^ ]*\) *\([^ ]*\).*|s/\1/\2/g|
    ' <patterns.txt |
    sed -f- ./editfile >outfile

5. Multiple replace operations: replace multiple patterns with the same string

  • Replace any ofย foo,ย barย orย bazย withย foobar
    sed -Ei 's/foo|bar|baz/foobar/g' file
  • or
    perl -i -pe 's/foo|bar|baz/foobar/g' file

 

 

…TBD

Linux tiny Tips

(for myself…)

Transfer file to the destination pc:

option 1:

#scp mylocalfile.txt root@<destination_ip>:/recipient/directory/

option 2:

A simple option is to use netcat (nc). This is particularly useful on stripped down Linux systems where services like ssh and ftp are turned off.

On destination machine run the following command: nc -l -p 1234 > out.file

On source machine run the following command: nc -w 3 <dest-ip-adr> 1234 < out.file

For more details look, for example, here.

There are also netcat implementations for Windows, e.g. ncat.

 

How to Linux Terminal Split Screen With Screen:

pre-requirement:

#sudo apt-get install screen

How to split screen
a)Split the Window
Horizontally
Ctrl + a, Then Press Shift + s
or
Vertically
Ctrl + a, Then Press Shift + \

b)Switch between spilted windows
Ctrl + a, Then Press Tab
or
Ctrl + a, Then Type :focus
* Here :focus is a command

c)In the spited window use following command to open existing session
Ctrl + a, Then Press 0-9
or
Ctrl + a, Then Press n or p
or
Ctrl + a, Then Press Shift + ‘
or
Ctrl + a, Then Presss c

d)Resize a splitted window/region
Ctrl + a, Then Type :resize 25
* Here :resize is a command

e)Remove current splitted window/region
Ctrl + a, Then Type :remove
* Here :remove is a command
or
Ctrl + a, Then Press Shift + x

f)Remove all spiltted windows/regions except the current one.
Ctrl + a, Then Type :only
* Here :only is a command
or
Ctrl + a, Then Press Shift +q

g) Close the screen and all regions
Ctrl + a, Then Press \

Screen Terminal Multiplexer Commands

 

Editor:

VIM,eMacs, Sublime-text

Terminal:

Guake Terminal

Connection & File Transfer:

SecureCRT,ย  SSH, putty, FileZilla

 

Run Program in background and save the log to the file:

Redirect the output to a file like this:

./a.sh > somefile 2>&1 &

This will redirect both stdout and stderr to the same file. If you want to redirect stdout and stderr to two different files use this:

./a.sh > stdoutfile 2> stderrfile &

 

 

Task: List or display loaded modules

Open a terminal or login over the ssh session and type the following command
$ less /proc/modules
Sample outputs:

sha1_generic 1759 4 - Live 0xffffffffa059e000
arc4 1274 2 - Live 0xffffffffa0598000
ecb 1841 2 - Live 0xffffffffa0592000
ppp_mppe 5240 2 - Live 0xffffffffa058b000
ppp_async 6245 1 - Live 0xffffffffa0584000
crc_ccitt 1323 1 ppp_async, Live 0xffffffffa057e000
ppp_generic 19291 6 ppp_mppe,ppp_async, Live 0xffffffffa0572000
slhc 4003 1 ppp_generic, Live 0xffffffffa056c000
ext3 106854 1 - Live 0xffffffffa0546000
jbd 37349 1 ext3, Live 0xffffffffa0533000
sha256_generic 8692 2 - Live 0xffffffffa0525000
aes_x86_64 7340 2 - Live 0xffffffffa0517000
aes_generic 25714 1 aes_x86_64, Live 0xffffffffa050b000
....
...
....
ahci 32950 20 - Live 0xffffffffa007b000
libata 133824 3 ata_generic,pata_jmicron,ahci, Live 0xffffffffa0045000
scsi_mod 126901 3 usb_storage,sd_mod,libata, Live 0xffffffffa0012000
thermal 11674 0 - Live 0xffffffffa0009000
thermal_sys 11942 3 video,processor,thermal, Live 0xffffffffa0000000

To see nicely formatted output, type:
$ lsmod
Sample outputs:

Module                  Size  Used by
sha1_generic            1759  4
arc4                    1274  2
ecb                     1841  2
ppp_mppe                5240  2
ppp_async               6245  1
crc_ccitt               1323  1 ppp_async
ppp_generic            19291  6 ppp_mppe,ppp_async
slhc                    4003  1 ppp_generic
.................

First column is Module name and second column is the size of the modules i..e the output format is module name, size, use count, list of referring modules.

Finding more info about any module or driver

Type the following command:
# modinfo driver-Name-Here
# modinfo thermal_sys
# modinfo e1000e

Sample outputs:

filename:       /lib/modules/2.6.32-5-amd64/kernel/drivers/net/e1000e/e1000e.ko
version:        1.2.20-k2
license:        GPL
description:    Intel(R) PRO/1000 Network Driver
author:         Intel Corporation, <linux.nics@intel.com>
srcversion:     AB58ACECA1618E521F58503
alias:          pci:v00008086d00001503sv*sd*bc*sc*i*

 

Disk Utility:

#du -sh file_path

Explanation

du command estimates file_path space usage
The options -sh are (from man du):

-s, –summarize
display only a total for each argument

-h, –human-readable
print sizes in human readable format (e.g., 1K 234M 2G)
To check more than one directory and see the total, use du -sch:
-c, –total
produce a grand total

You could extend this command to:

#du -h –max-depth=1 | sort -hr

which will give you the size of all sub-folders (level 1). The output will be sorted (largest folder on top).

Print all usb devices:

#cat /proc/bus/input/devices

OR
#cat /proc/bus/usb/devices

Grep tip:

For BSD or GNU grep you can use -B num to set how many lines before the match and -A num for the number of lines after the match.

#grep -B 3 -A 2 foo README.txt

If you want the same number of lines before and after you can use -C num.

#grep -C 3 foo README.txt

This will show 3 lines before and 3 lines after.

 

REF.

http://stackoverflow.com/questions/15807122/telnet-file-transfer-between-two-linux-machines

http://fosshelp.blogspot.kr/2014/02/how-to-linux-terminal-split-screen-with.html

How to Split Terminal Screen in Linux Ubuntu 14.04

http://www.cyberciti.biz/faq/howto-display-list-of-modules-or-device-drivers-in-the-linux-kernel/