for the past 2 days i was trying to get the results of a show run on my switch and send it to me via mail. i alrdy made a bash script for it but still i dunno what i'm doing wrong here and after fixing everything seems like my bash scripts has its problems.
i made a file for sshpass info like this : 1.1.1.1:myusername:mypass
now here is my bash script:
#!/bin/bash
EMAIL="$1"
while read line
do
export SSHPASS=$(echo $line | awk -F':' '{print $3}')
user=$(echo $line | awk -F':' '{print $2}')
ip=$(echo $line | awk -F':' '{print $1}')
[ ! -d $ip ] && mkdir $ip
echo "Backing up ($ip)"
confpath="./${ip}/${ip}_$(date +"%Y-%m-%d_%H-%M-%S").txt"
res=$(sshpass -e scp -oKexAlgorithms=+diffie-hellman-group1-sha1 -c aes128-cbc $user@$ip:running-config $confpath 2>&1)
exit_code=$?
echo $res
if [ $? -eq $exit_code ] ; then
echo "Done"
echo "backup for switch $ip" | mutt -a "$confpath" -s "backup for switch $ip" -- $EMAIL
else
echo "In $(date +'%Y-%m-%d_%H-%M-%S') tried to get a backup from $ip and it failed.\n\n###\n$res\n###" | mutt -s "backup for switch $ip" -- $EMAIL
echo "Failed"
fi
done <&0
also the installed mutt on my vps works perfectly when used alone,but when i try the format of the command im trying is : cat file | ./backup "some@email.com" it returns Failed and i have no idea why it should fail unless something is wrong with my script which i cant figure it out.if anyone needs any clarification about the script just shoot and i will explain more.
Thanks in advance guys :)