Search code examples
bashunixredhat

Multiple Aliases for different commands


I am attempting to create a bash script that will ssh into remote network devices, run commands based on the model, and then save the output.

At this time I have my expect file that contains the following:

#!/user/bin/expect

set pw xxxxxxx
set timeout 5

spawn ssh [lindex $argv 0]

expect "TACACS Password:"
    send "$pw\r"
interact

I have my .sh file that contains variables which allows me to login to separate "host" files based on Model type. It contains:

shopt -s expand_aliases

fpath="path where scripts are located"
opath="MAC_Results.log"

for i in $( cat $fpath/3560hosts )
do
expect script.exp $i >> "$opath"
done

When I run my .sh, everything operates as expected. My issue lies in I do not know how to call my aliases. I have edited the .bashrc and have sourced it. The .bashrc contains the following:

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# User specific aliases and functions

alias loc3560="term length 0; show mac address-table | ex Gi|CPU|Po; exit"
alias locx="term length 0; show mac address-table | ex Gi[1|2]/1|CPU|Vl99|Po1|dynamic|system; exit"

I have also added the aliases within my .sh aliases. but cant seem to get the right syntax. I have tried the following variations but with no success...

for i in $( cat $fpath/3560hosts )
do
expect script.exp $i $loc3560 >> "$opath"
done

and

for i in $( cat $fpath/3560hosts )
do
expect script.exp $i >> "$opath";
$loc3560
done

Would appreciate any suggestions on where to put these to call to them.


Solution

  • Unfortunately I was not able to figure out how to call my functions or aliases within my main .sh script. With that though I found a way to achieve what I wanted via my expect file . Code listed below and it works like a charm. My subdirectory of 21 files has now been reduced to 3!

    set loc3560 "show mac address-table | ex Gi|CPU|Po
    exit"
    set locx "show mac address-table | ex Gi1/1|Gi2/1|CPU|Vl99|Po1|dynamic|system
    exit"
    set loc4500 "show mac address-table | ex 1/3|1/5|Port-channel|Switch|1/1|1/2|dynamic|system
    exit"
    set loc888 "show mac-address-table | i FastEthernet
    exit"
    set loces2 "show mac address-table | i Fa0
    exit"
    
    spawn ssh [lindex $argv 0]
    
    expect "TACACS Password:"
        send "$pw\r"
    
    expect  "#"
        send "$ver\r"
    
    expect {
        "C3560-IPSERVICESK9-M" {
        send "$loc3560\r"
        exp_continue
    }
        "CAT3K_CAA-UNIVERSALK9" {
        send "$locx\r"
        exp_continue
    }
        "C3750E-UNIVERSALK9-M" {
        send "$locx\r"
        exp_continue
    }
        "CISCO888-SEC-K9" {
        send "$loc888\r"
        exp_continue
    }
        "bootflash:/cat4500e" {
        send "$loc4500\r"
        exp_continue
    }
        "SM-ES2-24" {
        send "$loces2\r"
        exp_continue
    }
    }
    interact
    
    expect "#"
        send "exit\r"
    end
    

    From here I am then able to call my script and output it to a log file in my directory via:

    ./script.sh >> Results.log