Bash Script 10: Common Network Utilities
Published On: Monday, November 12th 2018
There are a lot of utilities available to a bash script. Most will be installed by default, which can make scripting a breeze. However, if it's not installed it can mean some unexpected behavior. First and foremost, you'll need to check to see if the program is installed.
Request the user install a program
When a program is not available/installed, you want to notify the user and exit. Here's an example:
[bash]
which avconv
if [ $? -ne 0 ]
then
echo 'avconv not found'
echo 'This script requires avconv.'
echo 'Please install it and try again'
exit 99
fi
[/bash]
Here, we're searching for 'avconv'. You can swap 'avconv' with any utility. Make sure you do this for the utilities discussed here, as some may not be present.
scp - Copy of ssh
This is one of the easiest commands, because it uses the same format as regular 'cp'.
[bash]
#Copy file_to_copy.txt to the server's /path/for/file/
scp file_to_copy.txt user@example.com:/path/for/file/
#Copy file_to_copy.txt to the server's /path/to/file/ and name it remote_file_name.txt
scp file_to_copy.txt user@example.com:/path/for/file/remote_file_name.txt
#Copy file_to_copy.txt to the server's /path/to/file/, using port 22222 for the ssh connection
scp -P 22222 file_to_copy.txt user@example.com:/path/for/file/
[/bash]
As you can see, it's pretty straightforward. The one trick here is specifying the port, which always comes right after 'scp' on the command line.
ssh
You might not think ssh is useful in a script, but it does allow you to run a command remotely.
[bash]
#Run ls on the remote server
ssh user@example.com ls
#Run cp on the remote server
ssh user@example.com "cp file1.txt file2.txt"
[/bash]
Basically, any command you want to run on the server is run and ssh exits immediately.
nslookup
nslookup checks DNS for a hostname. This is great if you want to know if a domain name exists or not, just look at the error code returned ($?).
[bash]
nslookup example.com
if [ $? -ne 0 ]
then
echo 'Domain name not found, exiting'
exit $?
fi
[/bash]
ping
Pinging a server can check to see if it's up. Some servers don't respond to pings, so it's not 100%. If you control the server and have ping open on purpose, this works great!
[bash]
ping example.com
if [ $? -ne 0 ]
then
echo 'Server ping returned an error, exiting'
exit $?
fi
[/bash]
curl and wget
curl and wget are very similar, so I'm going to cover them together. Basically they grab a webpage from a server. The main difference is where the page is placed by default. wget will save the page in a file and curl will output the page to stdout.
[bash]
#Grab the html into a variable
HTML_CONTENTS=`curl http://example.com`
#Save the html to a file
wget http://example.com
[/bash]
My suggestion is to stick to curl if you want the contents in your script, because wget may append to the filename if there is a conflict.
rsync
Last, but not least, is rsync. This will sync the contents of a folder to another folder, possibly on a different computer. If files are the same, no transfer will take place, saving you on bandwidth. Here are some examples:
[bash]
# Copy everything under a folder locally
rsync -av /folder/copying/from/ /folder/copying/to/
# Copy everything under a folder to a server
rsync -av /folder/copying/from/ user@example.com:/folder/copying/to/
# Copy to a server using 22222 as the ssh port
rsync -av -e "ssh -p 22222" /folder/copying/from/ user@example.com:/folder/copying/to/
[/bash]
Do Your Homework
These tools provide a massive amount of options you can pass in on the command line. Check out the man pages for each for more information.