Bash 不同命令的多个别名
我正在尝试创建一个bash脚本,该脚本将ssh连接到远程网络设备,根据模型运行命令,然后保存输出 此时,我的expect文件包含以下内容:Bash 不同命令的多个别名,bash,unix,redhat,Bash,Unix,Redhat,我正在尝试创建一个bash脚本,该脚本将ssh连接到远程网络设备,根据模型运行命令,然后保存输出 此时,我的expect文件包含以下内容: #!/user/bin/expect set pw xxxxxxx set timeout 5 spawn ssh [lindex $argv 0] expect "TACACS Password:" send "$pw\r" interact # .bashrc # Source global definitions if [ -f /e
#!/user/bin/expect
set pw xxxxxxx
set timeout 5
spawn ssh [lindex $argv 0]
expect "TACACS Password:"
send "$pw\r"
interact
# .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文件,该文件允许我根据模型类型登录到单独的“主机”文件。它包括:
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包含以下内容:
#!/user/bin/expect
set pw xxxxxxx
set timeout 5
spawn ssh [lindex $argv 0]
expect "TACACS Password:"
send "$pw\r"
interact
# .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
及
如果您能就如何调用这些函数提出建议,我将不胜感激。不幸的是,我无法在main.sh脚本中找到如何调用函数或别名的方法。尽管如此,我还是找到了一种通过expect文件实现我想要的东西的方法。下面列出了代码,它就像一个符咒。我的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
使用函数而不是别名谢谢提示!周一我必须尝试一下,因为一切都在进行中。@Inian我已经编辑了bashrc文件,并将命令转换成函数。我已经再次获取了它的源代码并编辑了我的.sh文件。当我运行我的.sh时,我通常会得到“Command Not found”,然后它会包含我希望在Cisco设备上运行的命令的字符串。