• Servers and Tools

Running Remote Commands

From the class:  SSH

Oftentimes we're just using SSH like this, and that's going to drop us into a remote shell, just like we were running the machine locally.

But another thing you can do is to run one-time commands, and you can do that by providing a shell script that you'd like to run, in quotation marks, right after the SSH options.

So for example, if I wanted to print out whoami-- I'm going to actually run this command whoami-- and notice it'll ask me for my password again, so I'll type that in, and then it prints to my local standard output, the results of that command. So even though this command is running on the remote machine, the result is printed right here to my console.

Let's try a different one. This time, let's print out all of the system information. So I'm going to run the uname command and provide the dash a option, so that'll say, give me all the information about this remote system. OK. Unfortunately, we need to enter the password again, and once we do that, it'll run the command and give me the result, this standard output being this local console here.

One of the really awesome things with SSH is that it works hand in hand with I/O redirection and piping that we get from shell and Unix systems. So for example, let's say I want to echo something like hello world, so that will just echo out hello world to the console. I can pipe that output into SSH, and then that will become the standard input of the SSH shell commands that gets run on the remote machine.

So let's say, we'll do the same shell command that we had before, but this time the standard input is going to contain the words hello world. Now what I can do is print out the standard input by using cat. Now, normally cat takes a path to a file, but remember that it can also work with standard input. So what it will do now is just print out hello world to the console. So a little tricky there, but notice I just used cat and I didn't provide it any arguments, and that's because it's going to read from standard input-- standard input-- and that standard input is coming from the output of this command, and then I'm piping it right into SSH.

Now, if this is confusing to you, go back and watch the shell class, because I talk all about how these different techniques work in the shell. But this really illustrates one of the most powerful aspects of Unix, and we can use it with SSH and do a lot of powerful things as a result.

Now, let's do the same thing we just did, except we're going to write this output to a file. So now what I can do is say, I want to cat out hello world to a new file, which I'm going to call hello.txt. Once I enter in my password again and press Enter, I don't get anything back to standard output, so nothing actually gets printed here, but if we log in to the system, we should see that a new file was created called hello.txt, and we've taken standard input and just written it out to the file. So we should have a file with hello world printed inside of that file. Let's try it out.

So I'll just use SSH regularly this time, so I'll just hit up until I get there, and then we'll log in, and if we take a look at the home directory here, notice we have a file called hello.txt in our home directory, and if I take a look at the contents of that file, it should say hello world.

All right. Back in our local Mac, I've shown you so far how to use a pipe to feed into standard input on the remote machine, but we can also use the file redirection, so that's the less than and greater than characters to use files.

So let's create a file on our local machine here. I'm going to just call it hello world 2, and we'll create a file called hello.txt. So this is all on my local Mac-- there's the file-- and what I want to do is to send that file up as standard input. So this time around, I'll use SSH directly, and I'll use the same command here-- cat-- and then I'm going to create a file called hello2 instead of hello. But this time around, I'm going to feed into that command the hello.txt file, so I'm using regular file I/O redirection using the less than symbol to send this file in as standard input.

Again, I shouldn't get anything printed out here to standard output, because that's not how cat works-- it's redirected it to a file-- so let's try logging in and see whether the file exists. There it is at hello2.txt.

So now you've seen how we can use file I/O redirection and pipes to get information up to the server.

We can also go the other way. So let's say we want to get data from the server down to our actual local machine. Well, we can use I/O direction and pipes with a remote server as well. So if we use the ssh command, I'm going to run cat hello2.txt, and let's just make sure we understand the output here. That's going to print out hello world 2, and what I want to do is to take that output and write it to a file.

Even before we write it to a file, I could just pipe it into another command. So let's just say I want to pipe it into the cat command, and now it prints out. It's sort of redundant, but it's just to show you that we can take the output of SSH and pipe that into a command that we run locally on this machine.

Let's actually write it out to a file, though. So I can use the greater than symbol, and I'm going to write it out to hello2.txt, and when I type my password properly and it completes, now we have a file called hello2.txt on our local machine, and this actually came from the remote server.

One of the really cool things you can do is pipe the output of the tar command into SSH, and tar is used for creating a tape, if you will, of a bunch of different files, so you can treat a bunch of different files as one file, and it can also compress that for us automatically, so we can create these tarballs and send them around more easily than we could all the individual files.

So let's say, for example, that we want to tar up this entire project directory. So I'll use the tar command, and we use c for create, z for zip format, and then the file we want to create is going to be standard output. So this dash here is going to say, create the tar file and just write it to standard output. And then the directory that we want to tar up is the current one, so we'll use .4, and we're going to pipe the output of that into SSH, just like we were piping into it before.

So now we have this tar file that's going to be the standard input of this command, so I can cat that out to something like project.tgz, and this will create the tar file on the remote machine.

Now, before we do that, let's go ahead and also create a directory called project, and we'll move this into that folder.

OK. So now once we log into the remote machine-- as, let's see, we'll go up to cmather herer-- we should see that there's a new folder called project and also project.tgz in there as well. There it is. And now we can extract that, so I can say tar extract zf project.tgz, and here's our entire project directory which has been extracted into this folder. So there's a lot of powerful combinations here.