Forming sanitary shell commands or system calls in Ruby

后端 未结 5 1782
长发绾君心
长发绾君心 2020-12-08 11:20

I\'m building a daemon that will help me manage my server(s). Webmin works fine, as does just opening a shell to the server, but I\'d prefer to be able to control server ope

5条回答
  •  被撕碎了的回忆
    2020-12-08 12:16

    I'd suggest looking into the 'shellwords' module. This script:

    require 'shellwords'
    parts = ['echo', "'hello world'; !%& some stuff", 'and another argument']
    command = Shellwords.shelljoin( parts )
    puts command
    output = `#{ command }`
    puts output
    

    outputs the escaped text and the expected output:

    echo \'hello\ world\'\;\ \!\%\&\ some\ stuff and\ another\ argument
    'hello world'; !%& some stuff and another argument
    

提交回复
热议问题