RHEL 6.2를 실행하면서 SSH를 통해 원격 서버 목록에 연결하는 bash 스크립트를 작성하고 CPU 및 총 메모리를 다음 형식으로 호스트당 한 줄씩 파일에 씁니다.
HOSTNAME1 CPUS: 12 MEMORY: 64
HOSTNAME2 CPUS: 08 MEMORY: 12
지금까지 내가 가지고 있는 것은 다음과 같습니다. 제대로 작동하지 않습니다. 부분 시스템 SSH 실행 cat /proc/cpuinfo
및 free
.
Bash 스크립트는 다음과 같이 호출됩니다../query_host_info.sh <DEST> <USER> <FILE>
호스트 목록을 읽는 파일은 한 줄에 하나의 호스트 이름 파일입니다.
#!/bin/bash
# username to connect via ssh
USER=$2
# destination path/filename to save results to
DEST=$3
# source list of hostnames to read from
FILE=$1
# Iterate through line items in FILE and
# execute ssh, if we connected successfully
# run proc/info and free to find memory/cpu alloc
# write it to DEST path/file
# if we don't connect successfully, write the hostname
# and "unable to connect to host" error to DEST path/file
for i in `cat $FILE`; do
echo -n ".";
CHK=`ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" \
-l $USER $i "echo success"`;
if [ "success" = $CHK ] >/dev/null 2>&1
then
`ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" -l $USER $i "\
printf "$i ";
echo "`cat /proc/cpuinfo | grep processor | awk '{a++} END {print a}';
free -g | sed -n -e '/^Mem:/s/^[^0-9]*\([0-9]*\) .*/\1/p'`";" >> ${DEST}`;
else
printf "${i}\tUnable to connect to host\n" >> ${DEST};
fi
done
# All line items have been gone through,
# show done, and exit out
echo ""
echo "Done!"
echo "Check the list 'checkssh_failure' for errors."
exit 0
답변1
방금 스크립트를 수정했습니다.
#!/bin/bash
# username to connect via ssh
USER=$2
# destination path/filename to save results to
DEST=$3
# source list of hostnames to read from
FILE=$1
[[ $# -ne 3 ]] && { echo -e "\nUsage: $0 <User> <ServerList> <LogFile>\n"; exit 1; };
func_ssh() {
local Ipaddr=$1
local Cmd="${@:2}"
local LogIt=${DEST}
ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" -l $USER $Ipaddr "${Cmd}"
[[ $? -ne 0 ]] && printf "${Ipaddr}\tUnable to connect to host\n" >> ${LogIt}
}
GetTotalProcs="awk '/processor/{a++} END{print a}' /proc/cpuinfo"
GetMemoryDetails="free -g | sed -n -e '/^Mem:/s/^[^0-9]*\([0-9]*\) .*/\1/p'"
# Iterate through line items in FILE and
# execute ssh, if we connected successfully
# run proc/info and free to find memory/cpu alloc
# write it to DEST path/file
# if we dont connect successfully, write the hostname
# and "unable to connect to host" error to DEST path/file
for srv in $(< $FILE );
do
echo -n "."
A="$( func_ssh $srv $GetTotalProcs )"
B="$( func_ssh $srv $GetMemoryDetails )"
echo "${srv} CPU: ${A} MEMORY: ${B}" >> ${DEST}
done
# All line items have been gone through,
# show done, and exit out
echo ""
echo "Done!"
echo "Check the list 'checkssh_failure' for errors."
exit 0
답변2
이렇게 스크립트를 실행할 수 없나요? :
% ./query_host_info.sh <DEST> <USER> <FILE> > output.log
-또는-
% ./query_host_info.sh <DEST> <USER> <FILE> | tee output.log
또한 명령 목록을 사용하여 여러 명령을 괄호로 묶고 해당 출력을 파일로 리디렉션할 수 있습니다.
% (ls; ls; ls;) > some_output.log
답변3
알아냈어.. 두 가지가 있었어.. SSH는 proc/cpuinfo를 좋아하지 않았고 연결도 자유로웠어. 명령이 실행되는 방식을 재구성했지.. 아마도 더 나은 방법일 것 같은데, 이게 작동하는 것 같았어..
#!/bin/bash
# username to connect via ssh
USER=$2
# destination path/filename to save results to
DEST=$3
# source list of hostnames to read from
FILE=$1
# Iterate through line items in FILE and
# execute ssh, if we connected successfully
# run proc/info and free to find memory/cpu alloc
# write it to DEST path/file
# if we dont connect successfully, write the hostname
# and "unable to connect to host" error to DEST path/file
for i in `cat $FILE`; do
echo -n ".";
CHK=`ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" \
-l $USER $i "echo success"`;
if [ "success" = $CHK ] >/dev/null 2>&1
then
A=`ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" -l $USER $i "cat /proc/cpuinfo | grep processor | awk '{a++} END {print a}'"`
B=`ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" -l $USER $i "free -g | sed -n -e '/^Mem:/s/^[^0-9]*\([0-9]*\) .*/\1/p'"`
echo "${i} CPU: ${A} MEMORY: ${B}" >> ${DEST}
else
printf "${i}\tUnable to connect to host\n" >> ${DEST};
fi
done
# All line items have been gone through,
# show done, and exit out
echo ""
echo "Done!"
echo "Check the list 'checkssh_failure' for errors."
exit 0