Assignment
Contents
Assignment#
Part 1: Introduction to Linux#
The learning objectives of this assignment are to familiarize you with basic Linux shell commands, standard input, standard output, standard error, and pipes. You will use these ideas when interacting with the Linux shell to operate your drone. Additionally you will use these ideas in the next section when working on the networking exercises.
Background Information#
When you enter a command in a shell, it executes a program. These programs read
from a stream, known as “standard input” and write to two output streams,
“standard output” and “standard error”. When you print
in python, it writes
its output to standard output. In another language, such as C, you use other
functions, such as printf
to write to standard output.
In addition to writing to standard output, a program can read from standard
input. The program cat
, short for concatenate, reads from standard input
and writes the result to standard output.
Standard Output (10 points)#
Write a python program that prints “Hello world” to standard output. Save the program as
hello1.py
and submit it.Write a python program that prints “Hello world” to standard output using
sys.stdout
. Save the program ashello2.py
and submit it.Write a bash script that prints “Hello World” to standard output. Save the script as
hello.sh
and submit it.
Standard Input (10 points)#
Write answers to questions 1-2 inshell.txt
. Submit this file.
Run
cat
with no arguments. Why doescat
seem like it is hanging?When you run
cat
, type a message into your terminal, and pressControl-D
. Describe whatcat
does. Make sure to include which streams are being used, and for what purpose.Write a python program
my_cat.py
that reads a message from standard input and prints to standard output, just ascat
does. You only need to reproduce the behavior ofcat
when run with no arguments. In addition, you do not need to handleControl-D
. Submit this file.
Pipes (20 points)#
Pipes are used to redirect standard input, standard output, and standard error.
First, >
is used to redirect standard output to a file. For example, echo "Hello World" > test.txt
will write the string Hello World
to test.txt
. Write answers to questions 1-4 in shell.txt
. Submit this file.
Create files
one.txt
,two.txt
andthree.txt
that contain the strings1
,2
, and3
, respectively usingecho
and output redirect.By convention, almost all shell programs read input from standard input, and write their output to standard output. Any error messages are printed to standard error. You can chain shell programs together by using
|
. For example, the programls
writes the contents of a directory to standard output. The programsort
reads from standard input, sorts what it reads, and writes the sorted content to standard output. So you can usels | sort
to print out a sorted directory list. Read the man page for sort (man sort
) to learn how to sort in reverse order. What is the bash script (using|
) that prints the contents of a directory in reverse alphabetical order?Use
cat
,|
andecho
to printhello world.
Do not write to any files and use both commands one time.This is not the simplest way to print hello world. Can you suggest a simpler way? (We asked you to do it the more complicated way to practice with pipes.)
Write a python script that reads from standard input, sorts lines in reverse alphabetical order, and prints the result. It should behave like
sort -r
. It does not need to process any command line arguments. Submit your script in a file calledmy_reverse_sort.py
. Do not submit this script inshell.txt
Standard Error (10 points)#
In addition to standard input and standard output, there is a third stream, standard error. If there is an error in a chain of pipes, it will be printed to the terminal rather than buried in the input to the next program.
Recall that
ls -a | sort > sorted.txt
puts all the names of files in a directory sorted in alphabetical order into the filesorted.txt
. If you modify the command to bels -a -hippo | sort > sorted.txt
, what text is insorted.txt
, what is outputted as standard error, and why? Answer this question inshell.txt
. Submit this file.Create a python script that prints reversed sorted output to standard error. Use it to sort
ls -a
instead ofsort
. Submit the file containing the script asmy_sort_status.py
.
Part 2: Networking#
The learning objectives of this section are to familiarize you with how a TCP/IP server works and how to explore a network to find what computers (and robots!) are around, and then how to connect to them. We will use tools at a lower level than the robot programming interface you will use in the rest of the course, in order to focus on the general networking ideas.
Netcat (20 points)#
The command nc
is short for “netcat” and is similar to cat
but works over
network connections. It reads from standard input and writes its contents not
to standard output, but to a specified server. Write your answers in the
corresponding sections of networking.txt
.
Point
nc
to google.com as follows:nc www.google.com 80
When you first connect, it will be silent. Then type any arbitrary text and press enter. What is the error number?Now type some valid http into nc:
GET / HTTP/1.1
. What is the output?Now use
nc
to make a server. In one window, typenc -l 12345
. This will causenc
to listen on port 12345. In another terminal on the same machine, typenc localhost 12345
. You can type a message in one window and it will appear in the other window (and vice versa). This trick can be very useful to test basic internet connectivity - if the client and server can send packets at all. No answer is required for this question.By convention,
roscore
listens on port 11311. Try usingnc
to connect to port 11311 on a machine whereroscore
is running, such as the Raspberry Pi on your drone. What protocol is roscore using to communicate (think application layer)?
Talking to Your Robot (10 points)#
So far, this assignment has required access to localhost
, the local machine
you are connected to, and google.com
.
Most commonly, the base station and robot are connected over TCP/IP to the same
local network. Then you can look up your machine’s IP address (ifconfig
in
Unix; other ways in other OSes), and your robot’s IP address, and connect them.
How can you find your robot’s IP address? Well it’s a chicken-and-egg problem.
If you knew the IP address, you can connect to the robot and run ifconfig
and
find the IP address, but you don’t know the IP address.
What to do? There are several solutions. Write the answers to the following
questions in networking.txt
.
Brainstorm how you can solve the chicken-and-egg program to connect to your robot. List three different solutions.
Look Ma, No Internet! (10 points)#
But what about if there is no public internet connection? What if you want to
fly your drone in the wilderness? Well, there does exist cellular modems and
satellite connections, but you can also tell your drone to act as a Wifi
Hotspot. It can create a network and run a DHCP server. You can configure this
on your drone using the file /etc/hostapd/hostapd.conf
. Then you can connect
your laptop’s base station using the SSID and passphrase specified in that
file, and connect to the drone.
Alternatively you can set up your laptop as the Wifi base station and configure the drone to connect to its network. The details will vary depending on your laptop OS and settings.
Your Raspberry Pi is configured to be a Wireless AP Master by default. Connect to it with
your base station. Write the answers to the following questions in networking.txt
.
Which machine is acting as the DHCP server?
What is the Raspberry Pi’s IP address? What is yours?
Describe another network configuration for the wifi, other than the Raspberry Pi being a Wireless AP Master.
Describe three network configurations for a network allowing a basestation and Duckiedrone to communicate with each other. Feel free to add additional devices, such as a cell phone performing internet connection sharing.