Monday, April 06, 2009

Avoiding all the banners

Sometime you need to automate your activities by ssh into a remote host and sudo to a particular user to execute a command. Although all the authentication has been setup to be passwordless, the output include those extra banners (/etc/motd and /etc/issue) which you normally want to get rid of.

There is one nice trick that you may want to adopt. By crafting a unique string in your script, you can use that as your marker to separate the banners from your command output. So your ssh sudo output can pipe through awk to separate out unwanted banners.

unique="=-a-b-c-="

ssh chihung@$remote "sudo su - someuser -c 'echo $unique;somecmd'" 2>&1 | \
    awk '/^'$unique'$/ { start=1; next } start==1 { print }'

Make sure your $unique get substituted in your current shell by enclosing them in double quotes. In this approach, you do not have to worry about how the banners look like and you do not have to do grep -v some-banner-string

Labels: ,

0 Comments:

Post a Comment

<< Home