我有一些简单的脚本可以生成SOAP请求并将其发送/评估到许多Web服务,从本质上讲,它们提交了一个示例请求以确保Web服务正常运行并按预期方式工作。
它们用作我的客户的状态检查。
因此DST在前几天才更改,并且由于时间不多了,而且我在BST / GMT中,脚本停止工作,因此我尝试了所有我能想到的(export TZ =“:Europe / Dublin”等..etc ..)没有喜悦。
您知道如何更改“ ctime”变量以产生本地时间吗?
#!/bin/bash
#//Get parameters
ENV="$1"
ENV2="$2"
checkdir="/opt/local/c/c/c/"
#//Generate nonce and hashed password
nonce=$(echo "$RANDOM$RANDOM$RANDOM")
hashnonce=$(echo $nonce | base64)
passwd="tiger"
ctime=`echo $(date +'%s')`
etime=`echo "$ctime+600" | bc`
timestamp=`echo $(date -d "1970-01-01 00:00:00 $ctime seconds" +'%Y-%m-%dT%H:%M:%S.%3NZ')`
timecreate=`echo $(date -d "1970-01-01 00:00:00 $ctime seconds" +'%Y-%m-%dT%H:%M:%SZ')`
timeexpire=`echo $(date -d "1970-01-01 00:00:00 $etime seconds" +'%Y-%m-%dT%H:%M:%SZ')`
combined=`echo -e "$nonce\n$timestamp$passwd"`
hashpass=$(echo -n "$combined" | openssl sha1 -binary | base64)
#//Output generated XML to file
echo -e '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sear="http://www.x.com/c/c/c">
<soapenv:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" soapenv:mustUnderstand="1"><wsu:Timestamp wsu:Id="TS-20"><wsu:Created>'$timecreate'</wsu:Created><wsu:Expires>'$timeexpire'</wsu:Expires></wsu:Timestamp><wsse:UsernameToken wsu:Id="UsernameToken-19"><wsse:Username>scott</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">'$hashpass'</wsse:Password><wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">'$hashnonce'</wsse:Nonce><wsu:Created>'$timestamp'</wsu:Created></wsse:UsernameToken></wsse:Security></soapenv:Header>
<soapenv:Body>
<sear:searchRequest>
<sear:importantNumber>'$ENV2'</sear:importantNumber>
<sear:status>R</sear:status>
<sear:versionNumber>1.0</sear:versionNumber>
</sear:searchRequest>
</soapenv:Body>
</soapenv:Envelope>' > $checkdir/Request_$ENV.xml
#//Send soap request and output to text file
curl -s --header "content-type: application/soap+xml" --data @$checkdir/Request_$ENV.xml https://$ENV.x.com:443/c/c/c/c/ > $checkdir/Result_$ENV.txt
#//Grep for response
echo `grep "Complete" $checkdir/Result_$ENV.txt | wc -l`
exit;
最佳答案
这是一个解决方案。我已经删除了所有无用的echo
和反引号。解决问题的方法只是将TZ
赋予date
,请参见下文。
#!/bin/bash
# Get parameters
if (($# != 2)); then
echo "USAGE: $0 arg1 arg2" >&2 # Specify what parameters you need
exit 1
fi
ENV="$1"
ENV2="$2"
# Configuration
MYTZ="Europe/Dublin"
checkdir="/opt/local/c/c/c/"
# Generate nonce and hashed password
nonce="$RANDOM$RANDOM$RANDOM"
hashnonce=$(echo $nonce | base64)
passwd="tiger"
ctime=$(TZ="$MYTZ" date +'%s')
etime=$(echo "$ctime+600" | bc)
timestamp=$(TZ="$MYTZ" date -d "1970-01-01 00:00:00 $ctime seconds" +'%Y-%m-%dT%H:%M:%S.%3NZ')
timecreate=$(TZ="$MYTZ" date -d "1970-01-01 00:00:00 $ctime seconds" +'%Y-%m-%dT%H:%M:%SZ')
timeexpire=$(TZ="$MYTZ" date -d "1970-01-01 00:00:00 $etime seconds" +'%Y-%m-%dT%H:%M:%SZ')
combined=$(echo -e "$nonce\n$timestamp$passwd")
hashpass=$(echo -n "$combined" | openssl sha1 -binary | base64)
# Output generated XML to file
cat > "$checkdir/Request_$ENV.xml" << ENDOFTEXT
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sear="http://www.x.com/c/c/c">
<soapenv:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" soapenv:mustUnderstand="1"><wsu:Timestamp wsu:Id="TS-20"><wsu:Created>'$timecreate'</wsu:Created><wsu:Expires>'$timeexpire'</wsu:Expires></wsu:Timestamp><wsse:UsernameToken wsu:Id="UsernameToken-19"><wsse:Username>scott</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">'$hashpass'</wsse:Password><wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">'$hashnonce'</wsse:Nonce><wsu:Created>'$timestamp'</wsu:Created></wsse:UsernameToken></wsse:Security></soapenv:Header>
<soapenv:Body>
<sear:searchRequest>
<sear:importantNumber>'$ENV2'</sear:importantNumber>
<sear:status>R</sear:status>
<sear:versionNumber>1.0</sear:versionNumber>
</sear:searchRequest>
</soapenv:Body>
</soapenv:Envelope>
ENDOFTEXT
# Send soap request and output to text file
curl -s --header "content-type: application/soap+xml" --data "@$checkdir/Request_$ENV.xml" https://$ENV.x.com:443/c/c/c/c/ > "$checkdir/Result_$ENV.txt"
# Grep for response
grep "Complete" $checkdir/Result_$ENV.txt | wc -l
exit
关于bash - DST破坏了bash脚本,如何为变量设置本地时间?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26628828/