Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Using Sed on A Moxa Linux Embedded Computer
1. Using sed on a Moxa Linux Embedded
Computer: A CGI Exercise
sed is a stream editor for filtering and transforming text in Linux. In the
example below, you can write a script with sed to find a matched pattern, Input,
and replace with the substituted string Output.
Input line will be changed
Pattern: Input
sd
e Substitute: Output
Output line will be changed
Fig. 1: sed is a stream editor
In this article we will take advantage of a shell script with the sed command to
write a small CGI (Common Gateway Interface) program to change the
hostname. There are two files created in this exercise. One is an html web
page (hostname.htm) for the user to input a new host name, the other is the
executable CGI program (hostname.cgi) to get the new host name and
configure it on the system.
Seting up the Web Page on a Moxa
Embedded Computer
In this exercise, we will change the host name using the Apache web server.
However, the default user of the web server “nobody” does not have the
permissions to change the host name. Hence, before starting up the Apache
web server, you need to modify the configuration file
/etc/apache/conf/httpd.conf to run the Apache server as root.
2. Open the configuration file and find the attributes User and Group. Set the
value of User and Group to root.
Put the below html file in the default web directory /home/httpd/htdocs. When
the submit button is clicked, the web page will trigger the action to execute the
CGI program hostname.cgi under the directory /home/httpd/cgi-bin.
hostname.htm
<HTML>
<HEAD><TITLE>Hostname configure page</TITLE></HEAD>
<BODY bgcolor="#cccccc" text="#000000">
<H2>Hostname configure</H2>
<form method=get action="/cgi-bin/hostname.cgi">
<pre>
Hostname: <input name="hostname" size=42>
</pre>
<input type="submit" value="Submit" size=20>
<input type="reset" value="Clear" size=20>
</form>
</BODY>
</HTML>
The web page should look like the screenshot below:
3. Using the hostname.cgi Script
We use a bash shell script named hostname.cgi to get parameters from the
web server and use sed to find the specific input value of hostname in the
environmental variable $QUERY_STRING. Note that you have to set the
executable permission to hostname.cgi so that the Apache server can invoke it
via the request from hostname.htm. The contents of hostname.cgi are shown
below:
hostname.cgi
#!/bin/sh
echo "Content-type: text/html"
echo
# our html code
echo "<html>"
echo "<head><title>Host name configure</title></head>"
echo "<body>"
echo "<pre>"
# print out the environment settings
#/usr/bin/env
4. hostname=`echo "$QUERY_STRING" | sed -n
's/^.*hostname=([^&]*).*$/1/p'`
orig_hostname=`cat /etc/hostname`
# Show the configuration on web page
echo "The original hostname $orig_hostname will be configured
as $hostname"
# Change the hostname via /bin/hostname command
/bin/hostname $hostname
echo "The new hostname is $hostname"
echo "</pre>"
echo "</body>"
echo "</html>"
In this script, we would like to explain how the sed command gets the user
input value from the web page. Related lines are shown in red color in the
above scripts.
$QUERY_STRING is an environmental variable passed by Apache server to
the CGI program. The value of this variable might be:
hostname=Moxa_new
sed -n 's/^.*hostname=([^&]*).*$/1/p':
-n: It will not print anything unless an explicit request to print is found.
s: The substitute command changes all occurrences of the regular expression
into a new value.
/syntax/: Between the two forward slashes “/” is the regular expression.
(^.*hostname=([^&]*).*$) means from the beginning of the variable
“$QUERY_STRING”, find the pattern “hostname=” and go to the end of line.
Useful reference sites for sed and regular expressions are listed at the end of
this document.
5. The following web page displays the results:
Notes
The following Moxa Linux embedded computers support sed in the following
firmware versions and later.
Model name Firmware version
UC-7410 v2.1
UC-7420 v2.1
UC-7402 v2.1
UC-7408 v2.1
DA-660 v1.1
DA-661-LX v1.3
DA-662-LX v1.3
DA-663-LX v1.3
UC-7410-LX Plus v1.3
UC-7420-LX Plus v1.3
UC-7402-LX Plus v1.3
UC-7408-LX Plus v1.3
UC-8410-LX v1.0
UC-8416-LX v1.0
UC-8418-LX v1.1
6. DA-681/682-LX v1.0
Reference
1. sed – An Introduction and tutorial http://www.grymoire.com/Unix/Sed.html
2. Regular Expression http://www.grymoire.com/Unix/Regular.html#uh-2