不同命令的多个别名
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 文件,其中包含允许我根据模型类型登录到单独的 "host" 文件的变量。它包含:
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
我正在尝试创建一个 bash 脚本,它将 ssh 连接到远程网络设备,运行 基于模型的命令,然后保存输出。
此时我的预期文件包含以下内容:
#!/user/bin/expect
set pw xxxxxxx
set timeout 5
spawn ssh [lindex $argv 0]
expect "TACACS Password:"
send "$pw\r"
interact
我有我的 .sh 文件,其中包含允许我根据模型类型登录到单独的 "host" 文件的变量。它包含:
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