tag:blogger.com,1999:blog-1501844987649494812024-03-16T11:52:23.905-07:00Linux shell - the black console of madnessFeel the power of the linux console. Useful commands, funny tools and scripts.berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.comBlogger36125tag:blogger.com,1999:blog-150184498764949481.post-70917816989678378352020-03-09T16:36:00.002-07:002020-03-09T16:41:52.863-07:00World ascii map in the terminal? No problem.Want scalable world map in the console?. No problem. Just type<br />
<br />
<b>$ <span class="text-gray-dark mr-2" itemprop="about">telnet mapscii.me</span></b><br />
<br />
<span class="text-gray-dark mr-2" itemprop="about">and have fun.</span><br />
<br />
<span class="text-gray-dark mr-2" itemprop="about">Project at:</span><br />
<br />
<span class="text-gray-dark mr-2" itemprop="about"><a href="https://github.com/rastapasta/mapscii">https://github.com/rastapasta/mapscii</a></span>berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0tag:blogger.com,1999:blog-150184498764949481.post-26713221236239962662017-02-07T00:35:00.000-08:002017-09-24T11:12:43.408-07:00Bc as CPU benchmark - revisitedRecently I found little script I wrote that may be helpful benchmarking machines with bc:<br />
<br />
if [ -n "$1" ]; then<br /> PRECISION="$1"<br />else<br /> PRECISION=10000<br />fi<br /><br />CPUINFO="/proc/cpuinfo"<br />MEMINFO="/proc/meminfo"<br /><br />echo "Linux shell CPU benchmark"<br />echo "------------------------------------"<br />echo "Usage: $0 <precision> "<br />echo<br /><br />BCCMD=$(which bc)<br />if [ -z "$BCCMD" ]; then<br /> echo "bc command not found... exiting now"<br /> exit<br />fi<br /><br />echo -n "Getting CPU data.... "<br />#get cpu data<br />if [ -r $CPUINFO ]; then<br /> VENDOR=$(cat "$CPUINFO" | grep vendor_id | head -n1 | cut -f2 -d':'| cut -c2- )<br /> MODEL=$(cat "$CPUINFO" | grep "model name" | head -n1 | cut -f2 -d':' | cut -c2- | sed -e 's/ */ /g')<br /> SPEED=$(cat "$CPUINFO" | grep "cpu MHz" | head -n1 | cut -f2 -d':' | cut -c2- )<br /> CACHE=$(cat "$CPUINFO" | grep "cache size" | head -n1 | cut -f2 -d':' | cut -c2-)<br /> LMFLAG=$(cat "$CPUINFO" | grep flags | head -n1 | cut -f2 -d':' | tr ' ' '\n' | grep lm)<br /><br /> if [ "$LMFLAG" = "lm" ]; then<br /> CPU64="1"<br /> else<br /> CPU64="0"<br /> fi<br /> echo "[OK]"<br />else<br /> CPU64=""<br /> echo "[NOT FOUND]"<br />fi<br /><br /><br />echo -n "Getting memory data..."<br />#get memory data<br />if [ -r "$MEMINFO" ]; then<br /> MEMTOTAL=$(cat "$MEMINFO" | grep MemTotal | cut -f2 -d':' | cut -c2- | sed -e 's/ *//g')<br /> echo "[OK]"<br />else<br /> MEMTOTAL=""<br /> echo "[NOT FOUND]"<br />fi<br />echo -n "Getting machine, OS and Kernel data..."<br />#get machine,os,kernel data<br />UNAME_CMD=$(which uname)<br />if [ -n "$UNAME_CMD" ]; then<br /> #MACHINE=$($UNAME_CMD -m)<br /> OS=$($UNAME_CMD -o)<br /> KERNEL=$($UNAME_CMD -r)<br /> echo "[OK]"<br />else<br /> OS=""<br /> KERNEL=""<br /> echo "[NOT FOUND]"<br />fi<br /><br />#get distro name<br />LSBRELEASE=$(which lsb_release 2>/dev/null)<br />if [ -n "$LSBRELEASE" ]; then<br /> DISTRO=$($LSBRELEASE -d | cut -f2 -d':' | tr -d '\t')<br />elif [ $(ls /etc/*version 2>/dev/null | wc -l) -gt 0 ]; then<br /> DISTRO=$(cat $(ls /etc/*version | head -n1))<br />elif [ $(ls /etc/*release 2>/dev/null | wc -l) -gt 0 ]; then<br /> DISTRO=$(cat $(ls /etc/*release | head -n1))<br />elif [ -f /proc/version ]; then<br /> DISTRO=$(cat /proc/version)<br />else<br /> echo "[NOT FOUND]"<br /> DISTRO=""<br />fi<br /><br /><br />echo -n "Benchmarking (precision=$PRECISION)..."<br />#benchmarking<br />if [ -n "$BCCMD" ]; then<br /> SCORE=$((time echo "scale=$PRECISION;a(1)*4" | $BCCMD -l) 2>&1 | grep real | sed -e 's/ */ /g' | tr '\t' ' ' | cut -f2 -d' ')<br /> #echo "SCORE#$SCORE"<br /> echo "[OK]"<br />else<br /> SCORE=""<br /> echo "[ERROR]"<br />fi<br /><br />#print values<br />echo<br />echo "--- cut here ---"<br />echo "DISTRO#$DISTRO"<br />echo "OS#$OS"<br />echo "KERNEL#$KERNEL"<br />echo "VENDOR#$VENDOR"<br />echo "MODEL#$MODEL"<br />echo "SPEED#$SPEED"<br />echo "CACHE#$CACHE"<br />echo "CPU64#$CPU64"<br />echo "MEMTOTAL#$MEMTOTAL"<br />echo "PRECISION#$PRECISION"<br />echo "SCORE#$SCORE"<br />echo "--- cut here ---"<br /></precision>berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com2tag:blogger.com,1999:blog-150184498764949481.post-50476208124880264062016-11-17T04:22:00.001-08:002017-09-24T11:12:43.442-07:00Summing numbers in a columnTo sum several values in column from file we can use <b>paste </b>command and bash arithmetic expressions<br />
<br />
$ echo $(($(paste -s -d'+' nums)))<br />
<br />
I've always used <b>tr</b> to glue the values into one line, but the remaining '+' sign must be removed:<br />
<br />
$ echo $(($(cat nums | tr '\n' '+' |sed 's/.$//')))<br />
<br />
Or with awk:<br />
<br />
$ awk '{s+=$1}; END { print s}' nums <br />
<br />
<br />
<br />
<br />berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0tag:blogger.com,1999:blog-150184498764949481.post-57582192740277842692016-07-16T15:51:00.000-07:002017-09-24T11:12:43.446-07:00wget and line delimiters CR LFIf you download file with wget and the filename comes from windows file, the output filename will always have %0D at the end. To get rid of this, instead of:<br />
<br />
wget $filename<br />
<br />
use<br />
<br />
wget $(echo "$filename" | tr -d '\r')<br />
<br />
This will do the trickberdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0tag:blogger.com,1999:blog-150184498764949481.post-81476894046856707812016-03-06T16:27:00.001-08:002017-09-24T11:12:43.472-07:00Raspberry Pi - booting into console modeBy default Raspbian boots into GUI mode. I don't need that. I wanted to change it to boot into command line. In Slackware or RedHat distros I always did it by editing runlevel in /etc/inittab.<br />
Raspbian is different. I did it with raspi-config utility:<br />
<br />
$ sudo raspi-config<br />
<br />
And then I chose "Boot options"=>"B2 Console Autologin Text console, automatically logged in as 'pi' user"berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0tag:blogger.com,1999:blog-150184498764949481.post-46014099701501681332016-02-21T13:44:00.001-08:002017-09-24T11:12:43.431-07:00InsecureMag - downloading all issuesI bumped into InsecureMag (<a href="https://www.helpnetsecurity.com/insecuremag-archive/">https://www.helpnetsecurity.com/insecuremag-archive/</a>) - the free magazine on security. As I'm interested in it I decided to check it out. It's totally free. But being lazy I wanted to have all the issues on my harddisk. As usual bash comes with help:<br />
<br />
#!/bin/bash<br />
for i in {1..49}; do<br />
link="https://www.helpnetsecurity.com/dl/insecure/INSECURE-Mag-$i.pdf"<br />
wget -c "$link"<br />
done<br />
<br />
<br />
Have a nice reading :) <br />
<br />
<br />
<br />
<br />
<br />berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com5tag:blogger.com,1999:blog-150184498764949481.post-57882195220625876252016-02-21T10:16:00.001-08:002017-09-24T11:12:43.456-07:00Raspberry Pi - Setting Static IP addressFor Christmas I bought for myself :) Raspberry Pi and installed Raspbian and OpenELEC on this. For now I usually use it as low-energy uploader/downloader or photo batch processor.<br />
I want to have remote access to it also from outside my home network. By default Raspbian uses DHCP to obtain IP address from my home DHCP server. As I need to forward ports from my ISP router to my Raspberry PI, I need to give it always static IP address. I've been messing around with it a bit until I figured it out. Here is the recipe:<br />
<br />
1. Turn off dhcp client which will always override things<br />
<br />
$ sudo update-rc.d dhcpcd disable<br />
<br />
You can also do it old school way by removing execute right:<br />
<br />
$ sudo chmod a-x /etc/init.d/dhcpcd<br />
<br />
2. Add the following lines to /etc/network/interfaces (the bold ones are new)<br />
<br />
auto lo<br />
<b>auto eth0</b><br /><br />iface lo inet loopback<br /><br /><b>#</b>iface eth0 inet manuala<br /><b>iface eth0 inet static<br />address 192.168.1.100<br />gateway 192.168.1.254<br />netmask 255.255.255.0</b><br />
<br />
3. We need /etc/resolv.conf with static dns entry. This file is always rewritten by resolvconf utility, so we need to change it<b>.</b> In <i>/etc/resolvconf.conf</i> we add one line:<br />
<br />
<b>name_servers=192.168.1.254</b><br />
<br />
My nameserver is my gateway at the same time. <b> </b><br />
<br />
4. Restart networking services by:<br />
<br />
$ sudo /etc/init.d/networking restart<br />
<br />
Works for me. We can check if everything is ok running:<br />
<br />
$ /sbin/ifconfig berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0tag:blogger.com,1999:blog-150184498764949481.post-8429115103435486922016-01-09T14:46:00.000-08:002017-09-24T11:12:43.475-07:00Checking the proxy serversSometimes we need some privacy on the web. So I needed good proxy servers. I googled a bit and found good list of them here:<br />
<br />
<a href="http://proxylist.hidemyass.com/">http://proxylist.hidemyass.com/</a><br />
<br />
But checking them one by one is boring, so I copied the interesed one into the file called list (copy/paste interesting fragments from web browser :):<br />
<br />
198.169.246.30|80|flagCanada<br />
200.180.32.58|80|flagBrazil<br />
211.144.81.68|18000|flagChina<br />
84.45.246.38|3128|flagUK<br />
95.168.217.24|3128|flagCzechia<br />
<br />
<br />
There are some environment variables called <i>http_proxy</i> and <i>https_proxy. </i>We can set them system-wide to use proxy like this:<br />
<br />
export http_proxy="http://host:port"<br />
<br />
or<br />
<br />
export http_proxy="http://username:pass@host:port"<br />
<br />
if proxy requires autentication.<br />
<br />
So to check if proxy works we just need to set this env. variable and try to download something with wget. We can do this in the loop and get this list as input<br />
<br />
<br />
--- cut ---<br />
<br />
<b>#!/bin/bash<br />n=1<br /><br />for l in $(cat list); do<br />HOST=$(echo $l | cut -f1 -d '|')<br />PORT=$(echo $l | cut -f2 -d '|')<br /><br />export http_proxy="http://${HOST}:${PORT}"<br />export https_proxy=$http_proxy<br /><br />wget https://www.eff.org/files/https-everywhere2.jpg -T 10 -O ${n}.jpg<br /><br />unset http_proxy<br />unset https_proxy</b><br />
<br />
<b>n=n+1</b><br />
<b>done;</b><br />
<br />
--- cut ---<br />
<br />
-T means timeout<br />
-O means name of the output file<br />
<br />
This way I got several files numberd as proxy server position on the list. The 0 size ones are non-working proxies.<br />
<br />
-rw-r--r-- 1 pi pi 0 Jan 9 17:56 10.jpg<br />
-rw-r--r-- 1 pi pi 20216 Jun 12 2013 1.jpg<br />
-rw-r--r-- 1 pi pi 0 Jan 9 19:23 2.jpg<br />
-rw-r--r-- 1 pi pi 20216 Jun 12 2013 3.jpg<br />
-rw-r--r-- 1 pi pi 0 Jan 9 19:23 4.jpg<br />
-rw-r--r-- 1 pi pi 0 Jan 9 19:23 5.jpg<b><br /></b><br />
<br />
<br />
<br />berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0tag:blogger.com,1999:blog-150184498764949481.post-42555844101458990052015-10-18T01:43:00.001-07:002017-12-17T01:29:13.316-08:00Inside my video player ASUS HDP-R1 - CPU usage without topFor several years I've been using ASUS HDP-R1 video player for my media files. Recently I've had samoe problems playing big mkv file. I decided to dig into the player to see what's inside and check the CPU usage while playing movies.<br />
<br />
We can telnet to the player:<br />
<br />
<i>Venus login: root<br />warning: cannot change to home directory<br /><br /><br />BusyBox v1.1.3 (2011.05.25-05:45+0000) Built-in shell (ash)<br />Enter 'help' for a list of built-in commands.<br /><br />/ #</i><br />
<br />
uname -a gives us:<br />
<i> </i><br />
<i>/ # uname -a<br />Linux Venus 2.6.12.6-VENUS #323634 Wed May 25 13:40:36 CST 2011 mips unknown</i><br />
<i><br /></i>
And let's check the CPU:<br />
<i><br /></i>
<i>/ # cat /proc/cpuinfo<br />system type : Realtek Venus<br />processor : 0<br />cpu model : MIPS 24K V7.8<br />BogoMIPS : 269.51<br />wait instruction : yes<br />microsecond timers : yes<br />tlb_entries : 32<br />extra interrupt vector : yes<br />hardware watchpoint : yes<br />ASEs implemented : mips16<br />VCED exceptions : not available<br />VCEI exceptions : not available</i><br />
<br />
I just wanted to check CPU usage however neither <i>top </i>nor <i>mpstat </i>commands are available.<br />
My first plan was to compile top binary from sources linking it staticly but it's not x86 architecture so I had no other hardware to do that. Of cource as you suppose there are no compilers available in this box.<br />
I came up with the following solution - read cpu usage directly from the /proc filesystem with /bin/sh.<br />
<br />
Let's try:<br />
<br />
<i>/ # cat /proc/stat<br />cpu 4916 3748 25087 1879421 2633 326 459 0<br />cpu0 4916 3748 25087 1879421 2633 326 459 0<br />intr 2046721 0 0 86168 40259 0 3704 1916590 0<br />ctxt 10942464<br />btime 1388589011<br />processes 6008<br />procs_running 3<br />procs_blocked 0</i><br />
<i></i><br />
We are interested in the first line giving us the summary cpu usage of all the cores (in this case only cpu0). The magic numbers are:<br />
<ul>
<li>user: normal processes executing in user mode</li>
<li>nice: niced processes executing in user mode</li>
<li>system: processes executing in kernel mode</li>
<li>idle: twiddling thumbs</li>
<li>iowait: waiting for I/O to complete</li>
<li>irq: servicing interrupts</li>
<li>softirq: servicing softirqs</li>
</ul>
These values change over the time. To measure cpu usage we must take the user,system and idle values at one moment and again in let's say 2 seconds. So we have:<br />
<br />
Now:<br />
<br />
current_user<br />
current_system<br />
current_idle<br />
<br />
in 2 seconds (current values become the previous ones):<br />
<br />
prev_user = current_user<br />
prev_system = current_system<br />
prev_idle = current_idle<br />
<br />
Now we can calculate the cpu usage with this formula:<br />
<br />
current_total = current_user + current_system + current_idle<br />
prev_total = prev_user + prev_system + prev_idle<br />
<br />
diff_total = current_total - prev_total<br />
diff_idle = current_idle - prev_idle<br />
<br />
cpu_usage (%) = (diff_total - diff_idle)/diff_total*100<br />
<br />
Now we can write the shell script to calculate every 2 seconds: <br />
<br />
<i>#!/bin/sh<br />p_user=0<br />p_system=0<br />p_idle=0<br />p_total=0<br /><br />while true; do</i><br />
<i><br />#current</i><br />
<i><br />l=$(cat /proc/stat | grep "cpu ")<br />c_user=$(echo $l | cut -f2 -d' ')<br />c_nice=$(echo $l | cut -f3 -d' ')<br />c_system=$(echo $l | cut -f4 -d' ')<br />c_idle=$(echo $l | cut -f5 -d' ')<br />#calc<br />c_total=$(($c_idle+$c_user+$c_system))<br />d_total=$(($c_total-$p_total))<br />d_idle=$(($p_idle-$p_idle))<br />totalminusidle=$(($d_total-$d_idle))<br />#cpu=$((($totalminusidle/$d_total)*100))<br />echo -n $totalminusidle<br />echo -n "#"<br />echo -n $d_total<br />echo<br />sleep 2</i><br />
<i><br />#prev</i><br />
<i><br />p_user=$c_user<br />p_nice=$c_nice<br />Psystem=$c_system<br />p_idle=$c_idle<br />p_total=$c_total<br /><br />done</i><br />
<br />
We save it in writable /tmp space as top.sh<i>. </i>It will be there until device reboot.<br />
<br />
It's not the perfect script. I can't use bash feaures here as I don't have one. We have a little problem here. The cpu usage will be totally inaccurate because the shell doesn't support floating point arithmetic. And on this machine I don't have any tools to do that (like bc, dc). There is awk but it always returns nan, trying to do some math operations:<br />
<br />
<i>/ # awk 'BEGIN{print 2+2}'<br />nan</i><br />
<br />
So the only not ellegant idea I came up with was to list the values used in the final CPU equation and then do the calculations somewhere else, in spreadsheet for example :). (yes it's not realtime). So when we run the script we got: (we omit the first row as we didn't have the prev values yet):<br />
<br />
<br />
/tmp # sh ./top.sh<br />
<br />
31401#2110151<br />
33#232<br />
34#233<br />
29#228<br />
31#232<br />
34#233<br />
33#232<br />
32#231<br />
31#232<br />
33#232<br />
33#232<br />
32#231<br />
29#230<br />
<br />
Feeding spreadsheet with them we have:<br />
<br />
a b a/b*100<br />
<table border="0" cellspacing="0" cols="4" frame="VOID" rules="NONE">
<colgroup><col width="86"></col><col width="86"></col><col width="86"></col><col width="86"></col></colgroup>
<tbody>
<tr>
<td align="RIGHT" height="17" width="86"><span style="font-family: "times new roman";">33</span></td>
<td align="RIGHT" width="86">232</td>
<td align="LEFT" width="86"><span style="font-family: "times new roman";"><br /></span></td>
<td align="RIGHT" width="86">14,224137931</td>
</tr>
<tr>
<td align="RIGHT" height="17"><span style="font-family: "times new roman";">34</span></td>
<td align="RIGHT">233</td>
<td align="LEFT"><span style="font-family: "times new roman";"><br /></span></td>
<td align="RIGHT">14,5922746781</td>
</tr>
<tr>
<td align="RIGHT" height="17"><span style="font-family: "times new roman";">29</span></td>
<td align="RIGHT">228</td>
<td align="LEFT"><span style="font-family: "times new roman";"><br /></span></td>
<td align="RIGHT">12,7192982456</td>
</tr>
<tr>
<td align="RIGHT" height="17"><span style="font-family: "times new roman";">31</span></td>
<td align="RIGHT">232</td>
<td align="LEFT"><span style="font-family: "times new roman";"><br /></span></td>
<td align="RIGHT">13,3620689655</td>
</tr>
<tr>
<td align="RIGHT" height="17"><span style="font-family: "times new roman";">34</span></td>
<td align="RIGHT">233</td>
<td align="LEFT"><span style="font-family: "times new roman";"><br /></span></td>
<td align="RIGHT">14,5922746781</td>
</tr>
<tr>
<td align="RIGHT" height="17"><span style="font-family: "times new roman";">33</span></td>
<td align="RIGHT">232</td>
<td align="LEFT"><span style="font-family: "times new roman";"><br /></span></td>
<td align="RIGHT">14,224137931</td>
</tr>
<tr>
<td align="RIGHT" height="17"><span style="font-family: "times new roman";">32</span></td>
<td align="RIGHT">231</td>
<td align="LEFT"><span style="font-family: "times new roman";"><br /></span></td>
<td align="RIGHT">13,8528138528</td>
</tr>
<tr>
<td align="RIGHT" height="17"><span style="font-family: "times new roman";">31</span></td>
<td align="RIGHT">232</td>
<td align="LEFT"><span style="font-family: "times new roman";"><br /></span></td>
<td align="RIGHT">13,3620689655</td>
</tr>
<tr>
<td align="RIGHT" height="17"><span style="font-family: "times new roman";">33</span></td>
<td align="RIGHT">232</td>
<td align="LEFT"><span style="font-family: "times new roman";"><br /></span></td>
<td align="RIGHT">14,224137931</td>
</tr>
<tr>
<td align="RIGHT" height="17"><span style="font-family: "times new roman";">33</span></td>
<td align="RIGHT">232</td>
<td align="LEFT"><br /></td>
<td align="RIGHT">14,224137931</td>
</tr>
<tr>
<td align="RIGHT" height="17">32</td>
<td align="RIGHT">231</td>
<td align="LEFT"><br /></td>
<td align="RIGHT">13,8528138528</td>
</tr>
<tr>
<td align="RIGHT" height="17">29</td>
<td align="RIGHT">230</td>
<td align="LEFT"><br /></td>
<td align="RIGHT">12,6086956522</td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td><td align="RIGHT"></td>
</tr>
</tbody>
</table>
<br />
When the player is just turned on and idle the cpu usage is about 12-14%<br />
While playing mkv hd movie (720p) the cpu usage is about 20%<br />
While playing mkv full hd movie (1080p) the cpu usage is about 25%<br />
<br />
That's all for now berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0tag:blogger.com,1999:blog-150184498764949481.post-13334499776149280032015-02-18T16:13:00.005-08:002017-09-24T11:12:43.391-07:00Bc as CPU benchmark for multicore cpuReferring to this <a href="http://tuxshell.blogspot.com/2009/08/bc-as-cpu-benchmark.html">http://tuxshell.blogspot.com/2009/08/bc-as-cpu-benchmark.html</a> post, I figured out the simple way how to test speed of multicore cpu system or multi cpu system. This command:<br />
<br />
<b>$ time echo "scale=5000; a(1)*4" | bc -l</b><br />
<br />
will only be executed on one CPU core. However we can use GNU <i>parallel </i>command to execute it many times on multicore cpu. First we create benchmark script b.sh as follows:<br />
<span style="font-style: italic;"><br /></span>
<b>#!/bin/bash<br />echo "scale=$1; a(1)*4" | bc -l</b><br />
<br />
The first argument is number of digits to calculate. Now we can run in parallel (4 times) this way<br />
<b><br /></b>
<b>$ time parallel ./b.sh ::: 2000 2000 2000 2000</b><br />
<br />
We can determine the number of cores with this command:<br />
<span style="font-style: italic;"><br /></span>
<b>$ cat /proc/cpuinfo | grep processor | wc -l</b><br />
<b>4</b><br />
<span style="font-style: italic;"><br /></span>
If your cpu supports hyper threading - this will give you the number of logical cores, not only the physical ones<span style="font-style: italic;"><br /></span><br />
<span style="font-style: italic;"></span><br />
<span style="font-style: italic;"></span><br />
<span style="font-style: italic;"></span><br />
<span style="font-style: italic;"></span>berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com6tag:blogger.com,1999:blog-150184498764949481.post-4606951910806037672015-02-18T15:38:00.001-08:002017-09-24T11:12:43.434-07:00Convert string into hex - iwconfig wifi keyI've played a bit with my wifi command line configuration. The iwconfig command needs the key represented as hexadecimal string, so here is the formula to convert string into hex. The <i>od </i>command does the work and <i>tr </i>command removes spaces:<br />
<br />
<b>$ echo -n "mykey" | od -A n -t x1 | tr -d ' '</b><br />
<b>6d796b6579</b><br />
<br />
Note: we need to escape special shell symbols<br />
<br />
<b>$ echo -n "mykey\$\$" | od -A n -t x1 | tr -d ' '</b><br />
<b>6d796b65792424</b><br />
<br />
As I found out later the string can be given as parameter to the iwconfig key :). <br />
<br />
<br />
<br />berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0tag:blogger.com,1999:blog-150184498764949481.post-36018238715258555692012-02-28T01:59:00.004-08:002017-09-24T11:12:43.411-07:00Recently edited fileVery often I've got the situation when I edit some files remotly in the directory where there are many files. To see recently edited files I'm doing:<br />
<br />
ls -ltr <br />
<br />
They appear as the last ones on the list. The second trick is:<br />
<br />
ls -lt | head<br />
<br />
Then they appear as firstberdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0tag:blogger.com,1999:blog-150184498764949481.post-57851288196664607892010-03-29T22:24:00.000-07:002017-09-24T11:12:43.451-07:00Upper case, lower caseTo change the letters in the string to upper or lower case we can use tr command:<br /><br />$ echo "Hello world" | tr a-z A-Z<br />HELLO WORLD<br /><br />$ echo "Hello world" | tr A-Z a-z<br />hello worldberdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com1tag:blogger.com,1999:blog-150184498764949481.post-90710509153070399932009-08-25T08:04:00.000-07:002017-09-24T11:12:43.485-07:00Generating strong passwordsSometimes we need to make up the password. Normally the password should be easy to remember but hard to guess. I show you how to generate strong passwords that are not easy to remember :). We'll use for this purpose <span style="font-style: italic;">/dev/urandom</span> device, which generates random data. Suppose we want to generate 10 passwords, each 8 characters long. To achieve this we type:<br /><br /><span style="font-style: italic;">$ cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -8 | head -10</span><br /><span style="font-style: italic;">lUrBzyuv</span><br /><span style="font-style: italic;">RuDq498i</span><br /><span style="font-style: italic;">mOzJMs4Y</span><br /><span style="font-style: italic;">s0lUpMZd</span><br /><span style="font-style: italic;">LmwuN6Sy</span><br /><span style="font-style: italic;">fubIOO6g</span><br /><span style="font-style: italic;">KKynsPs4</span><br /><span style="font-style: italic;">dFH4W0Ux</span><br /><span style="font-style: italic;">3JAbwJQD</span><br /><span style="font-style: italic;">PofhRhTS<br /><br /></span>The fold command breaks each line after n characters, so defines the length of the password, the head command shows first n lines so defines the number of passwords. The tr deletes (-d) all but specified (-c) characters. The idea comes from <a href="http://blog.colovirt.com/2009/01/07/linux-generating-strong-passwords-using-randomurandom/"><span style="text-decoration: underline;">here</span></a>.berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0tag:blogger.com,1999:blog-150184498764949481.post-4996186436573352452009-08-23T22:42:00.000-07:002017-09-24T11:12:43.438-07:00Where to put beer?Our desk is full of papers. Where to put the cold beer? Type eject and eject -t after drinking :).berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0tag:blogger.com,1999:blog-150184498764949481.post-80843880267740197272009-08-22T09:02:00.000-07:002017-09-24T11:12:43.469-07:00Shrinking filesWhat can be done to make the executable files smaller? When we get the normal ELF file we can first strip it and then compress it in a way it will decompress during each run. We've got the binary file hello which prints hello world:<br /><br /><span style="font-style: italic;">$ ls -l hello</span><br /><span style="font-style: italic;">-rwxr-xr-x 1 berdzi users 6230 2009-08-22 18:04 hello</span><br /><br />It's 6230 bytes big. We will strip it with strip command. Strip removes all debugging symbols from the binary. They are very helpful when we want to debug the file with i.e. gdb, whereas they not necessary in normal use. Stripping them saves a lot of space:<br /><br /><span style="font-style: italic;">$ strip hello</span><br /><span style="font-style: italic;">$ ls -l hello</span><br /><span style="font-style: italic;">-rwxr-xr-x 1 berdzi users 2748 2009-08-22 18:09 hello<br /><br /></span>Now the file has only 2748 bytes. To make it smaller we can use gzexe. It will compress the file in place - it will be decompressed while each execution. This saves extra bytes increasing runtime. Let's see:<br /><span style="font-style: italic;"><br />$ gzexe ./hello<br />./hello: 53.9%<br />$ ls -l hello<br />-rwxr-xr-x 1 berdzi users 2092 2009-08-22 18:12 hello<br /><br /></span>This will create two files: hello and hello~ where the second one is the original file and if we sure the compressed version is ok, we can remove the source. We can always decompress the compressed file with -d switch.<span style="font-weight: bold;"><br /></span>Let's compare the execution time of compressed file and the original:<span style="font-weight: bold;"><br /><br /></span><span style="font-style: italic;">$ time ./hello~</span><br /><span style="font-style: italic;">Hello world</span><br /><br /><span style="font-style: italic;">real 0m0.004s</span><br /><span style="font-style: italic;">user 0m0.000s</span><br /><span style="font-style: italic;">sys 0m0.004s</span><br /><br /><span style="font-style: italic;">$ time ./hello</span><br /><span style="font-style: italic;">Hello world</span><br /><br /><span style="font-style: italic;">real 0m0.066s</span><br /><span style="font-style: italic;">user 0m0.016s</span><br /><span style="font-style: italic;">sys 0m0.040s</span><br /><span style="font-style: italic;"><br /></span>This particular binary executes 16 times slower (in this case in almost unnoticable).berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com1tag:blogger.com,1999:blog-150184498764949481.post-44236301363300043822009-08-22T07:51:00.000-07:002017-09-24T11:12:43.427-07:00The linesIf you want to check how many lines the file has, use <span style="font-style: italic;">wc -l</span> command:<br /><br /><span style="font-style: italic;">$ cat /etc/shells | wc -l</span><br /><span style="font-style: italic;">6<br /><br /></span>To see the content of the file along with line numbers use <span style="font-style: italic;">cat -n:<br /><br />$ cat -n /etc/shells<br /> 1 /bin/bash<br /> 2 /bin/tcsh<br /> 3 /bin/csh<br /> 4 /bin/ash<br /> 5 /bin/ksh<br /> 6 /bin/zsh<br /><br /></span>To get the particular line from the file use head and tail. To see the 4th line from /etc/shells, type:<br /><br /><span style="font-style: italic;">$ cat /etc/shells | head -4 | tail -1<br />/bin/ash</span>berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0tag:blogger.com,1999:blog-150184498764949481.post-19766648696327058072009-08-19T15:01:00.000-07:002017-09-24T11:12:43.404-07:00(S)hell fire :)<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVvUKDoe-zgu3pq-mdD6jb9rE4t7CTHYzsu8IzjerXUOmcl6VYgdB5_Y6lICkRZjv_UUdToH_SFl6TTTbaV4DRHNT3Bz_7qo8HjVv5CWWWoZSsNRII9isE7Bu6Za5glref8nbgLXs3sJqr/s1600-h/fire.jpg"><img style="cursor: pointer; width: 400px; height: 260px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVvUKDoe-zgu3pq-mdD6jb9rE4t7CTHYzsu8IzjerXUOmcl6VYgdB5_Y6lICkRZjv_UUdToH_SFl6TTTbaV4DRHNT3Bz_7qo8HjVv5CWWWoZSsNRII9isE7Bu6Za5glref8nbgLXs3sJqr/s400/fire.jpg" alt="" id="BLOGGER_PHOTO_ID_5371798795626623010" border="0" /></a><br /><br /><br />Although there's summer, you may sometimes feel cold. What can you do to warm up? Run <span style="font-style: italic;">aafire</span> and admire beautiful ASCII art fire in the console.berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0tag:blogger.com,1999:blog-150184498764949481.post-37820851696954958312009-08-19T09:52:00.000-07:002017-09-24T11:12:43.415-07:00File integrityWhat happens if someone hack into our system and modify the binaries. At first they look the same and behave normally but underneath they can do some bad things. The simple way to control things is to create md5 hashes of every binary after system installation and keep it safe somewhere on CD or other external drive. Let's do checksums file (as root):<br /><br /><span style="font-style: italic;"># find / -type f -perm /111 -print | xargs md5sum > checksums<br /><br /></span>We find all executable files and then create md5 hashes for them in the checksums file. Now if we're suspect any problems we can always compare the hashes in the system with the hashes in the checksums file, manually:<br /><br /><span style="font-style: italic;"># md5sum /bin/ls</span><br /><span style="font-style: italic;">a7d0f168866236756bafed5357e7e039 /bin/ls</span><br /><span style="font-style: italic;"># grep /bin/ls checksums</span><br /><span style="font-style: italic;">a7d0f168866236756bafed5357e7e039 /bin/ls<br /><br /></span>or with this script:<span style="font-style: italic;"><br /><br />#!/bin/bash<br />if [ "$(grep "${1}" checksums| cut -f1 -d' ')" == "$(md5sum "${1}" | cut -f1 -d ' ')" ]; then echo "OK<br />"; else echo "FAILED"; fi<br /><br /></span>Run this way:<span style="font-style: italic;"><br /><br />$ ./sumcmp.sh /bin/ls<br />OK</span><span style="font-style: italic;"><br /></span>berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0tag:blogger.com,1999:blog-150184498764949481.post-41325235403089772072009-08-17T17:10:00.000-07:002017-09-24T11:12:43.419-07:00Searching wikipedia with dig (or host)Dig is the tool to get information from DNS. As I read <a href="http://lifehacker.com/5329014/search-wikipedia-from-the-command-line">here</a>, suprisingly, it can also be used to query wikipedia (english) from the command line. The syntax is:<br /><br />dig +short txt <keyword><keyword>.wp.dg.cx<br /><br />Here's the query for 'linux' term:<br /><br /><span style="font-style: italic;">$ dig +short txt linux.wp.dg.cx</span><br /><span style="font-style: italic;">"Linux (commonly pronounced in English\; variants exist) is a generic term referring to Unix-like computer operating systems based on the Linux kernel. Their development is one of the most prominent examples of free and open source software collaboration\; t" "ypically all the underlying source code can be used, freely modified, and redistributed by anyone under the terms of the... http://a.vu/w:Linux"</span><br /><br />We can simplify things, writing small script wiki.sh:<br /><br /><span style="font-style: italic;">#!/bin/bash</span><br /><span style="font-style: italic;">dig +short txt "${1}.wp.dg.cx"<br /><br /></span>We can make the output prettier with this script (found <a href="http://www.commandlinefu.com/commands/view/2829/query-wikipedia-via-console-over-dns#comment">here</a>):<span style="font-style: italic;"><br /><br />#!/bin/sh<br />COLUMNS=`tput cols`<br />dig +short txt "${1}".wp.dg.cx | sed -e 's/" "//g' -e 's/^"//g' -e 's/"$//g' -e 's/ http:/\n\nhttp:/' | fmt -w $COLUMNS<br /><br /></span><span>We can also use host command to do the same:</span><span style="font-style: italic;"><br /><br />$ host -t txt linux.wp.dg.cx<br /></span></keyword>berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com1tag:blogger.com,1999:blog-150184498764949481.post-87825548322796549872009-08-16T14:59:00.000-07:002017-09-24T11:12:43.481-07:00Mass files renamingEver needed to change the extensions of the multiply files from one to the other? If you i.e. want to change all the files ended by .php3 to .php you could use the following command:<br /><br /><span style="font-style: italic;">$ ls -d *.php3 | sed 's/\(.*\).php3$/mv "&" "\1.php"/' | sh</span><br /><br />What we use here is sed replacing the part of the string (thanks to regular expression) with mv command and piping it to sh to which executes it. The detailed explanation of this process can be found <a href="http://6v8.gamboni.org/Mass-renaming-with-linux-shell.html">here</a>.<br /><code class="spip_code" dir="ltr" style="font-family:times new roman;"><span style="font-family:verdana;"><span style=";font-family:verdana;font-size:100%;" ><a href="http://6v8.gamboni.org/Mass-renaming-with-linux-shell.html"></a></span><br /></span></code>berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0tag:blogger.com,1999:blog-150184498764949481.post-67149403402763583622009-08-15T07:19:00.000-07:002017-09-24T11:12:43.465-07:00Detect and monitor CPU cores<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiBQVMm6_QSlGKW62V9fTD3W8bat3iH_wQ25edUYWuAW7s10GvSs9mKEVxvXcMPYbnhUBxySM03EmGY4YEnZHpG8XKqncAl5zAmIqPjLnJZIwRjitT71Pb1BG_hCzUcVh0lviWpl_WcPaD/s1600-h/top.jpg"><img style="cursor: pointer; width: 400px; height: 67px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiBQVMm6_QSlGKW62V9fTD3W8bat3iH_wQ25edUYWuAW7s10GvSs9mKEVxvXcMPYbnhUBxySM03EmGY4YEnZHpG8XKqncAl5zAmIqPjLnJZIwRjitT71Pb1BG_hCzUcVh0lviWpl_WcPaD/s400/top.jpg" alt="" id="BLOGGER_PHOTO_ID_5370197523954909810" border="0" /></a><br /><br /><br />If you have several CPUs on your system or multicore CPU, you can detect number of cores with this simple command:<br /><br /><span style="font-style: italic;">$ cat /proc/cpuinfo | grep processor | wc -l</span><br /><span style="font-style: italic;">2</span><br /><br />To determine the usage of each core you can use standard <span style="font-style: italic;">top</span> command. By default it shows only summary usage, however you can toggle between summary and per core usage with the '1' key after starting the top.berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com1tag:blogger.com,1999:blog-150184498764949481.post-60328298508822165302009-08-13T01:30:00.000-07:002017-09-24T11:12:43.460-07:00Bc as cpu benchmarkMy previous post (<a href="http://tuxshell.blogspot.com/2009/08/shell-as-calculator.html">http://tuxshell.blogspot.com/2009/08/shell-as-calculator.html</a>) was about making math calculation in shell. I showed how to calculate PI number up to n digits. I thought, together with time command it can be used as CPU benchmark. This kind of calculation is cpu intensive and takes some time. Let's calculate up to 5000 digits after period. We measure the time of doing it with time command and look for real value:<br /><br /><span style="font-style: italic;">$ time echo "scale=5000; a(1)*4" | bc -l</span><br /><br />My computer did it within 37.194s. How about yours?berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com81tag:blogger.com,1999:blog-150184498764949481.post-16180310410730430022009-08-13T00:57:00.000-07:002017-09-24T11:12:43.396-07:00Shell as calculatorThe standard Linux shell (bash) can serve us as calculator. The bash itself is only limited to integer numbers, however there is also <span style="font-style: italic;">bc</span> command. Bc stands for binary calculator and allows to do numbers of calculations using floating point math as well as conversions. First let's look at standard shell possibilites:<br /><br /><span style="font-style: italic;">$ echo $((2+2)</span><br /><span style="font-style: italic;">4</span><br /><br /><span style="font-style: italic;">$ echo $((2+2*(3*4)))</span><br /><span style="font-style: italic;">26</span><br /><br />modulo operator<br /><br /><span style="font-style: italic;">$ echo $(10 % 3)</span><br /><span style="font-style: italic;">3</span><br /><br />power operator<br /><br /><span style="font-style: italic;">$ echo $((2**3))</span><br /><span style="font-style: italic;">8</span><br /><br />bitwise operators (bitshifting):<br /><br /><span style="font-style: italic;">$ echo $((2<<3))</span><br /><span style="font-style: italic;">16</span><br /><br /><span style="font-style: italic;">$ echo $((10>>1))</span><br /><span style="font-style: italic;">5</span><br /><br />There is one trick as to bitshifts. Shifting the number by 1 bit left means multiplying by 2, shifting it by 1 bit right means dividing by 2. Bitshifting is faster than multiplying but it has the meaning in assembly language programs, not the linux shell :).<br />Of course we can assign the result to variabe to use it later:<br /><br /><span style="font-style: italic;">$ VALUE=$((100/4))</span><br /><span style="font-style: italic;">$ echo $VALUE</span><br /><span style="font-style: italic;">25</span><br /><br />Bash is good only for integer calculations. Bc is much more powerfull tool. To calculate anything with bc we usually pipe the output of echo<br />to bc:<br /><br /><span style="font-style: italic;">$ echo 2+2 | bc</span><br /><span style="font-style: italic;">4</span><br /><br /><span style="font-style: italic;">echo 2.56*2.66 | bc</span><br /><span style="font-style: italic;">6.80</span><br /><br />We can set the precision with scale parameter:<br /><br /><span style="font-style: italic;">$ echo "scale=4; 2.56*2.66" |bc</span><br /><span style="font-style: italic;">6.8096</span><br /><br />To calculate the power we use ^ (in bash it means bitwise negation):<br /><br /><span style="font-style: italic;">$ echo 2^10 | bc</span><br /><span style="font-style: italic;">1024</span><br /><br /><span style="font-weight: bold;">Bc as base converter :)</span><br /><br />Apart from simple calculation bc can be used as base converter. To convert among decimal, hex and binary numbers we use ibase (input base) and obase (output base) parameters. The default base is 10. Let's see some examples:<br /><br /><span style="font-style: italic;">$ echo "obase=16; 255" | bc</span><br /><span style="font-style: italic;">FF</span><br /><br /><span style="font-style: italic;">$ echo "obase=2; 255" | bc</span><br /><span style="font-style: italic;">11111111</span><br /><br />However to convert from binary to decimal we have to set obase as hex number (10 is A). The reason for that is 10 in binary is 2, so the obase would be 2, becase ibase is set to 2 and interprets decimal 10 decimal as binary 2:<br /><br /><span style="font-style: italic;">$ echo "ibase=2; obase=A; 1000" | bc</span><br /><span style="font-style: italic;">8</span><br /><br />When the switch -l is used the bc is preloaded with math library and default scale is set to 20. This allows to do some more advanced calculations such trigonomery math. The argument is given as radians. To calculate sine of 30 radians type:<br /><br /><span style="font-style: italic;">$ echo "s(30) | bc -l"</span><br /><br />We can calculate PI very easy up to many digits after period (set with scale):<br /><br /><span style="font-style: italic;">$ PI=$(echo "scale=10; 4*a(1)" | bc -l)</span><br /><span style="font-style: italic;">$ echo $PI</span><br /><span style="font-style: italic;">3.1415926532</span><br /><br />Now we can convert radians to degrees with the formula:<br /><br />degrees=radians*PI/180<br /><br />Shell calculator<br /><br />It's worth to write short script which can be used as calculator.<br /><br /><span style="font-style: italic;">#!/bin/bash</span><br /><span style="font-style: italic;">echo "scale=4; $1" | bc</span><br /><br />Save it as calc and give execution rights:<br /><br /><span style="font-style: italic;">$ chmod u+x calc</span><br /><br />From the root shell copy it somewhere i.e. to /usr/bin to make it visible system-wide.<br /><br /><span style="font-style: italic;">$ su -c "cp calc /usr/bin/"<br /><br /></span>Use it this way:<span style="font-style: italic;"><br /><br />$ calc 6.5*6.5<br />42.25<br /><br /><br /></span>berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com2tag:blogger.com,1999:blog-150184498764949481.post-16259428738468007812009-08-12T05:46:00.000-07:002009-08-13T13:35:00.845-07:00Tee timeHave you ever needed to write something to file and watch it on the screen at the same time? If so, tee command is your friend. You can pipe anything to tee and it will show it on the screen as well as write to the file (or even better - multiple files) at the same time. The useful switch -a means append mode - the data are added at the end of file instead of erasing it. Let's see and write current time and date to three files - date1, date2 and date3:<br /><br /><span style="font-style: italic;">$ date | tee -a date1 date2 date3<br /></span>berdzihttp://www.blogger.com/profile/14528334833883104699noreply@blogger.com0