SlideShare uma empresa Scribd logo
1 de 6
Baixar para ler offline
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.
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:
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
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.
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
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

Mais conteúdo relacionado

Último

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 

Último (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

Destaque

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destaque (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
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