Bash Script 10: Common Network Utilities

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:

which avconv
if [ $? -ne 0 ]
  echo 'avconv not found'
  echo 'This script requires avconv.'
  echo 'Please install it and try again'
  exit 99

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’.

#Copy file_to_copy.txt to the server's /path/for/file/
scp file_to_copy.txt

#Copy file_to_copy.txt to the server's /path/to/file/ and name it remote_file_name.txt
scp file_to_copy.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

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.


You might not think ssh is useful in a script, but it does allow you to run a command remotely.

#Run ls on the remote server
ssh ls

#Run cp on the remote server
ssh "cp file1.txt file2.txt"

Basically, any command you want to run on the server is run and ssh exits immediately.


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 ($?).

if [ $? -ne 0 ]
  echo 'Domain name not found, exiting'
  exit $?


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!

if [ $? -ne 0 ]
  echo 'Server ping returned an error, exiting'
  exit $?

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.

#Grab the html into a variable

#Save the html to a file

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.


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:

# 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/

# Copy to a server using 22222 as the ssh port
rsync -av -e "ssh -p 22222" /folder/copying/from/

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.