1 Comparing Remote Files without Breaking a Sweat
2 ===============================================
3 :author: Aaron Ball
4 :email: nullspoon@iohq.net
5
6
7 == {doctitle}
8
9 Today I needed to compare a relatively large list of remote files to a local
10 copy. Yep, you guessed it... it's auditing time again!
11
12 Here's what my setup looks like.
13
14 From various other commands (lots of pipes), I parsed my kludgy list of server
15 metadata down to a list of servers that I needed to check. On that note,
16 despite the really terrible methods we're using to track this kind of
17 information, I really do enjoy the challenge of having to write a bash or perl
18 one liner to parse the output of some badly formatted unknown space quantity
19 delimited data whose case is most likely wrong, trimming multiple spaces,
20 fixing the case, grabbing the columns I need, and redirecting to a file for
21 later use. My thanks to the folks a la GNU for cat, tr, cut, grep, and still
22 again tr.
23
24 Anyways, back to the topic at hand. We now have a list of server hostnames, one
25 per line. As they say, "Hey guys, watch this!"
26
27 ----
28 for s in `cat list.txt`; do echo -e "\n\n### $s" >> diff.txt; diff <( ssh root@$s cat /etc/sudoers ) sudoers >> diff.txt; done
29 ----
30
31 So what have we here?
32
33 Firstly, we start up a bash for loop. This will make $s equal to the
34 name of each of the servers as we loop to them.
35
36 Now, inside of the loop we first echo the server's name ( $s ) so we've
37 got a marker to tell us which diff we're looking at. After that, the fun
38 happens.
39
40 ----
41 diff <( ssh root@$s cat /etc/sudoers ) sudoers >> diff.txt
42 ----
43
44 Here, we are running the diff command to diff the remote file ( +<( ssh root@$s
45 cat /etc/sudoers )+ ) with the local file ( sudoers ), and we are redirecting
46 the output to diff.txt. What's neat about this (I think it's neat at least) is
47 the +<()+ bit. This is called
48 http://www.gnu.org/software/bash/manual/bashref.html#Process-Substitution[process
49 substitution]. It allows us to take the output of a command and use it as if it
50 were the contents of a file.
51
52
53 Category:Bash
54 Category:Linux
55
56
57 // vim: set syntax=asciidoc:
|