Cisco Wireless Controller – WPA PSK passphrase change Script and HTTP Publish

change_wlc_ssid_passphrase.pl

#!/usr/bin/perl

use Net::SSH::Expect;

my $ssh = Net::SSH::Expect->new (
 host => "192.168.10.3",
 raw_pty => 1
);

my $pass = generatePassword(10);
print ("Generating new WPA passphrase: ".$pass."\n");
print ("Getting into WLC...\n");
$ssh->run_ssh() or die "SSH process couldn't start: $!";
$ssh->waitfor('User:', 10) or die "prompt 'User' not found after 10 second";
$ssh->send("user");
$ssh->waitfor('Password:', 3) or die "prompt 'Password' not found after 3 second";
$ssh->send("password");
$ssh->waitfor('(Cisco Controller)', 3) or die "prompt 'Cisco Controller' not found";
print ("Disabling SSID Wlan\n");
$ssh->exec("config wlan disable 7");
print ("Setting passphrase to SSID Wlan\n");
$ssh->exec("config wlan security wpa akm psk set-key ascii $pass 7");
print ("Enabling SSID Wlan\n");
$ssh->exec("config wlan enable 7");
$ssh->exec("logout");
$ssh->send("y");

open (MYFILE, '>temp.txt');
my $html_prefix = '<strong><font size="12">';
my $html_suffix = '</font></strong>';
print MYFILE ($html_prefix.$pass.$html_suffix."\n");
close (MYFILE);
system("cp -rf temp.txt /var/www/html/wireless/index.html");
print ("Job done!\n");

sub generatePassword {
$length = shift;
$possible = 'abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
while (length($password) < $length)
 { $password .= substr($possible, (int(rand(length($possible)))), 1);
}
return $password
}
Advertisements

Ping Multiple Host

#!/bin/bash

HOSTS=(192.168.3.241 192.168.3.240 192.168.3.21 192.168.3.216 192.168.3.100 192.168.3.213 192.168.2.239 192.168.2.229 192.168.2.230 192.168.2.218 192.168.2.241 192.168.2.206 192.168.3.219)

