execute shell command from python script using subprocess module to search for a string in the directory of your choice

elementary script to execute shell commands using your very own python language…
i post this snippet here to demonstrate the power of python and yes an intro into the beautiful language called python and why i recommend learning it….

subprocess module in python helps to execute your favorite shell commands through python scripts…
for more references do look into python docs -> https://docs.python.org/2/library/subprocess.html

diving straight into the code…..
equivalent linux command for the below script is: ls -ltrh some_directory | grep search_term


#################################
#Script to search for a term in unix shell
#equivalent linux command: ls -ltrh some_directory | grep search_term
#author:nightslacker
#################################

#import sys module for taking system arguments from command line
import sys
#import subprocess module to execute unix shell commands from python script
import subprocess
#pass first argument as your directory
directory = sys.argv[1]
#pass second argument as the search string you want
search_term = sys.argv[2]
cmnd1 = subprocess.Popen(["ls", "-ltrh", directory], stdout=subprocess.PIPE)
cmnd2 = subprocess.Popen(["grep", search_term], stdin=cmnd1.stdout, stdout=subpr
ocess.PIPE)
output = cmnd2.communicate()[0]//cmnd2.communicate() returns a tuple containing (data of stdout,data of stderr)

print "*** Running ls -ltrh command on directory", directory, " and search term
:",search_term, "***\n", output

how to execute the above script from command line interface or CLI mode?
python anyname.py /home/nightslacker/Documents pythondocs

on executing the above line, the script checks in the Documents folder for any file with pythondocs in their filename and returns the output on your shell

keep slacking folks!!! cheers!!!

how to chroot from installation media( or my installation disk) when my slackware doesnt boot

so, you get situations where you edit your /etc/lilo.conf file and fail to run /sbin/lilo…an example is upgrading the kernel or changing to a generic kernel etc….in such a case your slackware system fails to reboot correctly….and it prompts because of a failed lilo configuration….

selfless and selfish too because i need it posted to come back again cause it recurs way too often:

upgrading kernel without running lilo for me….

Relax, you are in safe quarters if you reached here…

follow the steps here to get back up and running….

quoting from
http://docs.slackware.com/howtos:slackware_admin:how_to_chroot_from_media

In order to gain access to your system without booting directly to it, it is possible to use an installation media such as Slackware CD1 or the DVD. Once the installation media loads and starts, you can change the media’s root directory into a mounted hard-disk partition and use it as the root directory, thus running commands directly from it and affecting it.

1. so the first step is to mount your root partition….if you dont know your root partition just enter fdisk -l and check for the linux partition in it….

(let me explain for a single partition….If this is not the case, and you are using LVM/EVMS or an encrypted volume you will need to prepare the volumes before you can mount and chroot into them)

2. if its /dev/sda1, type

mount /mount/sda1 /mnt

3.   next, we need to prepare three virtual directories to be used by the environment. Those are /dev, a directory with virtual files that represent hardware devices, /proc, a directory with virtual files that represent processes and /sys which contains the kernel and other system files:

mount -o bind /dev /mnt/dev

mount -o bind /proc /mnt/proc

mount -o bind /sys /mnt/sys

4. now, since the partitions are mounted now we can chroot to it

chroot /mnt /bin/bash

quoting slackdocs again…..making my life easier…..

“The bash prompt that you see here is a bash prompt started on your system. You can now work on this environment naturally. For example editing /etc/lilo.conf and executing /sbin/lilo will happen on your system, not from the installation media.”

thats all there to it….now with the help of your installation disc you can use your underlying system when it doesnt boot up…

5. when you are done using, dont forget to run lilo again….and then there is the reboot or poweroff command which you can execute as per your needs….

*i was a bit panicky when my slackware failed to start…..but slackware it always surprises you with its tools…..and the folks at LQ…they keep your worries out….

keep slacking!!!

update/upgrade slackware distro through slackpkg utility

today, i will give a post on how to keep your slackware distro up to date quickly to the latest patches (bugs, security fixes if any) released for the distro the really quick way with slackpkg utility ( default in the distro if you have installed the full installation)….

1. open up /etc/slackpkg/mirrors with your fav text editor
vi /etc/slackpkg/mirrors

2. uncomment the mirror for your distro version or if you want the bleeding edge uncomment a link for slackware-current (note, this is not the stable releases but cutting edge developer’s build…where you have to tinker with issues and fix them if any./…dont uncommnet for production environments…not recommended thoughstill stable…yes i can assure you on that cos its current thats running for me and i feel at home for now with it too)

3. once done, save changes and close the file or exit vi…

4. now, run these four commands one by one….

i. slackpkg update gpg
ii. skackpkg update
iii. slackpkg install-new
iv. slackpkg upgrade-all

5. you can do system clean up after this to ensure no orphan files are present…

well, for now you should check for files with *.new extensions in your system….they are new configuration files that are from your updates for your applications and libraries….

the new files come into existence if you have made any changes to the old configuration files of those respective libraries….in such a case, slackware lets you have keep those old files while an update gives you the new configuration files in .new format so that you can see the difference between them and apply the changes as you wish….

eg: /etc/wpa_supplicant.conf usually have changes for your wireless connection or wifi you added like your essid and password….you get a wpa_supplicant.conf.new on an update so that you can check the diff and apply them…

i recommend going through each new files to see the changes and then remove old configs….

and now you get a brand new slackware( well, basically means you up to date with your times! )

keep slacking!!!

a distro you can tweak to how you want( and most importantly you are in full control)