Mailing List Archive

banning in one line command
Dear all,

I have Varnish 4.1 and I´m trying to run in one single line command a
ban for a particular website. If I run this:

root@myserver [~] # varnishadm -T :6082 -S /etc/varnish/secret

varnish> ban req.http.host ~ www.myserver.com
200

I get the 200 code. However If I run it in a single line I get no 200
return code:

root@myserver [~] # varnishadm -T :6082 -S /etc/varnish/secret 'ban
req.http.host ~ www.myserver.com'

I get nothing. If I try double quotes I get the same...

I googled around and I find people asking the same but no solutions.

What am I doing wrong?

Thanks!

Miguel

_______________________________________________
varnish-misc mailing list
varnish-misc@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
Re: banning in one line command [ In reply to ]
On Wed, Feb 1, 2017 at 3:31 PM, Miguel González
<miguel_3_gonzalez@yahoo.es> wrote:
> Dear all,
>
> I have Varnish 4.1 and I´m trying to run in one single line command a
> ban for a particular website. If I run this:
>
> root@myserver [~] # varnishadm -T :6082 -S /etc/varnish/secret
>
> varnish> ban req.http.host ~ www.myserver.com
> 200
>
> I get the 200 code. However If I run it in a single line I get no 200
> return code:
>
> root@myserver [~] # varnishadm -T :6082 -S /etc/varnish/secret 'ban
> req.http.host ~ www.myserver.com'

You probably don't need the -T arg for a local access.

> I get nothing. If I try double quotes I get the same...
>
> I googled around and I find people asking the same but no solutions.
>
> What am I doing wrong?

Nothing wrong: what you are seeing in varnishadm's interactive shell
is the response status from varnish (see man varnish-cli) but when you
use varnishadm as a scripting command, only the actual output is
shown.

Instead of relying on the status code of the varnish-cli command, use
the exit status of the varnishadm command.

if ! varnishadm [...]
then
echo "varnishadm just failed" >&2
exit 42
fi

Dridi

_______________________________________________
varnish-misc mailing list
varnish-misc@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
Re: banning in one line command [ In reply to ]
On 02/01/17 4:25 PM, Dridi Boukelmoune wrote:
> On Wed, Feb 1, 2017 at 3:31 PM, Miguel González
> <miguel_3_gonzalez@yahoo.es> wrote:
>> Dear all,
>>
>> I have Varnish 4.1 and I´m trying to run in one single line command a
>> ban for a particular website. If I run this:
>>
>> root@myserver [~] # varnishadm -T :6082 -S /etc/varnish/secret
>>
>> varnish> ban req.http.host ~ www.myserver.com
>> 200
>>
>> I get the 200 code. However If I run it in a single line I get no 200
>> return code:
>>
>> root@myserver [~] # varnishadm -T :6082 -S /etc/varnish/secret 'ban
>> req.http.host ~ www.myserver.com'
>
> You probably don't need the -T arg for a local access.
>
>> I get nothing. If I try double quotes I get the same...
>>
>> I googled around and I find people asking the same but no solutions.
>>
>> What am I doing wrong?
>
> Nothing wrong: what you are seeing in varnishadm's interactive shell
> is the response status from varnish (see man varnish-cli) but when you
> use varnishadm as a scripting command, only the actual output is
> shown.
>

Thanks for answering!

What do you mean with the actual output? I get an empty string...

Is there any manual or information about this in the wiki?

Thanks

_______________________________________________
varnish-misc mailing list
varnish-misc@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
Re: banning in one line command [ In reply to ]
> What do you mean with the actual output? I get an empty string...

For commands like param.show or backend.list that give you an output.

> Is there any manual or information about this in the wiki?

Please double check the varnishstat manual, if it's not mentioned let
me know and will push a docfix.

Dridi

_______________________________________________
varnish-misc mailing list
varnish-misc@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
Re: banning in one line command [ In reply to ]
On 02/01/17 5:24 PM, Miguel González wrote:
> On 02/01/17 5:08 PM, Dridi Boukelmoune wrote:
>>> What do you mean with the actual output? I get an empty string...
>>
>> For commands like param.show or backend.list that give you an output.
>>
>>> Is there any manual or information about this in the wiki?
>>
>> Please double check the varnishstat manual, if it's not mentioned let
>> me know and will push a docfix.
>
> you mean varnishadm manual, this one:
>
> https://varnish-cache.org/docs/4.1/reference/varnishadm.html
>
> ?
>
> My experience is that using a singleliner doesn´t work, cache is not
> banned. I thought It was the empty string output but considering your
> answer and the manual it supposedly works but it´s not actually banning
> the cache.

using quotes an after the example in the manual:

echo "ban req.http.host ~ myserver.com" | varnishadm -S /etc/varnish/secret
200


Before I was getting this error when not using commands for the echo
command (as showed in the example in the manual):

106
expected conditional (~, !~, == or !=) got "/root"

apparently this error is dued to the echo command and the use of
conditionals as ~

Thanks!

Miguel



_______________________________________________
varnish-misc mailing list
varnish-misc@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
Re: banning in one line command [ In reply to ]
On 02/02/17 1:05 PM, Dridi Boukelmoune wrote:
>>> you mean varnishadm manual, this one:
>>>
>>> https://varnish-cache.org/docs/4.1/reference/varnishadm.html
>>>
>>> ?
>
> Yes, I was looking at something else related to varnishstat and endup
> mixing things up. That's the manual I'm referring to.
>
>>> My experience is that using a singleliner doesn´t work, cache is not
>>> banned. I thought It was the empty string output but considering your
>>> answer and the manual it supposedly works but it´s not actually banning
>>> the cache.
>
> It works for me:
>
> $ cat ban_test.sh
> #!/bin/sh
>
> set -e
>
> if varnishadm ban "$1"
> then
> echo "Ban added."
> varnishadm ban.list
> else
> echo "Failed to add ban." >&2
> exit 1
> fi
>
> $ ./ban_test.sh "req.url ~ /foo"
>
> Ban added.
> Present bans:
> 1486036564.163640 0 - req.url ~ /foo
> 1486036505.008676 0 C
>
> $ ./ban_test.sh "req.url =="
> Unknown request.
> Type 'help' for more info.
> Too few parameters
>
> Command failed with error code 104
> Failed to add ban.
>
>> using quotes an after the example in the manual:
>>
>> echo "ban req.http.host ~ myserver.com" | varnishadm -S /etc/varnish/secret
>> 200
>
> You shouldn't need to specify -S for a local access with varnishadm.
>
>> Before I was getting this error when not using commands for the echo
>> command (as showed in the example in the manual):
>>
>> 106
>> expected conditional (~, !~, == or !=) got "/root"
>>
>> apparently this error is dued to the echo command and the use of
>> conditionals as ~
>
> I suspect you were getting a shell expansion of ~ to the home
> directory of the root user, hence the /root.
>
> Dridi

Yes, i did :)

Many thanks for your throughly answer!

Miguel

_______________________________________________
varnish-misc mailing list
varnish-misc@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc