如何通过SSH从多个源输出数据以获取主机列表(使用Bash)

时间:2015-09-03 05:40:13

标签: bash ssh

下面是我当前的Bash代码,用于从文件“testhost”转到列表框,它将转到testhost中列出的每个主机并收集我的wiki的信息并将其打印到Wiki格式的日志文件中。我试图用下面的代码做一些事情,例如我希望能够减少我必须SSH到一个盒子的次数,如果我可以一次设置多个变量那将是很好的。

其次我想在SSH'd主机上设置局部变量,然后运行一些if语句并返回值,我有点麻烦。

有关如何处理此事的任何提示都会很棒。

#!/bin/bash
for i in `cat testhost`
do
HOSTZ=`ssh $i "hostname" |cut -d. -f1`
printf "^  $HOSTZ  ^^^^^^\n" >> WikiMike
echo "^  Make  ^  Model  ^  CPU  ^  Number of Cores  ^  Memory  ^  Serial Number  ^" >> WikiMike
MAKE=`ssh $i "dmesg" |grep ProL |awk 'NR==1' |awk '{print $2}'`
MODEL=`ssh $i "dmesg" |grep ProL |awk 'NR==1' |awk '{print $3,$4,$5}' |sed 's/[,]//g'`
CPUZ=`ssh $i "cat /proc/cpuinfo" |grep 'model name' |awk 'NR==1' |awk '{print $7,$8,$9}'`
COREZ=`ssh $i "cat /proc/cpuinfo" |grep processor |tail -n 1 |awk '{print $3}'`
COREZ=`expr $COREZ + 1`
MEMZ=`ssh $i "cat /proc/meminfo" |head -n 1 |awk '{print $2}'`
MEMZ=`expr $MEMZ / 1024 / 1024`
SERIAL=`ssh $i "lshal" |grep system.hardware.serial |awk '{print $3}' |tr -d "'"`
echo "|  $MAKE  |  $MODEL  |  $CPUZ  |  $COREZ  |  $MEMZ gb  |  $SERIAL  | " >> WikiMike
echo "^  Interface  ^  Name       ^  MAC           ^  IP                ^  Broadcast              ^  Mask  ^" >> WikiMike
ssh $i "/sbin/ifconfig"  |egrep "eth|inet|bond" |sed 's/eth/~eth/g' |tr "\012" " " |tr "~" "\012" |grep inet |awk '{print "|             | ",$1,"  | "$5," | ",$7," | ",$8," | ",$9," |"}' >> WikiMike
printf "\n\n" >> WikiMike
done

1 个答案:

答案 0 :(得分:1)

您可以像这样重构您的程序,以减少每个主机所需的ssh连接:

            key = {}
            statokey = 0

            year = form.cleaned_data["year"]
            residence = form.cleaned_data["residence"]
            starred = form.cleaned_data["starred"]
            education_type = form.cleaned_data["education_type"]
            valutation = form.cleaned_data["valutation"]

            if year:
                key['birthdate__year'] = year

            if residence:
                key['residence__icontains'] = residence

            if starred:
                key['starred'] = starred

            if education_type:
                e = Education_type.objects.filter(title=education_type)
                result_education = Education.objects.filter(education_type=e).values_list('profile_id', flat=True)
                if statokey > 0:
                    for r in result_education:
                        for k in key['id__in']:
                            if r == k:
                                key['id__in'] = str(r)

                else:
                    key['id__in'] = result_education
                    statokey += 1

            if valutation:
                result_valutation = Status.objects.filter(valutation=valutation).values_list('profile_id', flat=True)
                if statokey > 0:
                    for r in result_valutation:
                        for k in key['id__in']:
                            if r == k:
                                key['id__in'] = str(r)
                else:
                    key['id__in'] = result_valutation
                    statokey += 1


            result_profile = Profile.objects.filter(**key).order_by('first_name')

请注意,您当前的代码并不总是遵循shell最佳做法,例如:

  • should use $() instead of backticks
  • superfluous uses of cat
  • 使用#!/bin/bash function wikiOutput() { HOSTZ=$( hostname | cut -d. -f1 ) MAKE=$( dmesg | grep ProL | awk 'NR==1' | awk '{print $2}') MODEL=$( dmesg | grep ProL | awk 'NR==1' | awk '{print $3,$4,$5}' | sed 's/[,]//g') CPUZ=$( cat /proc/cpuinfo | grep 'model name' | awk 'NR==1' | awk '{print $7,$8,$9}') COREZ=$( cat /proc/cpuinfo | grep processor | tail -n 1 | awk '{print $3}') COREZ=$( expr $COREZ + 1) MEMZ=$( cat /proc/meminfo | head -n 1 | awk '{print $2}') MEMZ=$( expr $MEMZ / 1024 / 1024) SERIAL=$( lshal | grep system.hardware.serial | awk '{print $3}' | tr -d "'") INTERFACES=$( /sbin/ifconfig | egrep "eth|inet|bond" | sed 's/eth/~eth/g' | tr "\012" " " | tr "~" "\012" | grep inet | awk '{print "| | ",$1," | "$5," | ",$7," | ",$8," | ",$9," |"}' ) echo "^ $HOSTZ ^^^^^^" echo "^ Make ^ Model ^ CPU ^ Number of Cores ^ Memory ^ Serial Number ^" echo "| $MAKE | $MODEL | $CPUZ | $COREZ | $MEMZ gb | $SERIAL | " echo "^ Interface ^ Name ^ MAC ^ IP ^ Broadcast ^ Mask ^" echo "$INTERFACES" echo ; echo } # for each host: while read hostname do # define function remotely, then call it: { type wikiOutput | tail -n +2 ; echo wikiOutput ; } | ssh "$hostname" done < testhost >> WikiMike 可以替换为算术评估expr,它既是内置的,也更具可读性

我没有纠正大部分内容以防止分散实际问题。