1

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 :)

Hamid
  • 11
  • 3
  • I don't really understand why you are comparing your `$exit_code` against the exit status of the following `echo` command; regardless, if the result is not what you expect then the next step would be to look at both those values – steeldriver May 13 '19 at 13:15
  • @steeldriver yeah i sure am not a good script writer m8 and as you can see its not rly a complicated one and i know i have a looong way the better logics behind programming, i honestly am new to bash script and i would really appreciate if you could show me a cleaner way and more reasonable one to write this script. Thanks again :) – Hamid May 14 '19 at 07:30

0 Answers0