python script to fetch gmail ids and script to send out mail with attachment


-----------------------------------------------------------------------------------------------------------------
script to fetch gmail ids
------------------------------------------------------------------------------------------------------------------
import imaplib, sys, email

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('gmail@gmail.com', 'password')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.


result, data = mail.search(None, '(HEADER From "info@monsterindia.com")')

for num in reversed(data[0].split()):
    rv, data = mail.fetch(num, '(RFC822)')
    raw_email = data[0][1] # here's the body, which is raw text of the whole email
# including headers and alternate payloads

    email_message = email.message_from_string(raw_email)
    if email_message['Reply-To'] in ('naukrialerts@naukri.com', None):
        continue
    print email_message['Reply-To']

-----------------------------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------------------------------
script to send out mail with attachment 
----------------------------------------------------------------------------------------------------------------
#!/usr/bin/python

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os,re

gmail_user = "some@gmail.com"
gmail_pwd = "passowrd"

def mail(to, subject, attach, recruitername ):
   msg = MIMEMultipart()

   msg['From'] = gmail_user
   msg['To'] = to
   msg['Subject'] = subject
   text = """
Hi  %s

balah 
balah 
balah 

balah 

Regards 

balah 
balah 

""" %  recruitername

   msg.attach(MIMEText(text))

   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition',
           'attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   mailServer.sendmail(gmail_user, to, msg.as_string())
   # Should be mailServer.quit(), but that crashes...
   mailServer.close()


myemailfile = "/scripts/cyber_send"
emaillist =  open( myemailfile, 'r')


for i in emaillist.readlines():
    match = re.search(r'[\w\.-]+@[\w\.-]+', i)
    if not  match:
        continue
    print "%s " % match.group(0)
    hrname =  str(re.split("\.|_|-", match.group(0).split('@')[0] )[0])
    #hrname = ''
    mail( match.group(0) , "Sr Engineer ( information technology )",
   "/scripts/resume_.pdf" , hrname )
-------------------------------------------------------------------------------------------------------------------

whats app plugin for Nagios


If you want to send alerts to your phone on watsapp this is one way . 

apt-get install python python-dateutil python-argparse python-dev python-setuptools



python install setup.py

cd yowsup

./yowsup-cli registration --requestcode sms --phone 91yournumber  --cc 91  --mcc 404 --mnc 20

./yowsup-cli registration --register 671587 --phone 91yournumber --cc 91


cat yowsup-cli.config

cc=91
phone=yournumber
id=yourimeanumber
password=yourpassword


./yowsup-cli demos -c yowsup-cli.config  -s tonumber " saleeeeee"


Nagios Plugin config : 


cat /etc/nagios3/commands.cfg
 
define command{
        command_name    notify-service-by-watsapp
        command_line    /usr/lib/nagios/plugins/whatsapp.sh --numbers $_CONTACTDEVOPSPHONE$  --msg "***** Nagios : $NOTIFICATIONTYPE$ ***** \n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$ "
}


cat  /etc/nagios3/contacts.cfg


define contact{
        contact_name                    admin
        alias                           root
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    w,u,c,r
        host_notification_options       d,r
        service_notification_commands   notify-service-by-email,notify-service-by-watsapp
        host_notification_commands      notify-host-by-email
        _devopsphone                    9819031945
        email                           youremailid
        }




cat /usr/lib/nagios/plugins/whatsapp.sh

#!/bin/bash


while [  "$1" != "" ]
do
case "$1" in
--numbers) numbers=$2; shift;;
--msg) msg=$2; shift;;
*) echo "$0: invalid option $1" >&2
echo "Usage: $0   --numbers 9819031911 --msg \" test mgs\"" >&2
exit 1
esac
shift
done


ph_nums=($(echo $numbers | sed 's/,/ /g' ))

prntmsg=$(printf "$msg")


for i in ${ph_nums[@]}
do
sudo /opt/yowsup/yowsup/yowsup-cli demos -c /opt/yowsup/yowsup/yowsup-cli.config  -s 91${i} "$prntmsg"
echo "$i $msg"

