[英]Multiple Aliases for different commands
我正在尝试创建一个 bash 脚本,该脚本将通过 ssh 连接到远程网络设备,运行基于模型的命令,然后保存输出。
此时,我的期望文件包含以下内容:
#!/user/bin/expect
set pw xxxxxxx
set timeout 5
spawn ssh [lindex $argv 0]
expect "TACACS Password:"
send "$pw\r"
interact
我有我的 .sh 文件,其中包含允许我根据模型类型登录到单独的“主机”文件的变量。 它包含了:
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
当我运行我的 .sh 时,一切都按预期运行。 我的问题在于我不知道如何称呼我的别名。 我已经编辑了 .bashrc 并获取了它。 .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"
我还在我的 .sh 别名中添加了别名。 但似乎无法获得正确的语法。 我尝试了以下变体,但没有成功......
for i in $( cat $fpath/3560hosts )
do
expect script.exp $i $loc3560 >> "$opath"
done
和
for i in $( cat $fpath/3560hosts )
do
expect script.exp $i >> "$opath";
$loc3560
done
将不胜感激任何关于在哪里放置这些给他们打电话的建议。
不幸的是,我无法弄清楚如何在我的主 .sh 脚本中调用我的函数或别名。 有了这个,虽然我找到了一种方法来通过我的期望文件来实现我想要的。 下面列出的代码,它就像一个魅力。 我的 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
从这里我可以调用我的脚本并通过以下方式将其输出到我目录中的日志文件:
./script.sh >> Results.log
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.