If else IP match case with servername












0















I have multiple servers and want to set up a periodic check so that trapserver(s) has correct trapaddress(es). trapserver 1 as 10.10.10.1 and trapserver2 as 10.10.10.2. However the below script does not see $trapaddress value of 10.10.10.x with "10.10.10.x" as equal?



Not sure where I am going wrong?



Many thanks



#!/bin/bash
for LINE in $( cat /home/username/serverslist.txt )
do
SERVER=`(echo $LINE | awk -F : '{ print $1 }')`

echo " "

echo $SERVER
traphost=$( ssh $SERVER -t sudo cat /etc/traphost.txt )
trapaddress=$( ssh $SERVER -t sudo cat /etc/trapaddress.txt )
echo -e " Traphost is $traphost n Trapaddress is $trapaddress"

if [ $traphost = "trapserver1" ] && [ $trapaddress= "10.10.10.1" ] || [ $traphost = "trapserver2" ] && [ $trapaddress= "10.10.10.2" ] ;
then
echo "server is happy."
else
echo " $servername server has incorrect $trapaddress ."
fi
done









share|improve this question









New contributor




Syed Mazhar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • There are lots of things to comment here on but, taking just one, add a space before = in the tests. In other words, replace [ $trapaddress= "10.10.10.1" ] with [ "$trapaddress" = "10.10.10.1" ]

    – John1024
    2 hours ago






  • 1





    Also, in general, whenever you have a shell script error, a good first step is to cut and paste your code into shellcheck.net and correct the errors (important) and warnings (might be important) that it identifies. If you have trouble understanding its messages, then come here and ask.

    – John1024
    2 hours ago
















0















I have multiple servers and want to set up a periodic check so that trapserver(s) has correct trapaddress(es). trapserver 1 as 10.10.10.1 and trapserver2 as 10.10.10.2. However the below script does not see $trapaddress value of 10.10.10.x with "10.10.10.x" as equal?



Not sure where I am going wrong?



Many thanks



#!/bin/bash
for LINE in $( cat /home/username/serverslist.txt )
do
SERVER=`(echo $LINE | awk -F : '{ print $1 }')`

echo " "

echo $SERVER
traphost=$( ssh $SERVER -t sudo cat /etc/traphost.txt )
trapaddress=$( ssh $SERVER -t sudo cat /etc/trapaddress.txt )
echo -e " Traphost is $traphost n Trapaddress is $trapaddress"

if [ $traphost = "trapserver1" ] && [ $trapaddress= "10.10.10.1" ] || [ $traphost = "trapserver2" ] && [ $trapaddress= "10.10.10.2" ] ;
then
echo "server is happy."
else
echo " $servername server has incorrect $trapaddress ."
fi
done









share|improve this question









New contributor




Syed Mazhar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • There are lots of things to comment here on but, taking just one, add a space before = in the tests. In other words, replace [ $trapaddress= "10.10.10.1" ] with [ "$trapaddress" = "10.10.10.1" ]

    – John1024
    2 hours ago






  • 1





    Also, in general, whenever you have a shell script error, a good first step is to cut and paste your code into shellcheck.net and correct the errors (important) and warnings (might be important) that it identifies. If you have trouble understanding its messages, then come here and ask.

    – John1024
    2 hours ago














0












0








0








I have multiple servers and want to set up a periodic check so that trapserver(s) has correct trapaddress(es). trapserver 1 as 10.10.10.1 and trapserver2 as 10.10.10.2. However the below script does not see $trapaddress value of 10.10.10.x with "10.10.10.x" as equal?



Not sure where I am going wrong?



Many thanks



#!/bin/bash
for LINE in $( cat /home/username/serverslist.txt )
do
SERVER=`(echo $LINE | awk -F : '{ print $1 }')`

echo " "

echo $SERVER
traphost=$( ssh $SERVER -t sudo cat /etc/traphost.txt )
trapaddress=$( ssh $SERVER -t sudo cat /etc/trapaddress.txt )
echo -e " Traphost is $traphost n Trapaddress is $trapaddress"

