Showing posts with label command line. Show all posts
Showing posts with label command line. Show all posts

2021/08/26

extracting PHP errors & warnings from apache log

slackware:

cd /var/log/httpd 

or  debian

cd  /var/log/apache2

clear ; cat error.log | grep PHP | grep -P  '(.*])\K.*' -o |sort -u



2021/01/05

Script to prevent SSH attacks while allowing you to connect from dynamic IP address.

while most of the time connecting to a remote server using openvpn would be a smart choice, sometimes it's not an option. This script is for a linux behind a firewall/router. It's intended to prevent SSH attacks (by simply denying access to the SSH port) while allowing you to connect from your own dynamic DNS, that is, if you have a dynamic ip given by your ISP, otherwise you could simply use iptables.

#!/bin/bash
valor=`host yourdyndns`
OK=$?
if [ $OK -ne 0 ]; then
  exit
fi
addr=$(echo $valor |egrep "address .*" -o)
ip=$(echo $parte |egrep "[0-9\.]*" -o)

whitelist=/root/whitelist
touch ${whitelist}

exists=`cat ${whitelist} | grep "${parte2}"`

if [ $? -ne 0 ]; then
  echo $ip >$whitelist
  iptables -F   iptables -A INPUT -s $ip -j ACCEPT
  iptables -A FORWARD -s $ip -j \ ACCEPT                                                                                         
  iptables -A INPUT -p tcp --dport 22022 -j DROP                                                                                   
fi

add this to rc.local to reload iptables during bootup:

# delete whitelist

rm /root/whitelist

# run previous script

/root/bin/testso.sh


2021/01/03

x2x on Slackware

 I had to write something. This was kinda shocking to me. I was looking for a KVM (Keyboard, Video and Mouse) software to be able to control my notebook from my PC.  Been using an old free version of Synergy, but for some odd reason seems to be compiled for 32 bits and I felt it was a terrible waste to install multilib just to run it. So I decided to look for an alternative.

As usual, went to google and discovered that Slackbuilds has Barrier but when I tried to install it, it asked for avahi, and avahi asked for something else and that something else asked for other-something-else. So I gave up. Then I came across x2x.

This particular software has a wikipedia page so I wont bother telling the story about it.

    cd

    mkdir -p install/util/x2x

    cd install/util/x2x

    wget https://slackbuilds.org/slackbuilds/14.2/desktop/x2x.tar.gz

    tar xf x2x.tar.gz 

    cd x2x

    wget https://slackware.uk/~urchlay/src/x2x-1.30_beta+20200121_ec10215.tar.xz

    su

    ./x2x.SlackBuild

    installpkg /tmp/x2x-1.30_beta+20200121_ec10215-x86_64-1_SBo.tgz

then I have to create an script to connect to my notebook, but before I get into that I had to read the man page, last section, after the examples, gives an important tip when you have multiple monitors 

Left: 1920*1080 (primary)

Right: 1366*768

So I had to do the math and write the parameters to x2x according to the man page:

#!/bin/bash
ssh -YC rudy@macabra x2x -north -to :0.0 -big  -completeregionleft 0 -completeregionup 0 -completeregionright 3286 -completeregionlow 768

I have ssh configured so it wont ask me for password. North parameter means I placed my notebook "over" my monitors and it doesn't really matter from which monitor my mouse goes up, it appears on my notebook and it's able to travel  the entire notebook screen.

What shocked me about this is the fact it's a very old proyect, but works like a charm.


 

    

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/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.

2019/11/19

Fix for VLC 3.X video freeze during movies

A few days ago I bough new monitor, a Philco 43" FHD. Since then, VLC started to freeze during playback on H264 MKV files.

I've been searching the web for the problem mentioned in title of this post. I have tried official solution which involves setting a higher value for file and video cache among other things. I have even upgraded it to the lastest version: 3.1.8.

But the problem persisted.

So I ended up doing the most basic thing to do when an specific software doesn't work: downgrading to a working version. Uninstalled version 3 and installed version 2.0.7 and I was able to play all H264 videos with no freeze.

Be warned thou, if you want to play H265 you will need either another player  or converting the files to H264. For converting the file, I made an script with generates an h264 dir in the current directory and then output the converted files there:

rudy@madcat3:~/bin$ cat convertir.h265.sh
#!/bin/bash
mkdir 264
for file in *.mkv
        do ffmpeg  -n -i "${file}" -map 0 -c:a aac -c:s copy -c:v libx264 264/"${file}"
done

2018/09/19

Eclipse PDT preventing focus stealing during test running with PHPUnit

