PrintLogo

One off Administration and Testing Scripts Using Vi




If you have a list of servers, you can use the substitution feature in vi to quickly turn that list into a test script. For instance, if I want to test a list of domains that looks like this:

domain1.com
domain2.com
domain3.com
domain4.com
domain5.com

I can insert text before and after the domain name. Here is the ping test I want to do:

[usr-1@srv-1 ~]$ ping -c 1 -n domain1.com | grep icmp
64 bytes from 69.57.130.27: icmp_seq=0 ttl=47 time=64.8 ms
[usr-1@srv-1 ~]$ 

Let's combine these two. Hit : and enter the substitute command below:

domain1.com
domain2.com
domain3.com
domain4.com
domain5.com
~                                                                               
~                                                                               
:%s/^/ping -c 1 -n /

The ^ marks the beginning of the line. Now we have:

ping -c 1 -n domain1.com
ping -c 1 -n domain2.com
ping -c 1 -n domain3.com
ping -c 1 -n domain4.com
ping -c 1 -n domain5.com
~                                                                               
~                                                                               
5 substitutions on 5 lines  

Now, let's substitute at the end of the line using the $ match:

ping -c 1 -n domain1.com
ping -c 1 -n domain2.com
ping -c 1 -n domain3.com
ping -c 1 -n domain4.com
ping -c 1 -n domain5.com
~                                                                               
~                                                                               
:%s/$/ | grep icmp/

We have our batch file / shell script:

ping -c 1 -n domain1.com | grep icmp 
ping -c 1 -n domain2.com | grep icmp 
ping -c 1 -n domain3.com | grep icmp 
ping -c 1 -n domain4.com | grep icmp 
ping -c 1 -n domain5.com | grep icmp 
~                                                                               
~                                                                               
5 substitutions on 5 lines  





This article comes from NetAdminTools:
http://www.netadmintools.com/

The URL for this story is:
http://www.netadmintools.com/art413.html

Copyright 1997-2007 NetAdminTools.com. Read our Terms of Use.