我希望写一个login脚本。 但是我希望这可以在其他不同的脚本中重用。 我想让所有的login命令都是bash子程序的一部分。 即,而不是
expect_login.sh #!/bin/usr/expect -f spawn .... set ....
我要这个:
expect_login { # put some necessary command to initiate expect program spawn ... set ... }
所以我想把这个子程序放在一个文件/库中,这个文件/库可以被许多不同的脚本重用。
我怎样才能做到这一点?
谢谢
PS:请原谅我的不准确的bash / expect语法。 我只想写一个伪代码的方式。
我会去2部分的解决scheme。 一部分是expect脚本,另一部分是Shell脚本。
对于期望的脚本,它应该是一个通用的脚本,接受input,并产生输出。
这是我的示例期望脚本接受主机名和密码,并将生成服务器的vcprofile名称
[user@server ~]$ cat getvcprofile.expect #!/usr/bin/expect set timeout 2 set host [lindex $argv 0] set password [lindex $argv 1] spawn ssh "ADMIN\@$host" expect_after eof { exit 0 } expect "yes/no" { send "yes\r" } expect "assword" { send "$password\r" } expect "oa>" { send "show vcmode\r" } expect "oa>" { send "exit\r" } exit
在shell脚本中,我将调用期望脚本并为其提供variables,在这种情况下是vcsystem的主机名。 密码实际上是根据主机名OA @ XXXX的模式 – 其中XXXX是服务器的最后4位数字
[user@server ~]$ cat getvcprofile.sh #/bin/bash # get VC profile for a host host=$1 #get the blade enclosure enclosure=`callsub -enc $host |grep Enclosure: | cut -d" " -f2` if [ ! -z $enclosure ]; then #get the last 4 digit of the enclosure fourdigit=${enclosure: -4} domain=`./getvcprofile.expect ${enclosure}oa OA@${fourdigit} |grep Domain|awk '{print $NF}'` echo $domain else echo "None" fi
有了这2部分的解决scheme,我可以做这样的事情:
for X in `cat serverlist.txt`; do echo -n $X": "; ./getvcprofile.sh $X; done
它将打印出文件serverlist.txt中每个服务器的vcprofile文件