On Sat, 3 Dec 2016 15:29, Carlos E. R. <robin.listas@...> wrote:
On 2016-12-03 14:59, Carlos E. R. wrote:
On 2016-12-03 09:07, Per Jessen wrote:
Carlos E. R. wrote:
I just have to finish the cronjob script.
I have a coding problem.
I obtain the current IP this way from the router:
/usr/local/bin/GetIpFromRouter.exp > /run/DDNS/router_output.log CURRENTIP=`grep PPPoE /run/DDNS/router_output.log | awk '{ print $10}'`
The first line is an expect script that does a telnet to the router, issues the command "wan show", which produces a text that includes the IP. The second line extracts the IP from the line, like this one:
N/A 1 6 ppp0.1 PPPoE Disable Enable Disable Connected 192.168.1.1\r
The problem is that as the router ends the line in non unix format, the variable awk gets is "192.168.1.1^M".
How can I remove that "^M"?
It produces several errors in the script.
I don't know awk, so I don't know how the awk code could be modified:
echo line | awk '{ print $10}'
cut? I don't see how to extract till the last char minus one.
Convert the entire telnet session to Linux format? dos2unix? No, does not work.
This is the code:
usr/local/bin/GetIpFromRouter.exp > /run/DDNS/router_output.log 2> /run/DDNS/router_output_err.log # remove ^M chars from eol /usr/bin/dos2unix /run/DDNS/router_output_err.log CURRENTIP=`grep PPPoE /run/DDNS/router_output.log | awk '{ print $10}'` 2> /run/DDNS/router_output_greperr.log
echo "Last IP: $LASTKNOWNIP" echo "Current IP: $CURRENTIP"
echo "Last IP: $LASTKNOWNIP Current IP: $CURRENTIP"
The last echo produces this output:
cer@Isengard:~> TrackIpAndUpdateDDNS dos2unix: converting file /run/DDNS/router_output_err.log to Unix format ... Last IP: 83.36.42.230 Current IP: 83.36.42.230 Current IP: 83.36.42.230 <<= cer@Isengard:~>
The text "Last IP ..." in the last line is overwritten with "Current IP..." because it contains a carriage return.
So the problem is in the awk code, it cuts too late.
sed? I don't know how to use it.
I could try appending an space to the line, perhaps... How?
Let's try "simple" first: [code of cron-script] # make sure LASTKNOWNIP is clean: tempip=`echo $LASTKNOWNIP | tr -d '\r'` LASTKNOWNIP=$tempip # get actual info from router, log errors /usr/local/bin/GetIpFromRouter.exp > /run/DDNS/router_output.log 2> /run/DDNS/router_output_err.log # grep out PPoE line, catch out wan ip, log errors CURRENTIP=`grep PPPoE /run/DDNS/router_output.log | tr -d '\r' | awk '{print $10}'` 2> /run/DDNS/router_output_greperr.log echo "Last IP: '$LASTKNOWNIP' Current IP: '$CURRENTIP'" [/code] that code (which is mostly what You'he shown) should give you: Last IP: '83.36.42.230' Current IP: '83.36.42.230' Ok, now what did I different: 1. Added cleanup of LASTKNOWNIP, most likely it still will have that added '\r' or '^M', lets remove that. 2. Removed dos2unix of the router_output_err.log, why the error log?? 3. Added cleanup to the output of grep, before awk. How did I do that cleanup? Using the most simple tool available: "tr" Cheers, have a fruitfull weekend, - Yamaban PS: Yes, it's seeing that single tree before the forest thing. Missing the obvious, because beeing to deep in the problem. Happens to every one now and then. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org