Thursday, November 17, 2016

Summing numbers in a column

To sum several values in column from file we can use paste command and bash arithmetic expressions

$ echo $(($(paste -s -d'+' nums)))

I've always used tr to glue the values into one line, but the remaining '+' sign must be removed:

$ echo $(($(cat nums | tr '\n' '+' |sed 's/.$//')))

Or with awk:

$ awk '{s+=$1}; END { print s}' nums




Saturday, July 16, 2016

wget and line delimiters CR LF

If 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:

wget $filename

use

wget $(echo "$filename" | tr -d '\r')

This will do the trick

Sunday, March 6, 2016

Raspberry Pi - booting into console mode

By 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.
Raspbian is different. I did it with raspi-config utility:

$ sudo raspi-config

And then I chose "Boot options"=>"B2 Console Autologin Text console, automatically logged in as 'pi' user"

Sunday, February 21, 2016

InsecureMag - downloading all issues

I bumped into InsecureMag (https://www.helpnetsecurity.com/insecuremag-archive/) - 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:

#!/bin/bash
for i in {1..49}; do
  link="https://www.helpnetsecurity.com/dl/insecure/INSECURE-Mag-$i.pdf"
  wget -c "$link"
done


Have a nice reading :)





Raspberry Pi - Setting Static IP address

For 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.
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:

1. Turn off dhcp client which will always override things

$ sudo update-rc.d dhcpcd disable

You can also do it old school way by removing execute right:

$ sudo chmod a-x /etc/init.d/dhcpcd

2. Add the following lines to /etc/network/interfaces (the bold ones are new)

auto lo
auto eth0

iface lo inet loopback

#iface eth0 inet manuala
iface eth0 inet static
address 192.168.1.100
gateway 192.168.1.254
netmask 255.255.255.0


3. We need /etc/resolv.conf with static dns entry. This file is always rewritten by resolvconf utility, so we need to change it. In /etc/resolvconf.conf we add one line:

name_servers=192.168.1.254

My nameserver is my gateway at the same time. 

4. Restart networking services by:

 $ sudo /etc/init.d/networking restart

Works for me. We can check if everything is ok running:

$ /sbin/ifconfig

Saturday, January 9, 2016

Checking the proxy servers

Sometimes we need some privacy on the web. So I needed good proxy servers. I googled a bit and found good list of them here:

http://proxylist.hidemyass.com/

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 :):

198.169.246.30|80|flagCanada
200.180.32.58|80|flagBrazil
211.144.81.68|18000|flagChina
84.45.246.38|3128|flagUK
95.168.217.24|3128|flagCzechia


There are some environment variables called http_proxy and https_proxy. We can set them system-wide to use proxy like this:

export http_proxy="http://host:port"

or

export http_proxy="http://username:pass@host:port"

if proxy requires autentication.

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


--- cut ---

#!/bin/bash
n=1

for l in $(cat list); do
HOST=$(echo $l | cut -f1 -d '|')
PORT=$(echo $l | cut -f2 -d '|')

export http_proxy="http://${HOST}:${PORT}"
export https_proxy=$http_proxy

wget https://www.eff.org/files/https-everywhere2.jpg -T 10 -O ${n}.jpg

unset http_proxy
unset https_proxy


n=n+1
done;

--- cut ---

-T means timeout
-O means name of the output file

This way I got several files numberd as proxy server position on the list. The 0 size ones are non-working proxies.

-rw-r--r--  1 pi pi     0 Jan  9 17:56 10.jpg
-rw-r--r--  1 pi pi 20216 Jun 12  2013 1.jpg
-rw-r--r--  1 pi pi     0 Jan  9 19:23 2.jpg
-rw-r--r--  1 pi pi 20216 Jun 12  2013 3.jpg
-rw-r--r--  1 pi pi     0 Jan  9 19:23 4.jpg
-rw-r--r--  1 pi pi     0 Jan  9 19:23 5.jpg