Sometimes you just need a set of tools to help you automate certain functions of your daily routine. For me it’s running builds and verifying that things are working as I expect.
Lately, I’ve needed a way to tell if certain domains/pages are available or not. I have bash scripts to do this, but they are bulky and confusing to read when you only modify them every couple months. There has to be a better way…
Enter: Python.
Of course this makes sense, I can easily have one of my bash scripts simply run a python “availability evaluator” and then check the status code from there.
In your bash script you’d only need:
[pastacode lang=”bash” manual=”python%20script.py%20http%3A%2F%2Fstuff.com%2Fthings” message=”” highlight=”” provider=”manual”/]
Then in your python script:
[pastacode lang=”python” manual=”%23!%2Fusr%2Flocal%2Fbin%2Fpython%0A%0Aimport%20requests%2C%20sys%0A%0A%23%20set%20the%20url%20from%20the%20argument%20you%20passed%20in%20(URL)%0Aurl%20%3D%20sys.argv%5B1%5D%0A%0A%23%20check%20the%20url%0Aresponse%20%3D%20requests.get(url)%0A%0A%23%20output%20the%20response%20code%0Aprint%20response.status_code” message=”” highlight=”” provider=”manual”/]
It works by using the request library to fetch the url and do some parsing of the response header automatically. This makes it easy to test when most of the time you are simply looking for the response code instead of the entire header.
So now you have something that will check a url and output the response code only. With this it’s much easier to check in bash whether the response code is the code you want. Like this:
[pastacode lang=”bash” manual=”responsecode%3D%60python%20~%2FPath%2Fto%2Fcheck-aow.py%20%24sitesdir%60%0Aif%20%5B%5B%20%24responsecode%20%3D%3D%20%22200%22%20%5D%5D%3B%20then%0A%09shouldaow%3DY%0Afi” message=”” highlight=”” provider=”manual”/]
With this you should be able to have something that takes input from your bash script and outputs a response as to whether or not that url is available.