The script does the following:
- Takes a URL to download, remote machine id and user id on the remote machine.
- Runs remote pwd to get the working directory.
- Runs remote wget with the URL and captures the saved file name.
- Runs scp to copy the file to local system.
- Runs remote rm to remove the file from remote system.
- Deletes temporary files created.
#!/bin/bash
# Replace the following with your server details.
# If you do not want password prompts setup authorized keys with following commands:
# ssh-keygen -t rsa
# cat ~/.ssh/id_rsa.pub | ssh@ 'cd .ssh; cat >> authorized_keys; chmod 600 authorized_keys'
remserver="<remote server>"
remuser="<remote user>"
if [ $# -lt 1 ]
then
echo "Usage: $0 <url>"
exit 1
fi
url=$1
echo "ssh ${remuser}@${remserver} 'pwd' >getthruremotedir" > ./getthrutmpscript
echo "remotedir=\`cat getthruremotedir\`" >> ./getthrutmpscript
echo "rm getthruremotedir" >> ./getthrutmpscript
echo "echo \"Got remotedir: \${remotedir}\"" >> ./getthrutmpscript
echo "ssh ${remuser}@${remserver} 'wget \"${url}\"' 2>&1 | grep \"saved\" | cut -d'\`' -f2 | cut -d\"'\" -f1 >getthrufilename" >> ./getthrutmpscript
echo "filename=\`cat getthrufilename\`" >> ./getthrutmpscript
echo "rm getthrufilename" >> ./getthrutmpscript
echo "echo \"Got filename: \${filename}\"" >> ./getthrutmpscript
echo "echo \"scp ${remuser}@${remserver}:\${remotedir}/\${filename} .\" > ./getthrutmpscript2" >> ./getthrutmpscript
echo "echo \"ssh ${remuser}@${remserver} 'rm -i \${remotedir}/\${filename}'\" >> ./getthrutmpscript2" >> ./getthrutmpscript
echo "chmod +x ./getthrutmpscript2" >> ./getthrutmpscript
echo "./getthrutmpscript2" >> ./getthrutmpscript
echo "rm ./getthrutmpscript2" >> ./getthrutmpscript
chmod +x ./getthrutmpscript
./getthrutmpscript
rm ./getthrutmpscript
If you do not want password prompts setup authorized keys with following commands:
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub | ssh <user>@<machine> 'cd .ssh; cat >> authorized_keys; chmod 600 authorized_keys'
The script has worked fine for me, but use it carefully as it executes commands in remote system without any substantial output and error checks.
No comments:
Post a Comment