count=${#HOSTS[@]}
index=0
while [ "$index" -lt "$count" ]; do
 echo -e "-----------------------------------\n"
 echo -e "Haciendo ping ${HOSTS[$index]} ... \n"
 ping -c 3 ${HOSTS[$index]}
 let "index++"
done

Send email Perl with attached files and remove file after sent

send_email_file_report.pl

#!/usr/bin/perl

use MIME::Lite;

my $attached_file = $ARGV[0];
my $report_directory = "/mnt/reports";

my $msg = MIME::Lite->new(
 From => 'xxxx@xxxx.com',
 To => 'xxx@xxx.com',
 Cc => 'xxxx@xxxx.com, xxxxx@xxxx.com',
 Bcc => 'xxx@xxx.com, xxx@xxx.com',
 Subject => "$attached_file",
 Type => 'multipart/mixed',
);

$msg->attach(
 Type => 'TEXT',
 Data => "Mensaje : $attached_file \nNota: Este correo ha sido generado autom√°ticamente por favor no responda a este mensaje.",
);

$msg->attach(
 Type => 'text/plain',
 Path => "$report_directory/$attached_file",
 Filename => "$attached_file",
 Disposition => 'attachment'
);

$msg->send or die "Mensaje no enviado";

system("rm -rf $report_directory/$attached_file");


------------ bash execution ------------------------

#!/bin/bash

report_directory="/mnt/reports"
exec_directory="/root/bin"

cd $report_directory
for i in *.csv; do $exec_directory/send_email_file_report.pl $i; done

Backup Linux System

#!/bin/bash

# Global Variables
INFO_FILE="info_`hostname`_`date +%Y-%m-%d`"
BACKUP_DIRECTORY="/mnt/backup/Linux"
DIRECTORY_NAME="`hostname`"

# Create backup directory
mkdir -pv $BACKUP_DIRECTORY/$DIRECTORY_NAME

# Output commands backup
echo -e "`hostname` > cat /etc/issue" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
cat /etc/issue >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > uname -a" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
uname -a >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > free -m" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
free -m >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > fdisk -l" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
fdisk -l >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > df -h" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
df -h >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > cat /etc/fstab" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
cat /etc/fstab >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > ifconfig" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
ifconfig >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > netstat -nr" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
netstat -nr >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > cat /etc/rc.local" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
cat /etc/rc.local >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > netstat -tunpl" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
netstat -tunpl >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > pstree" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
pstree >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > crontab -l" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
crontab -l >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > for i in cat /etc/passwd|awk -F: '($3>999 && $3 < 10000 ) {print $1}'; do crontab -l -u $i;done" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
for i in `cat /etc/passwd|awk -F: '($3>999 && $3 < 10000 ) {print $1}'`; do crontab -l -u $i; done >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > iptables -nvL" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
iptables -nvL >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > cat /proc/cpuinfo" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
cat /proc/cpuinfo >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > dmidecode" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
dmidecode >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > lspci" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
lspci >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > lsusb" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
lsusb >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > ls /etc/rc*.d" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
ls /etc/rc*.d >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE

# Backup all directories excluding /proc /sys /dev
tar cf $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE.tar / --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/mnt

Arduino temperature sensor

#include "DHT.h"
#define DHTPIN 2 // what pin we're connected to

int led = 13;
int pinAlerta = 10;
unsigned counter;

#define DHTTYPE DHT11 // DHT 11

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

void setup() {
 Serial.begin(9600);
 pinMode(led, OUTPUT);
 pinMode(pinAlerta, OUTPUT);
 dht.begin();
}

void loop() {
 float h = dht.readHumidity();
 float t = dht.readTemperature();
 counter = counter + 1;
 if (isnan(t) || isnan(h)) {
 Serial.println("Failed to read from DHT");
 } else {
 Serial.print(counter);
 Serial.print(";");
 Serial.print(h);
 Serial.print(";");
 Serial.println(t);
 }
 if ( t > 25 ) {
 digitalWrite(pinAlerta, HIGH);
 }
 if ( t < 25 ) {
 digitalWrite(pinAlerta, LOW);
 }
 digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
 delay(1000); // wait for a second
 digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
 delay(1000); // wait for a second
 delay(1800);
}

Connect ssh hosts list (shortcut command)

connect.pl
#!/usr/bin/perl

use Term::ANSIColor;

if (! defined $ARGV[0]) {
 print "Don't be stupid please...";
 exit 0;
}

my $file = "/home/user/hosts.txt";
my $keysdir = "/home/user/keys";
my $username = $ARGV[1];
my $remote_hostname = $ARGV[0];
my $time = qx/date/;

if ( $ARGV[0] =~ /edit/) {
 my $command = "vim $file";
 system ($command);
 exit 0;
}

if ( $ARGV[0] =~ /list/) {
 print "\n";
 open (my $data, "<" , "$file")
 or die "Failed to open file: $!\n";
 while (my $row = <$data>) {
 chomp $row;
 print "$row\n"
 }
 exit 0;
}

open (my $data, "<" , "$file")
or die "Failed to open file: $!\n";

while (my $row = <$data>) {
 chomp $row;
 if ( $row =~ /$remote_hostname/) {
 if ( defined $ARGV[2] ) {
 my @args = ($row, $ARGV[2]);
 ssh_connect_key(@args);
 exit 0;
 }
 else {
 ssh_connect($row);
 exit 0;
 }
 }
}

print "Remote host not found!";

sub ssh_connect() {

 my $row = $_[0];
 my @parts = split(",",$row);
 my $hostname = $parts[0];
 my $ipaddress = $parts[1];
 my $port = $parts[2];
 print $time;
 print "Connecting to $hostname ...\n";
 print color("red"), "ssh $ipaddress -p $port -l $username \n", color("reset");
 my $command = "ssh $ipaddress -p $port -l $username";
 system($command);
 exit 0;

}

sub ssh_connect_key() {

 my $row = $_[0];
 my $key = $_[1];
 my @parts = split(",",$row);
 my $hostname = $parts[0];
 my $ipaddress = $parts[1];
 my $port = $parts[2];
 print $time;
 print "Connecting to $hostname ...\n";
 print color("red"), "ssh $ipaddress -p $port -l $username -i $keysdir/$key \n", color("reset");
 my $command = "ssh $ipaddress -p $port -l $username -i $keysdir/$key";
 system($command);
 exit 0;

}

close $data;

handy linux and technology info