done

send email using MIME::Lite::TT::HTML

#!/usr/bin/perl

 use strict;
 use warnings;
 use MIME::Lite::TT::HTML;




email ('agnello','agnello.dsouza@gmail.com');


sub email {

 my ( $recruiter_name, $to ) = @_;

 my %options;
 $options{INCLUDE_PATH} = '/scripts';
 my %params;
 $params{recruiter_name} = $recruiter_name ;

 my $msg = MIME::Lite::TT::HTML->new(
          From        =>  'agnello.dsouza@gmail.com'  ,
            To          =>  $to,
            Subject     =>  'Some subject  ',
            Template    =>  {
                                html     =>  'resume.tt',
                                text     =>  'text.resume.tt',
                            },
            TmplOptions =>  \%options,
            TmplParams  =>  \%params,
 );

# Set our content type properly
 $msg->attr("content-type"  => "multipart/mixed");

 # Attach a PDF to the message
 $msg->attach(  Type        =>  'application/pdf',
                Path        =>  '/scripts',
                Filename    =>  'Agnello_dsouza.pdf',
                Disposition =>  'attachment'
 );


# $msg->send('smtp', "smtp.gmail.com",   SSL=>1,  AuthUser=>'agnello.dsouza.linux@gmail.com',  AuthPass=>'xxxxxxxxxx',  Debug=>1);
 $msg->send();


}

HACK : script to get all linkedin Contact email IDs -- part 1

#!/usr/bin/perl


use strict;
use warnings;
use WWW::Mechanize;
use LWP;
use JSON;
use Data::Dumper;
use Getopt::Long;
use POSIX qw/ strftime /;
my $username = 'agnello.dsouza';
my $password = 'xxxxxxxxxxxxxx';
my $loginurl=  $maiurl.'/uas/login?goback=&trk=hb_signin';
my $json_file = '/tmp/json.data';



my $mech = login_linkedin();

#my $jdata = get_jdata($mech,$all_data);
#write_to_file($jdata,$json_file);

my $jdata = read_from_file($json_file);

my @contacts = gen_url ($jdata);

foreach (@contacts){
my $fnd_details_json = get_jdata($mech,$_);
my $email = $fnd_details_json->{'contact_data'}->{'emails_extended'}->[0]->{'email'};
next unless ( $email);
print "$email\n"
}





sub gen_url {
my $jdata= shift;
my @url ;
for my $i (@{$jdata->{'contacts'}} ){
my $uri = $maiurl.'/contacts/api/contacts/'.$i->{'id'}.'/?fields=name,emails_extended,birthday,phone_numbers,sites,addresses,company,title,location,ims,profiles,twitter,wechat,display_sources';
push (@url,  $uri );
}
return @url;
}


sub get_jdata {
my ( $mech,$url ) =@_;
$mech -> get ("$url");
return    decode_json($mech ->content()) ;
}


sub login_linkedin {
my $mech = WWW::Mechanize->new();
$mech -> cookie_jar(HTTP::Cookies->new());


$mech -> get("$loginurl");
$mech -> form_id('login');
$mech -> field ('session_key' => $username);
$mech -> field ('session_password' => $password);
$mech -> click_button (value => 'Sign In');

return $mech ;

}



sub read_from_file {
my $file = shift;
my $json;

{
  local $/; #Enable 'slurp' mode
  open my $fh, "<", "$file";
  $json = <$fh>;
  close $fh;
 }
return decode_json($json);
}

sub write_to_file {
my $data  = shift;
my $file  = shift;
open my $fh, ">", "$file";
print $fh encode_json($data);
close $fh;
}


perl script to send download file from website - integration with Jenkins- using Machanize

#!/usr/bin/perl