you are writting code and at some point you press Ctrl-F11 to run the default test configuration. Thing is: both Console and PHPUnit window will steal the focus from the source editor.
theres  a bug report marked as NEW while its 4 years old and some other fixes for Mac but I ended up with the easiest solution: to do not test under Eclipse and instead use an external console. The script I made in bash is as follows:

#!/bin/bash

clear
inotifywait -m --format %w%f -q -r -e close_write $1 $2 | \
while read CUAL
do
    if [ $? == 0 ]; then
        clear
        phpunit.phar --color  $1
   fi
done

first parameter is the location of the dir containing the tests, or the test Suite. Second parameter is the directory from where inotifywait should watch for changes on filesystem.
  


2018/08/29

PHP to UML

I wanted a tool to convert PHP classes to UML, but I also wanted to be able to modify the result.

As a (maybe positive) side effect I had to separate all PHP classes in a particular directory in order to avoid the tool to get confused with other scripts used to generate Javascript code among other things.


(this post may require some revisions)

grab php2xmi from

http://tech.motion-twin.com/php_php2xmi.html

sample script to run on project directory:


php2xmi \
--no-private \
--no-protected \
--output=/home/rudy/borrame/myresult.xmi \
`find src/class -name "*.php"`

add --path= if you need to include other project files

once you get the XMI file, you can import it with ArgoUML

http://argouml-downloads.tigris.org/argouml-0.34/

it wont generate the UML on the fly


2018/08/02

network bonding on slackware 14.2

perhaps a first sloppy attempt I made to make bonding work in 14.2, based on this info

https://docs.slackware.com/howtos:misc:network_interace_bonding

inside /etc/rc.d/rc.M add (before rc.local section):

if [ -x /etc/rc.d/rc.bond ]; then
  . /etc/rc.d/rc.bond
fi



/etc/rc.d/rc.bond :
modprobe bonding

ip addr add 192.168.111.3/24 dev bond0
ip link set bond0 up


ip link set eth0 master bond0
ip link set eth1 master bond0

ifconfig eth0 up
ifconfig eth1 up

ip route add default via 192.168.111.13 dev bond0
chmod +x rc.bond and reboot

2018/07/23

bash script to kill another script by pid file

the idea is to get the pid from a running script and save it to a file, so we can retrieve the id and kill the process. Also it should kill child processes

to create the file, during the script we must do (replace /home/mantener/var/run with another dir of your liking):

minombre=$(basename $0)
pidfile=/home/mantener/var/run/$minombre.pid
echo $$ >$pidfile

 
 and the script to make use of the pid file could be:

#!/bin/bash
pidfile=/home/mantener/var/run/$1.pid

if [ ! -f $pidfile ] ; then
   echo missing $pidfile !
   logger $0 missing $pidfile !
   exit -1
fi

cualpid=$(cat $pidfile)

# get child processes
resto=$(ps --ppid $cualpid -o pid --no-headers)

echo killing $1
kill -9 $cualpid

while IFS= read -r line ; do
    echo killing $line
    kill -9 $line
done <<< "$resto"

rm $pidfile

2018/07/19

script to kill a specific process by name

This one was made specifically for VLC since the stupid program sometimes hangs and there is no way to close it by normal means (Quit command, etc)

#!/bin/bash
$(echo -n kill -9 ; ps -A |grep vlc |grep -o "^ [0-9]*")
what it does is to create a command line on the fly by extracting vlc process id and then execute it.

2018/06/12

ffmpeg: converting m4a to mp3 script

to be able to do this, ffmpeg must be compiled with mp3 support.


#!/bin/bash
mkdir mp3
for file in *.m4a
   do ffmpeg -i "${file}" -vn -ar 44100 -ac 2 -ab 128k -f mp3 mp3/"${file/%m4a/mp3}"
done

2018/06/06

"unable to un/install on windows" error

a quick list of things to check:
MSIEXEC /UNREGISTER
MSIEXEC /REGSERVER

or

HKEY_LOCAL_MACHINE \Software\Microsoft\Windows\CurrentVersion\Installer\InProgress   

or

HKEY_LOCAL_MACHINE \System\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations


HKEY_LOCAL_MACHINE \Software\Microsoft\Windows\CurrentVersion\Installer

2018/06/05

zeroing a disk on linux using DD

WARNING: if you don't really understand what are you doing (even if you THINK you are), then start learning/reading first or asking someone who really understands linux BEFORE doing something stupid like erasing a wrong disk by mistake.

if you need to wipe the content of a hard disk (REPLACE /dev/sda with the appropiate device !):

