Monday, May 11, 2015

Linux - remote ssh and execute commands


The below script will make copy of the file. insert the desired line as the second line.

for LINE in $(cat machines.csv)
do
        ssh root@$LINE "cp /etc/xyz.conf /etc/xyz.conf.old; sed -i '2i nameserver 10.xx.xxx.xxx' /etc/xyz.conf"
done

Grep and Awk Tips and Examples

Remove duplicates and retain the order
awk ' !x[$0]++' 1.txt > 2.txt


Grep regex to find strings only contains alphabets 

grep "^[a-zA-Z]*$"

alphabets, numbers and + (this is typical for a query string)
 grep "^[a-zA-Z0-9+]*$"



Grep few characters after match - this case 15 characters after match - for getting the various calls to www.xyz.com

grep -E -o "www.xyz.com \"GET \/.{0,15}" httpd2-xyz.com.access-log.1370347200

-o - to show only the match
-E - extended regular expression


Grep - escape double quote
\"

-as in above example, before GET



Various calls goes to www.xyz.com from apache logs
grep -E -o "www.xyz.com \"GET \/.{0,15}" httpd2-xyz.com.access-log.1370347200 | awk '{ print $3}' | awk '{ if(match($0,"?")) { split($0,a,"?"); print a[1]}}' 



Grep for lines ending with q= - from the logs which has no query param
grep '.*q=$'


Sed Tips

Remove empty lines

sed '/^$/d'


Replace a string

sed 's/original/new/' - only first occurence of the line
sed 's/original/new/g' - all occurences

JMeter JSON Path extractor example

1.      Json path extractor – this comes from JMeterPlugins-ExtrasLibs.

Issue to use array of response by referencing _1 and _2 all resolved by moving from v1.1.3 to v1.2.1
Online json path extractor - http://jsonpath.curiousconcept.com/


JMeter Response Assertion - matching for pattern

1.      Response assertion matching values in response text to validate the success of the request

a.      To validate a value from input, for example SSN value, <SSN>${ssn}</SSN>
b.      For any value in the response, but with a pattern
                                                    i.     <SSN>.*</SSN>
c.      <!- - no errors on the page -->


JMeter Adding a random number to input, within range of

${__Random(100000,999999)}

JMeter Socket read timeouts from jmeter

Add “HTTP Request Defaults” and assign timeouts for connect and Response




JMeter Regex operators in response


JMeter Using different forms of date, by means of user defined variables

Add a user defined variable


JMeter Setting user defined cookie

a.      Add a BeanShell PreProcessor in the HTTP Request where you need to use the cookie
import org.apache.jmeter.protocol.http.control.CookieManager;
  import org.apache.jmeter.protocol.http.control.Cookie;
  CookieManager manager = sampler.getCookieManager();
  Cookie cookie = new Cookie("CRM_Cookies","CRM_EmplId=${employeeId}[$$]CRM_CompanyID=${company}[$$]CRM_Admin=Y","reportsuiqeb.hrpassport.com","/",false,0);

  manager.add(cookie);

JMeter Passing variables between thread group

a.      Add a “BeanShell Assertion” in Origin thread group
Inside the script Set as property
${__setProperty(preGoto,${Goto})};
${__setProperty(preSunQ,${SunQ})};
b.      Use it in the target thread group referencing the property names
${__property(preGoto)}

${__property(preSunQ)}