# Check for Alibaba Cloud
if echo "$dmi" | grep -q 'Manufacturer: Alibaba'; then
- # Check for Alibaba Cloud ECS Bare Metal (EBM) Instance
- if [ "x$root" = "x" ] && ( { echo -e "GET /latest/meta-datainstance/instance-type HTTP/1.0\r\nHost: 100.100.100.200\r\n\r" >&3; grep -sq 'ebm' <&3 ; } 3<> /dev/tcp/100.100.100.200/80 ) 2>/dev/null ; then
- echo "alibaba_cloud-ebm"
+ if $(timeout --version >/dev/null 2>&1); then
+ timeout_cmd_prefix="timeout 1s"
+ timeout_cmp="-eq"
+ timeout_return_value=124
else
+ timeout_cmd_prefix=""
+ timeout_cmp="-ne"
+ timeout_return_value=0
+ fi
+
+ # Check for Alibaba Cloud ECS Bare Metal (EBM) Instance
+ metadata=$($timeout_cmd_prefix sh -c '( { echo -e "GET /latest/meta-datainstance/instance-type HTTP/1.0\r\nHost: 100.100.100.200\r\n\r" >&3; grep -s 'ebm' <&3 ; } 3<> /dev/tcp/100.100.100.200/80 ) 2>/dev/null')
+ ret_value=$?
+ if [ $ret_value $timeout_cmp $timeout_return_value ]; then
+ # a timeout occurred when fetching metadata, assuming remote host unaccessible
+ # which means it might be a non-cloud environment, or test environment.
echo "alibaba_cloud"
+ else
+ if [ -z "$metadata" ] ; then
+ echo "alibaba_cloud"
+ else
+ echo "alibaba_cloud-ebm"
+ fi
fi
fi