#use strict;
#use warnings;
use WWW::Mechanize;
use LWP;
use JSON;
use Data::Dumper;
use Getopt::Long;
use POSIX qw/ strftime /;
$| = 1;
my $username = 'something@something.com';
my $password = 'xxxxxx';
my $loginurl= 'https://loginurl';
my $insighturl= 'https://loginurl';
my %region_Squeryurl= ( 'apac' => '/cloud/paths/download/prod/slow-query/9538?s=1767176',
        'na' => '/cloud/paths/download/prod/slow-query/8972?s=1767151',
        'emea' => '/cloud/paths/download/prod/slow-query/3664?s=670131');
my $region;
GetOptions(
        'region|r=s' => \$region  );
die 'usage: $0 -r [apac|na|emea]'  unless( $region_Squeryurl{$region} );


my $date = my $time = strftime("%Y%m%d", localtime());
#my $log_dir = "$ENV{JENKINS_HOME}/docroot/getlog/";
my $log_dir = "/mnt/getlog/";

my $fullpath= "$log_dir"."$region/"."$date/";


unless (-d $fullpath ){
print "creating $fullpath ";
    system( "sudo mkdir -p  $fullpath" )  ;
    system( "sudo chown -R www-data:www-data  $fullpath" )  ;

}




my $mech = WWW::Mechanize->new();
$mech -> cookie_jar(HTTP::Cookies->new());

download_file ($username,$password,$loginurl,$insighturl,$region_Squeryurl{$region},$date,$fullpath);

sub download_file {

    my ($username,$password,$loginurl,$insighturl,$region_Squeryurl,$date,$fullpath)=@_;
    $mech -> get("$loginurl");
    $mech -> form_id('user-login');
    $mech -> field ('name' => $username);
    $mech -> field ('pass' => $password);
    $mech -> click_button (value => 'Sign in');
    $mech -> get ("$insighturl$region_Squeryurl");
    my $jdata_1 =   decode_json($mech ->content());

    $jdata_1 = $jdata_1->[1]->{'url'};
    $jdata_1 =~ s/'//g;
    print "$insighturl$jdata\n";
    while (1){
    sleep(2);
     $mech -> get ("$insighturl$jdata_1");
    $jdata = decode_json($mech ->content());
     print "get url $insighturl$jdata_1\n";
        if ( $jdata->[3]->{'delay'} =~ /^0$/ ) {
         print "mnt url --> ".$jdata->[3]->{'url'}."\n";
         print "delay --->". $jdata->[3]->{'delay'}."\n";
        last;
        }

    }
    my $D_url =  $jdata->[3]->{'url'}."\n";

    $mech -> get ("$D_url");
    $mech->save_content( "/tmp/mysql-slow-log-$date.$$.log" );
    system("sudo mv /tmp/mysql-slow-log-$date.$$.log ${fullpath}mysql-slow-log-$date.$$.log" );
    print $mech->content_type()."\n";
    if ($mech ->status()){
    print "created successfully ".$fullpath."mysql-slow-log-$date.$$.log\n ";
    my $msql_slow_log_q=$fullpath."mysql-slow-log-$date.$$.log";
    system("/bin/bash $ENV{WORKSPACE}/scripts/pt-query-digest.sh $region $msql_slow_log_q ");
    }

}
#print $mech ->status();
#print $mech ->content();

#print $mech-> content();


logstash - ELK

input {
        file {
         path => [  "/var/log/apache2/example.com_access_log",
                    "/var/log/apache2/domain.com_access_log",
                    "/var/log/apache2/somesite.com_access_log",
                    "/var/log/apache2/access.log"
                 ]
         type => "apache-access-log"
            }


         }


