[英]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.我正在尝试创建一个 bash 脚本,该脚本将通过 ssh 连接到远程网络设备,运行基于模型的命令,然后保存输出。
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.我有我的 .sh 文件,其中包含允许我根据模型类型登录到单独的“主机”文件的变量。 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.当我运行我的 .sh 时,一切都按预期运行。 My issue lies in I do not know how to call my aliases.
我的问题在于我不知道如何称呼我的别名。 I have edited the .bashrc and have sourced it.
我已经编辑了 .bashrc 并获取了它。 The .bashrc contains the following:
.bashrc 包含以下内容:
# .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.我还在我的 .sh 别名中添加了别名。 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.将不胜感激任何关于在哪里放置这些给他们打电话的建议。
Unfortunately I was not able to figure out how to call my functions or aliases within my main .sh script.不幸的是,我无法弄清楚如何在我的主 .sh 脚本中调用我的函数或别名。 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!
我的 21 个文件的子目录现在已减少到 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.