Executing commands remotely with ssh and output redirection
Last Updated on Saturday, 08 May 2010 21:10
ssh command can by used to remotely login to a server running sshd daemon. This allows linux administrators to perform variety of administrative jobs. However, ssh is more powerful than just providing a user with remove shell access, it can also be used to automate remote command executions, running simple backups and download the backup file locally. Here are couple examples:
The following ssh command can be used to to create a file remotely:
ssh user@ssh-server.com '( cd /tmp/ && touch ssh_file.txt )'
Make a local copy of a remote /etc/passwd file to /tmp/passwd:
ssh user@ssh-server.com '( cat /etc/passwd )' > /tmp/passwd
Execute a script on the remote server with ssh:
NOTE: we assume that the script exists, is executable and you have executable permissions
ssh user@ssh-server.com '( cat ~/myscript.sh )'
In this example we make a bzip2 local copy of /var/log/auth.log file to a local /tmp/ direcotry:
ssh user@ssh-server.com '( cp /var/log/auth.log /tmp/; cd /tmp/ && tar -jcvf - auth.log )' > /tmp/auth.tar.bz2















