SlideShare ist ein Scribd-Unternehmen logo
1 von 6
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Kürzlich hochgeladen

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 

Kürzlich hochgeladen (20)

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 

Empfohlen

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
 

Empfohlen (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