dd if=/dev/zero of=/dev/sda bs=1M 

last parameter (block size) makes the process a lot faster

2018/05/26

inotifywait example

edit: modified parameter "-n 1" to the "head" command

for this example I'm assuming text files:
to avoid confusions long lines are splitted with \


#!/bin/bash

if [ $# -ne 2 ]
    then
    echo "$0 <src dir> <dst dir>"
    exit
fi

clear

inotifywait $1 -qmre CLOSE_WRITE \
--format='%f' | while read WHICHONE ; do  

   if [ $? != 0 ]; then exit; fi;
   LINESELECTED=$(head -n 1 $WHICHONE | \
grep CONDITION)

if[ $LINESELECTED -eq 1 ] then
   cp $1/$WHICHONE $2
fi


done

2018/05/04

phpunit script

assuming theres a test directory inside a project where the source files are held:

#!/bin/bash

if [ $# -ne 2 ]
    then
    echo "testphp <test dir> <src dir>"
    echo "example: $ testphp test ."
    exit
fi

clear

inotifywait $2 -qmre CLOSE_WRITE --format='%f' | while read CUAL ; do  
   if [ $? != 0 ]; then exit; fi;
   clear
   phpunit.phar --log-junit /tmp/ultimo.txt --color -v $1
done

2018/04/29

geany plugins for 1.33

assuming:
  • have geany 1.33 installed
  • you want to use geany pluings for the same version
  • you have downloaded and unpacked source distribution 
  • you get an error while running ./configure under slackware64 14.2 similar linux flavor
if configure complains no "geany.pc", to solve this:


find /usr |grep "geany.pc"

take note where the "geany.pc" is located and append to PKG_CONFIG_PATH

ie if the file is located under /usr/local/lib/pkgconfig

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig

then you can run ./configure again without errors

2018/04/24

new install to-do list

this post is a work in progress:

after installing slackware, things to do besides the normal setup like configuring network card:

copy ssh keys from old install
copy /etc/sshd config?
copy .bashrc from old home install or the entire home directory
user groups review? (seems like its no longer needed under Slackware 14.2)
xorgsetup ? only if needed
compat32
libreoffice ?
wine?
firefox 40 (yes I stick to this version until Firefox can fix newer versions problems).


kde: fix nasty konsole prompt, edit Shell: /bin/bash -l


Installing PHP 7 from source

this post assumes you are familiar with Slackware 14.2 and have php 5.6 installed

  1. Donwload php from sources, and extract it
  2. inside the sources, create a script with this code or copy/paste on bash:
    #!/bin/bash
    ./configure \
        --with-config-file-path=/etc \
        --with-config-file-scan-dir=/etc \
        --enable-mbstring \
        --enable-zip \
        --enable-bcmath \
        --enable-pcntl \
        --enable-ftp \
        --enable-exif \
        --enable-calendar \
        --enable-sysvmsg \
        --enable-sysvsem \
        --enable-sysvshm \
        --enable-wddx \
        --enable-intl \
        --with-curl \
        --with-iconv \
        --with-gmp \
        --with-pspell \
        --with-gd \
        --with-jpeg-dir=/usr \
        --with-png-dir=/usr \
        --with-zlib-dir=/usr \
        --with-xpm-dir=/usr \
        --with-freetype-dir=/usr \
        --enable-gd-jis-conv \
        --with-openssl \
        --with-pdo-mysql=/usr \
        --with-gettext=/usr \
        --with-zlib=/usr \
        --with-bz2 \
        --with-apxs2=/usr/bin/apxs \
        --with-mysqli=/usr/bin/mysql_config \
        --with-ldap \
      --with-libdir=lib64
    edit: the long line from above as one line:
    ./configure --with-config-file-path=/etc --with-config-file-scan-dir=/etc --enable-mbstring --enable-zip --enable-bcmath --enable-pcntl --enable-ftp --enable-exif --enable-calendar --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --enable-intl --with-curl --with-iconv --with-gmp --with-pspell --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --with-zlib-dir=/usr --with-xpm-dir=/usr --with-freetype-dir=/usr --enable-gd-jis-conv --with-openssl --with-pdo-mysql=/usr --with-gettext=/usr --with-zlib=/usr --with-bz2 --with-apxs2=/usr/bin/apxs --with-mysqli=/usr/bin/mysql_config --with-ldap --with-libdir=lib64

  3. make
  4. make test
  5. su
  6. make install
  7. edit /etc/httpd/mod_php.ini and comment lib_php5.so line
  8. rc.httpd restart