Skip to content

Execute system commands and display outputs to a browser

License

Notifications You must be signed in to change notification settings

paulmchen/cmd2browser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cmd2browser

Execute system commands and display outputs to a browser

A python application that can be modified to execute system commands and then display the outputs to a Web browser.

github stats

Install Prerequisites

Install Python (v3) and PIP:

  1. Visit https://linuxize.com/post/how-to-install-python-3-7-on-ubuntu-18-04/ to learn how to install Python
  2. Visit https://www.shellhacks.com/python-install-pip-mac-ubuntu-centos/ to learn how to install PIP
  3. Run the following commands to install Python flask and PYyaml modules:
  pip install flask
  pip install PYyaml

Install and Launch the App

  1. Download the code
   git clone https://github.com/paulmchen/cmd2browser.git
  1. Go to /cmd2browser folder and Launch the app
  python app.py

Verify

  1. Start a browser session and use the following URL to validate examples provided
  1. http://127.0.0.1:5000/ls/
  2. http://127.0.0.1:5000/docker_ps/

Tips

  • When you are connecting to the server from another client IP, you would need to add your client IP address to the white list in config/config.yaml. By default, the server will reject any non-local connections from any remote clients. Modify the following line by adding your client IP address to the white list of the server:
  ip_whitelist: '[''192.168.1.2'', ''192.168.1.3'', ''127.0.0.1'']'
  • To add a new command line call and then send its output to a web browser, you can create a new def function in app.py. For example, to call a system command "kubectl get pods", add the following 'def' to app.py:
  @app.route('/kc_pods/')
  def get_kc_pods():
      try:
          return command.run("kubectl get pods")
      except subprocess.CalledProcessError as e:
          return message.error_500_msg
  • To support a command that may not return results immediately, you can use exec_command_async function instead. exec_command_async takes 2 input parameters. The first parameter is the command that you want to run, for example, top, the 2nd parameter exec_time_in_seconds is the elapse time to indicate how long it needs to wait in seconds before the browser session should resume to fetch outputs and then terminate the process of the command. Note: ensure that you set a proper value of the exec_time_in_seconds to avoid from command process being terminated before it is completed.
  # Example: call async command, e.g. top
  @app.route('/top/')
  def get_top():
      try:
          return command.run_async("top", 2)
      except subprocess.CalledProcessError as e:
          return message.error_500_msg

Launch the following URL to verify your new command line execution from your favourite browser:

http://127.0.0.1:5000/kc_pods/

About

Execute system commands and display outputs to a browser

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages