2020/12/05

Script to reconnect wan via rc.inet1

My router is a Linux PC with 2 lan boards, one for local lan and the other directly connected to the ISP router (configured as bridge). Each time my ISP disconnects me for whatever reason I had to either reboot the computer or keep running rc.inet1 restart until the connection is made.

So, even if it doesn't happen often, at one point I got bored of it and decided to write an script to force the reconnection as soon as possible:

 #!/bin/bash
while true ; do
   clear
   ./rc.inet1 restart
   ping google.com -c 1
   if [ $? == 0 ]; then
      exit
   fi;
   sleep 30
done

 

2020/06/28

Linux: [Draft] how files should be organizaded inside $home

as the title says, this is a draft made public.

what I intend to do here is to organize my thoughts on how scripts and files should be placed inside $HOME user directory, were $HOME refers to the proper /home/yourusername directory inside the Linux filesystem, so I will simply refer  to it as "~" . And by user I mean "normal non-root user", But some of these ideas can be used as root too, specially if you run scripts as root under cron and you don' t want other to take a look at them.

At first I came up with the idea of naming each script with a prefix, like convert.mp42mp3, which is not bad by itself since it makes you easier to remember what the command name should look like or even better, tab key can hint you which is the one you are looking for.

Having all scripts inside a director ~/bin is a good thing, but if if you end up with too many scripts like me, it could be annoying each time you do "ls" inside of it trying to find out what is the script you are looking for. Specially if you don't remember the name of it. PATH variable should also point to it, so each time you want to execute them from another directory you don't have to explicit mention the path of the script you want to invoke. To this end, ~/.bashrc_profile must contain a line like this:

export PATH=$PATH:~/bin:

So I guess ~/bin should contain mostly symbolic links to the proper scripts. Where to place the scripts then? inside directories under ~/bin. Each one of those directories names should represent a category under each script belongs.

Examples of names that comes to mind:

remotes (for SSH access to specific hosts, could be ssh too)
develop (if you are a developer)
cron for scripts that are run by cron. A good example could be one to update date and time using ntpdate, but you can do that inside cron as a one line.
os: scripts that does specific things on the operating system (???)

Some programs don' t need to be installed system-wide: if you plan to keep them updated, you can always decompress them under your home dir. Firefox is a glaring example of that.  What I do is to extract the x64 files under ~/usr/local then create a symlink to ~/usr/local/firefox/firefox. And to avoid any conflict with the system-wide Firefox install, what I do is to remove it.

2020/05/10

PHP code for a permutation algorithm

Since I'm currently writing yet-another-php-code-analyzer, I ended up writing a function or something to allow me to do permutation of the elements of any given array. The goal was to make a call to an anonymous function on each new iteration of the elements. Last time I did this was about 17 years ago using legacy mode, and I barely had the idea how I did it. Unfortunately that code is gone (perhaps still lying around on a CD waiting to be thrown to the trash can), so I had to start from zero.

To make this three days story short, as as TDD wanna-be practitioner, at first I tried to create an object, and as the Transformation Priority Premises states, I started using basic values at first (null, empty array, one element array and so on). But I took several wrong turns (as you can see my repository). Had to read again the TPP blog entry to see what I was doing wrong. On top of that I had a misunderstanding on how the array_splice and slice functions works so I had to write a microtest to make sure I understood them.

So my last try was to kept thing as simple as possible. Created a function to return the permuted values. Tests were up to 0, 1, 2, 3 and 4 elements, once I got it tried one with 10 elements and PHP ran out of memory.

Then I went to the anonymous function version and I got it (doing TDD but using the previous code as guide). Then tried a 10 elements version (which is on the tests but commented out) and php took about 4 seconds to do it on a Core2Duo Mac 5 Notebook (Maverick). For reference, back at the time I did it for the first time, an old Pentium II with 233 mhz clock, using Quick C the same process took about 10 seconds.

Resulting code is here

2020/04/30

bash script to kill a process based on partial name

why you might need this: because sometimes some processes can't simply terminated using the GUI. And after too many years typing ps -A | grep something then kill -9 the id, I decided to write a script to avoid having to type two commands.

Be warned, this will only kill the first process found with the lowest process ID.

it takes only one argument as the partial name to be looked upon the ps -A command.
#!/bin/bash
proc=$(ps -A |grep $1 | head -n 1)
if [ -z "$proc" ] ; then
   echo Error: no processes found with the given partial name!
   exit 1
fi
echo ps -A says:
echo $proc
procid=$( echo "$proc" |grep "[0-9]*" -o |head -n 1)

echo "kill? <Y/N>"
read answer
if [ "$answer" != "${answer#[Yy]}" ] ;then
    echo Yes
    kill -9 $procid
    echo Testing if the process is still alive ...
    isdead=$(ps -A |grep "$proc")
    [ -z "$isdead" ] && echo Process has been terminated! || echo process is still alive !?
else
    echo No!
fi
you can replace the question with the actual command but I don't recommend it.

2020/04/08

Installing multilib on slackware-current

this is not really big a problem (except if you consider the time one may waste)

today I made a fresh install of slackware-current on a notebook. Since I wanted to install wine to run some specific wine programs, I went to google typed "slackware multilib". I have done this process several times over the years. So, just as before, I reached this page http://www.slackware.com/~alien/multilib/ which I use as a cheatsheet each time I want to install multilib.

the problem arises because its my first time using -current and also because I copied/pasted the commands mentioned in the "Enabling multilib" first four points without reading the next section. As a consequence I ended up with a broken installation. At first I thought I have installed the 32 bits version, but later I realized there's also a "current" directory on the ftp site.

two things that may add up to the confusion: the fact that Slackware-current says "Slackware 15" at the lilo screen and the /etc/slackware-version file says "slackware 14.2"

so the four commands should be:


Quote:
lftp -c 'open http://slackware.com/~alien/multilib/ ; mirror -c -e current'
cd current
upgradepkg --reinstall --install-new *.t?z
upgradepkg --install-new slackware64-compat32/*-compat32/*.t?z

2020/04/02

Installing Waterfox addons (the "hard" way)

Apparently the guys at Mozilla have found/left a hole somewhere on Waterfox browsers engine (since its also from Mozilla but modified) to reveal the real user agent to websites.

(Note to self: check where is it and how to counter it)

Specific case is addons.mozilla.org: even if you have an user agent spoofer and you choose whatever Firefox version, it will not let download the addon you want.

So the trick is to have an old copy of firefox installed and use it to open the addon page you want. Then simply copy the link to install it, paste into Waterfox and remove the last part of the url so it ends with the xpi extension.