if [ $traphost = "trapserver1" ] && [ $trapaddress= "10.10.10.1" ] || [ $traphost = "trapserver2" ] && [ $trapaddress= "10.10.10.2" ] ;
then
echo "server is happy."
else
echo " $servername server has incorrect $trapaddress ."
fi
done









share|improve this question









New contributor




Syed Mazhar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I have multiple servers and want to set up a periodic check so that trapserver(s) has correct trapaddress(es). trapserver 1 as 10.10.10.1 and trapserver2 as 10.10.10.2. However the below script does not see $trapaddress value of 10.10.10.x with "10.10.10.x" as equal?



Not sure where I am going wrong?



Many thanks



#!/bin/bash
for LINE in $( cat /home/username/serverslist.txt )
do
SERVER=`(echo $LINE | awk -F : '{ print $1 }')`

echo " "

echo $SERVER
traphost=$( ssh $SERVER -t sudo cat /etc/traphost.txt )
trapaddress=$( ssh $SERVER -t sudo cat /etc/trapaddress.txt )
echo -e " Traphost is $traphost n Trapaddress is $trapaddress"

if [ $traphost = "trapserver1" ] && [ $trapaddress= "10.10.10.1" ] || [ $traphost = "trapserver2" ] && [ $trapaddress= "10.10.10.2" ] ;
then
echo "server is happy."
else
echo " $servername server has incorrect $trapaddress ."
fi
done






shell scripting ip






share|improve this question









New contributor




Syed Mazhar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Syed Mazhar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 hours ago









ilkkachu

59.4k893167




59.4k893167






New contributor




Syed Mazhar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









Syed MazharSyed Mazhar

1




1




New contributor




Syed Mazhar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Syed Mazhar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Syed Mazhar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • There are lots of things to comment here on but, taking just one, add a space before = in the tests. In other words, replace [ $trapaddress= "10.10.10.1" ] with [ "$trapaddress" = "10.10.10.1" ]

    – John1024
    2 hours ago






  • 1





    Also, in general, whenever you have a shell script error, a good first step is to cut and paste your code into shellcheck.net and correct the errors (important) and warnings (might be important) that it identifies. If you have trouble understanding its messages, then come here and ask.

    – John1024
    2 hours ago



















  • There are lots of things to comment here on but, taking just one, add a space before = in the tests. In other words, replace [ $trapaddress= "10.10.10.1" ] with [ "$trapaddress" = "10.10.10.1" ]

    – John1024
    2 hours ago






  • 1





    Also, in general, whenever you have a shell script error, a good first step is to cut and paste your code into shellcheck.net and correct the errors (important) and warnings (might be important) that it identifies. If you have trouble understanding its messages, then come here and ask.

    – John1024
    2 hours ago

















There are lots of things to comment here on but, taking just one, add a space before = in the tests. In other words, replace [ $trapaddress= "10.10.10.1" ] with [ "$trapaddress" = "10.10.10.1" ]

– John1024
2 hours ago





There are lots of things to comment here on but, taking just one, add a space before = in the tests. In other words, replace [ $trapaddress= "10.10.10.1" ] with [ "$trapaddress" = "10.10.10.1" ]

– John1024
2 hours ago




1




1





Also, in general, whenever you have a shell script error, a good first step is to cut and paste your code into shellcheck.net and correct the errors (important) and warnings (might be important) that it identifies. If you have trouble understanding its messages, then come here and ask.

– John1024
2 hours ago





Also, in general, whenever you have a shell script error, a good first step is to cut and paste your code into shellcheck.net and correct the errors (important) and warnings (might be important) that it identifies. If you have trouble understanding its messages, then come here and ask.

– John1024
2 hours ago










1 Answer
1






active

oldest

votes


















1