filter {
  if [type] == "apache-access-log" {
    grok {
      match => { "message" => "%{COMBINEDAPACHELOG}" }
    }
  }


  date {
    match => [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z" ]
  }
}






logstash.conf 

output {
stdout {
    codec => rubydebug
  }
elasticsearch {
         bind_host => "127.0.0.1"
        cluster => "elasticsearch"
         host => "127.0.0.1"
        }
   }


web scraping part 1

This script download all emails of all recruiters. 

=============================================
using simple regex. 
=============================================
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;
use LWP::Simple;




my $html =  get "$url";

my @html = split(/\n/,$html);

for my $i  (@html){
    next unless  $i =~ /<a href="\/recruiter\/(\S.*\/)">/ ;
    print "${url}${1}\n";
    my $indiv_recruiter = get("$url$1");
    my @indiv_recruiter = split(/\n/,$indiv_recruiter);
    for my $j (@indiv_recruiter ){

     next unless $j =~ /<a class="recruiter-email-link" href="mailto:(\S.*?\@CyberCoders\.com)">/ ;
        print "$1\n"

    }
}

=========================================================
Using HTM::treebuilder::Xpath

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;
use LWP::Simple;
use HTML::TreeBuilder;
use HTML::TreeBuilder::XPath;




my $html =  get "$url";



my $tree = HTML::TreeBuilder::XPath->new_from_content();
$tree->parse($html);

my @customers = $tree->findnodes('//div[@class="recruiter-item"');

for my $custm (@customers){
    my @all_a_tag = $custm->look_down('_tag' => 'a'  ) ;
     my $recruiter_pth = $all_a_tag[0]->attr('href') ."\n" ;
      $recruiter_pth =~ s/\/recruiter\///g;
      get_email($recruiter_pth);

}


sub get_email {

my ($r_pth)=@_;
my $html =  get ("$url$r_pth") ;
my    $tree2 = HTML::TreeBuilder->new_from_content($html);
    my @mailto = $tree2->look_down( _tag => 'a' , class => 'recruiter-email-link' );
     print $mailto[0]->attr('href')."\n";
   print "get $url$r_pth";
}

Cloud developer Chef


> The sender of this email is registered with Naukri.com as dynproindia. To respond back directly to the Employer, please click on Reply button, or send an email to savitha@dynproindia.com
> Experience required for the Job: 5 - 15 years
> Annual Salary of the Job: 0.0 - 30.0 Lacs
>
> Apply Now   Reply
>
>
> Dear Candidate,
>>
>> Hi All
>>
>> About Dynpro
>> * DynPro specializes in helping companies build infrastructures that can weather the most fierce storms by focusing on strong fundamentals: appropriate and effective systems, the right people, and proven business processes. Our worldwide force of technology and business experts can identify the right solutions to meet any challenge. Our relationships with the world's best service and product providers ensures that you get the most practical, value-driven solutions available anywhere. Location: USA,UK, Kolkata,Bangalore,Noida
>>
>> Skill sets -Cloud,Chef,AWS
>>
>> Experience: 5+ Yrs
>>
>> Location: Bangalore
>>
>> NP: Who can join Immediate or within 15 to 20 days of time
>> JD
>>
>> Following is the detailed Job description:
>> 1. Hands on experience (preferably 2 years) on Chef and Ruby scripting.
>> 2. Aware of cloud technologies such as AWS, Azure.
>> 3. Experience on working on DevOps methodology.
>> 4. Good to have - shell, python scripting hands on.
>> 5. Technically strong with a min of 5 years of experience.
>> 6. Self-motivated, team player and ability to work in high pressure scenarios.
>> 7. Capable of taking instructions and action them without too much hand holding.
>> 8. Show technical innovation and strive for continuous improvement on the platform.
>>
>> Key skills required for the job are:
>>
>> Devops-Chef(Mandatory)
>> Amazon Web Services - AWS(Mandatory)
>> ServiceNow - SaaS
>>
>>
>>
>> Mode Of Employment: Contract to Hire
>>
>> If your profile is relevant to the JD then kindly revert me back with your updated resume and with below Details
>>
>> Total Exp :
>> Rel Exp :
>> Current CTC :
>> Expected CTC :
>> Notice Period :
>> Work Location :
>> Pan no:
>> Passport no:
>> Educational Background with University name and Year of Passing and with %
>>
>> Thanks & Regards,
>>
>> Savita
>> Dynpro India Pvt Ltd.
>> Bangalore
>> Email: savitha@dynproindia.com
>> URL: www.dynproindia.com ; www.dynpro.com
>
>
> Is this job relevant to you? Yes No
> Your feedback would help us in sending you the most relevant job opportunities
> Disclaimer:
> The sender of this email is registered with naukri.com as dynproindia (venkatesh@dynpro.com, #3793, 7th Main HAL 2nd Stage Indiranagar, 121, Dickenson Road,, BANGALORE, Karnataka - 560038) using Naukri.com services. The responsibility of checking the authenticity of offers/correspondence lies with you. If you consider the content of this email inappropriate or spam, you may: Forward this email to: compliance@naukri.com or you can Block this Company from searching your resume in the database.
> Advisory: Please do not pay any money to anyone who promises to find you a job. This could be in the form of a registration fee, or document processing fee or visa charges or any other pretext. The money could be asked for upfront or it could be asked after trust has been built after some correspondence has been exchanged. Also please note that in case you get a job offer or a letter of intent without having been through an interview process it is probably a scam and you should contact compliance@naukri.com for advise.
> Update Your Resume Now to be on the top of Employer's Resume Search
> Create your own job alert to receive similar jobs directly in your inbox.
> Your Resume has only 30 seconds to impress a Recruiter. Get your resume written by Experts.
> Call 1800-102-5557 now! (Toll-Free)

Devops professionals with chef automation


>>
>> Experienced DevOps Consultant working in an Agile Environment (Scrum) and specialized in design, implementation, administration, configuration, upgrade and maintenance of Chef, Clearcase, ClearQuest, TFS, JIRA, Splunk, VMware ESX, NetApp.Storage, Flexlm License Server, Perforce, Tableau, and BladeLogic. Strong hands on experience in Chef, docker, AWS, Openstack, Mysql.
>> Good experience in automation using Unix shell scripting, PowerShell, PowerCLI and Perl. Experienced in delivering virtualised environments for multiple applications like TFS, Splunk, Bladelogic.. Good experience in Build and Release activities. Excellent interpersonal skills and ability to communicate effectively with all levels of management.A quick learner with the confidence to master new applications and technologies in order to deliver results.
>>
>> * Help companies setup Enterprise Chef Environment and conducted demos/PoCs.
>> * Enable macbook provisioning for a using Chef.
>> * Write Chef cookbooks for various requirements from clients.
>> * Write Ohai plugins, chef handlers.
>> * Work on Chef cookbook testing using foodcritic, chefspec, rubocop, Test Kitchen, Vagrant.
>> * Write cookbooks to create Docker containers using Chef.
>> * Design and implement complex and efficient Splunk dashboards using Splunk DB Connect and Sideview Utils.
>> * Involve in the administration of AWS, Openstack and Cloudstack setups.
>> * Setup of MySQL database, importing data from external sources into MySQL
>>
>>.

Senior Puppet Engineer


>
> Job Description:
>
> A candidate with good DevOps exposure and expertise on managing Puppet infrastructure on various cloud platforms (preferably AWS). He/She should have an excellent working knowledge of Linux with Puppet Master/Client setup.
>
> Desired Skills & Experience
> Strong hands-on experience on various• linux platforms. Good knowledge of linux shell scripting for automation of regular activities.
> Extensive experience in• DevOps and Puppet Master/Client setup.
> Proficient in deploying• Puppet, Puppet Dashboard, and Puppet DB for configuration management to the existing infrastructure.
> Experience in configuring parameters in Puppet for• different deployments.
> Experience in enterprise software monitoring tools• like Nagios, Cacti, OpsView, ManageEngine.
> Experience working with• scripting/programming languages (shell, PHP, Ruby, Python and/or Perl)
> Knowledge in configuring Puppet Foreman.•
> Knowledge in Puppet Hiera.•
> Understanding of cloud computing e.g.• AWS, Azure, and Google etc. will be an advantage.
> • Puppet Certified Professional will be an advantage.
> Monitoring: Nagios, Cacti, ManageEngine•

Other Articles

Enter your email address: