POST Json message to server

Send a POST to a Json server

#!/bin/bash
BOT_SERVICE=”http://bots.json.com”

message=$(cat <<EOF
{
“to”:”test@test.com”,
“from”:”test@test.com”,
“html”:”<font color=’red’>Am a bot.</font>”
}
EOF
)
curl -i -X POST -H “Content-Type: application/json” -d “$message” $BOT_SERVICE

Can also read in a file and format that as the message

#!/bin/bash

bot_input=”bot_input.txt”
BOT_SERVICE=”http://bots.json.com”

html_input=$(<$bot_input)

message=$(cat <<EOF
{
“to”:”test@test.com”,
“from”:”test@test.com”,
“html”:”$html_input”
}
EOF
)
curl -i -X POST -H “Content-Type: application/json” -d “$message” $BOT_SERVICE

SSH to multiple servers using Expect

Script will login to multiple servers and perform certain command automatically.

Requires an input file of format HostName:IP:Pass

#!/bin/bash
# Require an input.txt file with the format of CLLI:IP:Pass
# This scirpt will add the RSA key for lss user

if [ -e “input.txt” ]; then
while read i; do

/usr/bin/expect <(cat << EOD
set timeout 15
spawn ssh “user@$(echo $i | cut -d: -f 2)”
#######################

expect “yes/no” {
send “yes\r”
expect “Password:” { send {$(echo $i | cut -d: -f 3)}; send \r }
} “Password:” { send {$(echo $i | cut -d: -f 3)}; send \r }
expect -re “day: $” { send “\r” }
expect “:” { send “\r” }
expect -re “# $” { send “ll\r” }
expect -re “# $” { send “mkdir .ssh\r” }
expect -re “# $” { send “cd .ssh\r” }
expect -re “# $” { send “touch authorized_keys\r” }
expect -re “# $” { send “chmod 700 authorized_keys\r” }
expect -re “# $” { send “echo ‘ssh-rsa KEY’ >> authorized_keys\r” }
expect -re “# $” { send “cat authorized_keys\r” }
expect -re “# $” { send “exit\r” }
EOD
)
done < input.txt
fi

Sendmail sample

Sendmail script passing in a file as message. File have filename format test.hostname.network

#!/bin/bash

mailserver=’test@test.com’
for f in $*
do
m=$( echo $f | cut -d. -f2 )
mail -s “XML file from $m” $mailserver < $f
done

You execute the script as such

./sample_script.sh filename

./sample_script.sh test*                    # execute for all files begin with test in directory

Download from multiple servers using SCP

Script allow you to download from multiple servers using SCP protocol and sshpass to send password for authentication. Files will be stored in different directory with hostname.

Requires an input file with format HostName:IP:Pass

if [ -e “input.txt” ]; then
while read i; do
mkdir “$(echo $i | cut -d: -f 1)”
echo “$(echo $i | cut -d: -f 3)” > pass.txt
sshpass -f “pass.txt” scp -o StrictHostkeyChecking=no user@”$(echo $i | cut -d: -f 2)”:/tmp/*sample “./$(echo $i|cut -d: -f 1)”
done < input.txt
fi

rm pass.txt