First, I assume the lack of space before the = in [ $trapaddress= "10.10.10.1" ] is a typo. If it weren't you'd immediately get an error from [.



Second, you have a problem in the relation between the && and || operators. In the shell, they are processed left to right, so your condition doesn't do what you want it to do. In fact, this:



if cond1 && cond2 || cond3 && cond4; then ... 


is more like this:



if ((cond1 && cond2) || cond3) && cond4; then ... 


Notably, it requires cond4 to be true. What you want is either



if ( ... && ...) || ( ... && ... ); then ...


or



if [[ ... && ... || ... && ... ]]; then ...


(Within [[ .. ]] they do have the usual relative precedence.)



If you want, you could concatenate the variables to make the comparison easier, assuming they never contain e.g. a slash:



if [[ "$traphost/$trapaddress" = "trapserver1/10.10.10.1" ||
"$traphost/$trapaddress" = "trapserver2/10.10.10.2" ]]; then ...


Note that you want to quote the variables to avoid word splitting, see When is double-quoting necessary?






share|improve this answer


























  • I have changed to this however I am experiencing the same issue. The problem is that although the value of $trapaddress echoing as 10.10.10.1 but if condition does not match it with "10.10.10.1", which is really strange and goes to else condition, instead of then. if [[ "$traphost/$trapaddress" = "trapserver1*/10.10.10.1" || "$traphost/$trapaddress" = "trapserver2*/10.10.10.2" ]]; then ...

    – Syed Mazhar
    2 hours ago











  • @SyedMazhar, ah, right. Check that you don't have any traiiling whitespace or such in there. Put something like printf "host <%s> addr <%s>n" "$traphost" "$trapaddress" there in the script to see that. Or even look at the file itself to see any invisible characters. od -c /etc/trapaddress.txt or such should do. If there's anything to see edit your question to add it in, the comments don't really lend well for presenting code and output like that

    – ilkkachu
    2 hours ago













  • True, there seems to be issue with line 1 changed.. is there a way I can transfer $IP value without just the IP address and remove any whitespaces? will look further. thanks output was 1c1 < 10.10.10.1 --- > 10.10.10.1

    – Syed Mazhar
    1 hour ago













  • @SyedMazhar, there are probably better ways, but you already have used awk, so you could use it here, too. awk '{ print $1 }' /etc/trapaddress.txt would print just the first field, removing extra whitespace. Or tr -dc '0-9.' /etc/trapaddress.txt, it would remove anything but digits and dots.

    – ilkkachu
    1 hour ago











  • tr -dc '0-9.' worked perfectly , you are a star. Many thanks Sir. awk '{print $3 }' | tr -dc '0-9.'

    – Syed Mazhar
    1 hour ago













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Syed Mazhar is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501709%2fif-else-ip-match-case-with-servername%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














First, I assume the lack of space before the = in [ $trapaddress= "10.10.10.1" ] is a typo. If it weren't you'd immediately get an error from [.



Second, you have a problem in the relation between the && and || operators. In the shell, they are processed left to right, so your condition doesn't do what you want it to do. In fact, this:



if cond1 && cond2 || cond3 && cond4; then ... 


is more like this:



if ((cond1 && cond2) || cond3) && cond4; then ... 


Notably, it requires cond4 to be true. What you want is either



if ( ... && ...) || ( ... && ... ); then ...


or



if [[ ... && ... || ... && ... ]]; then ...


(Within [[ .. ]] they do have the usual relative precedence.)



If you want, you could concatenate the variables to make the comparison easier, assuming they never contain e.g. a slash:



if [[ "$traphost/$trapaddress" = "trapserver1/10.10.10.1" ||
"$traphost/$trapaddress" = "trapserver2/10.10.10.2" ]]; then ...


Note that you want to quote the variables to avoid word splitting, see When is double-quoting necessary?






share|improve this answer


























  • I have changed to this however I am experiencing the same issue. The problem is that although the value of $trapaddress echoing as 10.10.10.1 but if condition does not match it with "10.10.10.1", which is really strange and goes to else condition, instead of then. if [[ "$traphost/$trapaddress" = "trapserver1*/10.10.10.1" || "$traphost/$trapaddress" = "trapserver2*/10.10.10.2" ]]; then ...

    – Syed Mazhar
    2 hours ago











  • @SyedMazhar, ah, right. Check that you don't have any traiiling whitespace or such in there. Put something like printf "host <%s> addr <%s>n" "$traphost" "$trapaddress" there in the script to see that. Or even look at the file itself to see any invisible characters. od -c /etc/trapaddress.txt or such should do. If there's anything to see edit your question to add it in, the comments don't really lend well for presenting code and output like that

    – ilkkachu
    2 hours ago













  • True, there seems to be issue with line 1 changed.. is there a way I can transfer $IP value without just the IP address and remove any whitespaces? will look further. thanks output was 1c1 < 10.10.10.1 --- > 10.10.10.1

    – Syed Mazhar
    1 hour ago













  • @SyedMazhar, there are probably better ways, but you already have used awk, so you could use it here, too. awk '{ print $1 }' /etc/trapaddress.txt would print just the first field, removing extra whitespace. Or tr -dc '0-9.' /etc/trapaddress.txt, it would remove anything but digits and dots.

    – ilkkachu
    1 hour ago











  • tr -dc '0-9.' worked perfectly , you are a star. Many thanks Sir. awk '{print $3 }' | tr -dc '0-9.'

    – Syed Mazhar
    1 hour ago


















1














First, I assume the lack of space before the = in [ $trapaddress= "10.10.10.1" ] is a typo. If it weren't you'd immediately get an error from [.



Second, you have a problem in the relation between the && and || operators. In the shell, they are processed left to right, so your condition doesn't do what you want it to do. In fact, this:



if cond1 && cond2 || cond3 && cond4; then ... 


is more like this:



if ((cond1 && cond2) || cond3) && cond4; then ... 


Notably, it requires cond4 to be true. What you want is either



if ( ... && ...) || ( ... && ... ); then ...


or



if [[ ... && ... || ... && ... ]]; then ...


(Within [[ .. ]] they do have the usual relative precedence.)



If you want, you could concatenate the variables to make the comparison easier, assuming they never contain e.g. a slash:



if [[ "$traphost/$trapaddress" = "trapserver1/10.10.10.1" ||
"$traphost/$trapaddress" = "trapserver2/10.10.10.2" ]]; then ...


Note that you want to quote the variables to avoid word splitting, see When is double-quoting necessary?






share|improve this answer


























  • I have changed to this however I am experiencing the same issue. The problem is that although the value of $trapaddress echoing as 10.10.10.1 but if condition does not match it with "10.10.10.1", which is really strange and goes to else condition, instead of then. if [[ "$traphost/$trapaddress" = "trapserver1*/10.10.10.1" || "$traphost/$trapaddress" = "trapserver2*/10.10.10.2" ]]; then ...

    – Syed Mazhar
    2 hours ago











  • @SyedMazhar, ah, right. Check that you don't have any traiiling whitespace or such in there. Put something like printf "host <%s> addr <%s>n" "$traphost" "$trapaddress" there in the script to see that. Or even look at the file itself to see any invisible characters. od -c /etc/trapaddress.txt or such should do. If there's anything to see edit your question to add it in, the comments don't really lend well for presenting code and output like that

    – ilkkachu
    2 hours ago













  • True, there seems to be issue with line 1 changed.. is there a way I can transfer $IP value without just the IP address and remove any whitespaces? will look further. thanks output was 1c1 < 10.10.10.1 --- > 10.10.10.1

    – Syed Mazhar
    1 hour ago













  • @SyedMazhar, there are probably better ways, but you already have used awk, so you could use it here, too. awk '{ print $1 }' /etc/trapaddress.txt would print just the first field, removing extra whitespace. Or tr -dc '0-9.' /etc/trapaddress.txt, it would remove anything but digits and dots.

    – ilkkachu
    1 hour ago











  • tr -dc '0-9.' worked perfectly , you are a star. Many thanks Sir. awk '{print $3 }' | tr -dc '0-9.'

    – Syed Mazhar
    1 hour ago
















1












1








1







First, I assume the lack of space before the = in [ $trapaddress= "10.10.10.1" ] is a typo. If it weren't you'd immediately get an error from [.



Second, you have a problem in the relation between the && and || operators. In the shell, they are processed left to right, so your condition doesn't do what you want it to do. In fact, this:



if cond1 && cond2 || cond3 && cond4; then ... 


is more like this:



if ((cond1 && cond2) || cond3) && cond4; then ... 


Notably, it requires cond4 to be true. What you want is either



if ( ... && ...) || ( ... && ... ); then ...


or



if [[ ... && ... || ... && ... ]]; then ...


(Within [[ .. ]] they do have the usual relative precedence.)



If you want, you could concatenate the variables to make the comparison easier, assuming they never contain e.g. a slash:



if [[ "$traphost/$trapaddress" = "trapserver1/10.10.10.1" ||
"$traphost/$trapaddress" = "trapserver2/10.10.10.2" ]]; then ...


Note that you want to quote the variables to avoid word splitting, see When is double-quoting necessary?






share|improve this answer















First, I assume the lack of space before the = in [ $trapaddress= "10.10.10.1" ] is a typo. If it weren't you'd immediately get an error from [.



Second, you have a problem in the relation between the && and || operators. In the shell, they are processed left to right, so your condition doesn't do what you want it to do. In fact, this:



if cond1 && cond2 || cond3 && cond4; then ... 


is more like this:



if ((cond1 && cond2) || cond3) && cond4; then ... 


Notably, it requires cond4 to be true. What you want is either



if ( ... && ...) || ( ... && ... ); then ...


or



if [[ ... && ... || ... && ... ]]; then ...


(Within [[ .. ]] they do have the usual relative precedence.)



If you want, you could concatenate the variables to make the comparison easier, assuming they never contain e.g. a slash:



if [[ "$traphost/$trapaddress" = "trapserver1/10.10.10.1" ||
"$traphost/$trapaddress" = "trapserver2/10.10.10.2" ]]; then ...


Note that you want to quote the variables to avoid word splitting, see When is double-quoting necessary?







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 hours ago

























answered 2 hours ago









ilkkachuilkkachu

59.4k893167




59.4k893167













  • I have changed to this however I am experiencing the same issue. The problem is that although the value of $trapaddress echoing as 10.10.10.1 but if condition does not match it with "10.10.10.1", which is really strange and goes to else condition, instead of then. if [[ "$traphost/$trapaddress" = "trapserver1*/10.10.10.1" || "$traphost/$trapaddress" = "trapserver2*/10.10.10.2" ]]; then ...

    – Syed Mazhar
    2 hours ago











  • @SyedMazhar, ah, right. Check that you don't have any traiiling whitespace or such in there. Put something like printf "host <%s> addr <%s>n" "$traphost" "$trapaddress" there in the script to see that. Or even look at the file itself to see any invisible characters. od -c /etc/trapaddress.txt or such should do. If there's anything to see edit your question to add it in, the comments don't really lend well for presenting code and output like that

    – ilkkachu
    2 hours ago













  • True, there seems to be issue with line 1 changed.. is there a way I can transfer $IP value without just the IP address and remove any whitespaces? will look further. thanks output was 1c1 < 10.10.10.1 --- > 10.10.10.1

    – Syed Mazhar
    1 hour ago













  • @SyedMazhar, there are probably better ways, but you already have used awk, so you could use it here, too. awk '{ print $1 }' /etc/trapaddress.txt would print just the first field, removing extra whitespace. Or tr -dc '0-9.' /etc/trapaddress.txt, it would remove anything but digits and dots.

    – ilkkachu
    1 hour ago











  • tr -dc '0-9.' worked perfectly , you are a star. Many thanks Sir. awk '{print $3 }' | tr -dc '0-9.'

    – Syed Mazhar
    1 hour ago





















  • I have changed to this however I am experiencing the same issue. The problem is that although the value of $trapaddress echoing as 10.10.10.1 but if condition does not match it with "10.10.10.1", which is really strange and goes to else condition, instead of then. if [[ "$traphost/$trapaddress" = "trapserver1*/10.10.10.1" || "$traphost/$trapaddress" = "trapserver2*/10.10.10.2" ]]; then ...

    – Syed Mazhar
    2 hours ago











  • @SyedMazhar, ah, right. Check that you don't have any traiiling whitespace or such in there. Put something like printf "host <%s> addr <%s>n" "$traphost" "$trapaddress" there in the script to see that. Or even look at the file itself to see any invisible characters. od -c /etc/trapaddress.txt or such should do. If there's anything to see edit your question to add it in, the comments don't really lend well for presenting code and output like that

    – ilkkachu
    2 hours ago













  • True, there seems to be issue with line 1 changed.. is there a way I can transfer $IP value without just the IP address and remove any whitespaces? will look further. thanks output was 1c1 < 10.10.10.1 --- > 10.10.10.1

    – Syed Mazhar
    1 hour ago













  • @SyedMazhar, there are probably better ways, but you already have used awk, so you could use it here, too. awk '{ print $1 }' /etc/trapaddress.txt would print just the first field, removing extra whitespace. Or tr -dc '0-9.' /etc/trapaddress.txt, it would remove anything but digits and dots.

    – ilkkachu
    1 hour ago











  • tr -dc '0-9.' worked perfectly , you are a star. Many thanks Sir. awk '{print $3 }' | tr -dc '0-9.'

    – Syed Mazhar
    1 hour ago



















I have changed to this however I am experiencing the same issue. The problem is that although the value of $trapaddress echoing as 10.10.10.1 but if condition does not match it with "10.10.10.1", which is really strange and goes to else condition, instead of then. if [[ "$traphost/$trapaddress" = "trapserver1*/10.10.10.1" || "$traphost/$trapaddress" = "trapserver2*/10.10.10.2" ]]; then ...

– Syed Mazhar
2 hours ago





I have changed to this however I am experiencing the same issue. The problem is that although the value of $trapaddress echoing as 10.10.10.1 but if condition does not match it with "10.10.10.1", which is really strange and goes to else condition, instead of then. if [[ "$traphost/$trapaddress" = "trapserver1*/10.10.10.1" || "$traphost/$trapaddress" = "trapserver2*/10.10.10.2" ]]; then ...

– Syed Mazhar
2 hours ago













@SyedMazhar, ah, right. Check that you don't have any traiiling whitespace or such in there. Put something like printf "host <%s> addr <%s>n" "$traphost" "$trapaddress" there in the script to see that. Or even look at the file itself to see any invisible characters. od -c /etc/trapaddress.txt or such should do. If there's anything to see edit your question to add it in, the comments don't really lend well for presenting code and output like that

– ilkkachu
2 hours ago







@SyedMazhar, ah, right. Check that you don't have any traiiling whitespace or such in there. Put something like printf "host <%s> addr <%s>n" "$traphost" "$trapaddress" there in the script to see that. Or even look at the file itself to see any invisible characters. od -c /etc/trapaddress.txt or such should do. If there's anything to see edit your question to add it in, the comments don't really lend well for presenting code and output like that

– ilkkachu
2 hours ago















True, there seems to be issue with line 1 changed.. is there a way I can transfer $IP value without just the IP address and remove any whitespaces? will look further. thanks output was 1c1 < 10.10.10.1 --- > 10.10.10.1

– Syed Mazhar
1 hour ago







True, there seems to be issue with line 1 changed.. is there a way I can transfer $IP value without just the IP address and remove any whitespaces? will look further. thanks output was 1c1 < 10.10.10.1 --- > 10.10.10.1

– Syed Mazhar
1 hour ago















@SyedMazhar, there are probably better ways, but you already have used awk, so you could use it here, too. awk '{ print $1 }' /etc/trapaddress.txt would print just the first field, removing extra whitespace. Or tr -dc '0-9.' /etc/trapaddress.txt, it would remove anything but digits and dots.

– ilkkachu
1 hour ago





@SyedMazhar, there are probably better ways, but you already have used awk, so you could use it here, too. awk '{ print $1 }' /etc/trapaddress.txt would print just the first field, removing extra whitespace. Or tr -dc '0-9.' /etc/trapaddress.txt, it would remove anything but digits and dots.

– ilkkachu
1 hour ago













tr -dc '0-9.' worked perfectly , you are a star. Many thanks Sir. awk '{print $3 }' | tr -dc '0-9.'

– Syed Mazhar
1 hour ago







tr -dc '0-9.' worked perfectly , you are a star. Many thanks Sir. awk '{print $3 }' | tr -dc '0-9.'

– Syed Mazhar
1 hour ago












Syed Mazhar is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Syed Mazhar is a new contributor. Be nice, and check out our Code of Conduct.













Syed Mazhar is a new contributor. Be nice, and check out our Code of Conduct.












Syed Mazhar is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501709%2fif-else-ip-match-case-with-servername%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

宮崎県

How to check if a shell is login/interactive/batch

濃尾地震