summaryrefslogtreecommitdiff
path: root/src/Linux:Comparing_Remote_with_Local.ascii
blob: c681a3aed448c1232614f23ca66054a617911130 (plain)
    1 Linux:Comparing Remote with Local
    2 =================================
    3 :author: Aaron Ball
    4 :email: nullspoon@iohq.net
    5 
    6 
    7 == {doctitle}
    8 
    9 Today I ran into quite the conundrum. I've been needing to compare the contents
   10 of a file on a remote server with the contents of a file on my local server
   11 over ssh. My original solution was to copy the file from one server to the
   12 other over sftp and compare their contents with diff.
   13 
   14 However, when you need to compare more than just the contents of a file, things
   15 can get really messy. Take for instance another situation where I needed to
   16 compare the contents of a directory. First, I needed to output the contents of
   17 a directory on BOTH servers into two files I had to create. Then I had to sftp
   18 into one server, copy it over to the other server, and run diff to compare
   19 their contents. Talk about complicated, yeah?
   20 
   21 All this copying and sftp-ing around frustrated me to the point where I wanted
   22 to find another solution. Sadly, my solution is a bit complicated for someone
   23 who doesn't know the linux command line super well, but at least it works and
   24 it works fast. I'll go slowly because if I don't, I won't get it myself either. 
   25 
   26 ----
   27 ssh username@ServerIP 'ls -1 /tmp' | diff <(ls -1 /tmp)
   28 ----
   29 
   30 Here's our example. The end result of this example is to get a comparison of
   31 the contents of a remote /tmp directory and the contents of our local /tmp
   32 directory.
   33 
   34 First things first, we have to run a command remotely to get the contents of
   35 said remote directory. To do this, we run simply
   36 
   37 ----
   38 ssh username@ServerIP 'ls -1 /tmp'
   39 ----
   40 
   41 That gets a list of the files and folders in the /tmp directory.  Specifically,
   42 the '-1' switch gives us one file or folder per line.
   43 
   44 Next up we pipe that into the diff command.
   45 
   46 For those of you who may not know about this functionality, piping basically
   47 takes the output of one command, and feeds it to another. In this case, we are
   48 taking the listed contents of a remote directory and feeding it to the diff
   49 command.  Now, we do this by using the following.
   50 
   51 ----
   52 ... | diff ...
   53 ----
   54 
   55 Basically, the diff command works by finding the difference between the first
   56 thing it is given and the second thing it is given. Generally speaking, diff
   57 works like the following. 
   58 
   59 ----
   60 diff <file1> <file2>
   61 ----
   62 
   63 In this case we are saying diff which means to substitute what was piped in
   64 with the -.
   65 
   66 Up to this point, we have the contents of our remote directory and we have run
   67 the diff command. All we need now is to give it the second input to compare our
   68 first to. This brings us to our final step getting the contents of a local
   69 directory.
   70 
   71 This is about one of the most common linux command line functions performed.
   72 However, due to the fact that we want to compare the contents of the directory
   73 with the contents of another directory, things get a bit more complicated
   74 sadly. Do accomplish this, we need to run a nested command.
   75 
   76 Ordinarily running ls -1 /tmp after a diff command would result in an error
   77 rather than giving us what we want. To substitute a command for a file and so
   78 compare the command's output, we need to encase it in <().  Our final piece of
   79 the command should look like this.
   80 
   81 ----
   82 <(ls -1 /tmp)
   83 ----
   84 
   85 This completes our command. If you try to run the entire thing, you should be
   86 asked for your password to the remote server. Upon entering your password, the
   87 command should run as expected, comparing the files and folders in the two
   88 directories.
   89 
   90 The final command again looks like this...
   91 
   92 ----
   93 ssh username@ServerIP 'ls -1 /tmp' | diff <(ls -1 /tmp)
   94 ----
   95 
   96 If you want to get really tricky, you can compare the contents of a
   97 remote file and the contents of a local file. We'll take httpd.conf for
   98 instance. 
   99 
  100 ----
  101 ssh username@ServerIP 'cat /etc/httpd/conf/httpd.conf' | diff <(cat /etc/httpd/conf/httpd.conf)
  102 ----
  103 
  104 Hopefully that description wasn't too confusing. It's a complicated command to
  105 run (probably the worst I have ever used actually), but with some practice, it
  106 should become pretty easy if you understand how it works.
  107 
  108 Let me know if I didn't describe anything well enough and I will do my best to
  109 help out and update the post so it is more user friendly.
  110 
  111 Thanks for reading!
  112 
  113 
  114 Category:Linux
  115 Category:SSH
  116 
  117 
  118 // vim: set syntax=asciidoc:

Generated by cgit