Monday, 23 March 2015

Killing process + perl

#!/usr/bin/perl

@s=`ps aux`;

if(! $ARGV[0] )
    {
    print "Pass the arguement \n\n USAGE: pk processname (ex: firefox)\n";
    exit;
    }

foreach $i (@s)
    {
    if(($i=~/$ARGV[0]/i)&&($i!~m/processkill/i))
        {
        print "Killing $ARGV[0] Process\n";
        @processlist=split(" ",$i);
        system("kill -9 $processlist[1]");
        }
    }




scriptname process_name_to_kill (eg firefox )

Sunday, 22 March 2015

Passing stdin and argument for the same variable perl

#!/usr/bin/perl

print "You have not passed arguement , please enter the value\n" if(! "$ARGV[0]");

$s=$ARGV[0]||<STDIN>;

print $s."\n";




Friday, 20 March 2015

Laptop Low battery alert perl , Voice activated

#!/usr/bin/perl
open(FILE,"/proc/acpi/battery/BAT0/state");

while($line=<FILE>)
        {
        if(($line=~/remain/i)&&($line=~/(.*)(\s+)([0-9]{1,9})(.*)/))
                {
                system('espeak -s 100 "Battery is low , please make charge" > /dev/null 2>&1')  if($3<600);

                system('espeak -s 100 "Machine fully drained, Will be down in 2 minutes" > /dev/null 2>&1 ') if($3<100);
                system('espeak -s 100 "Game over Game over Game over" > /dev/null 2>&1 ')  if($3<70);

                system('sudo init 0') if($3<45);

                }
        }
close(FILE);

Thursday, 12 March 2015

FTP upload script

#!/bin/bash
HOST='cis.insdm.net'
USER='bas0sss9_asssm'
PASSWD='asasa2aa02ss42011'

ftp -n -v $HOST << EOT
ascii
user $USER $PASSWD
prompt
put $1
ls -la
bye
EOT


Usage :-  scriptname "file_to_upload"

Sunday, 1 March 2015

Perl Look behind + look ahead

Look behind:

    open(FILE,"passwd");
while($line=<FILE>)
    {
    if($line=~/(?<=lpd\:\/)bin/)
        {
        print $line."\n";
        }
    }
close(FILE);



daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh


 eptuwya@JDQ08R1:~$ perl passwdple
lp:x:7:7:lp:/var/spool/lpd:/bin/sh



Look Ahead:


open(FILE,"passwd");
    while($line=<FILE>)
        {
        if($line=~/bin(?=\/sync)/)
            {
            print $line."\n";
            }
        }
close(FILE);


root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh


eptuwya@JDQ08R1:~$ perl passwdplepost
sync:x:4:65534:sync:/bin:/bin/sync