3 回答
TA贡献1824条经验 获得超5个赞
system
, %x[]
system
Open3.popen3
IO::popen
Open3.popen3
:
require 'open3'stdin, stdout, stderr, wait_thr = Open3.popen3('usermod', '-p', @options['shadow'], @options['username'])stdout.gets(nil)stdout.close stderr.gets(nil)stderr.close exit_code = wait_thr.value
TA贡献1805条经验 获得超10个赞
Open3.capture2()
, Open3.capture2e()
Open3.capture3()
.
%x
untrusted = "; date; echo"out = `echo #{untrusted}` # BADuntrusted = '"; date; echo"'out = `echo "#{untrusted}"` # BADuntrusted = "'; date; echo'"out = `echo '#{untrusted}'` # BAD
system
ret = system "echo #{untrusted}" # BADret = system 'echo', untrusted # good
Open3.capture2
, capture2e
capture3
system
out, err, st = Open3.capture3("echo #{untrusted}") # BADout, err, st = Open3.capture3('echo', untrusted) # goodout_err, st = Open3.capture2e('echo', untrusted) # goodout, st = Open3.capture2('echo', untrusted) # goodp st.exitstatus
IO.popen()
out = IO.popen(['echo', untrusted]).read # good
Open3.capture3()
## Returns stdout on success, false on failure, nil on error#def syscall(*cmd) begin stdout, stderr, status = Open3.capture3(*cmd) status.success? && stdout.slice!(0..-(1 + $/.size)) # strip trailing eol rescue endend
p system('foo')p syscall('foo')p system('which', 'foo')p syscall('which', 'foo')p system('which', 'which')p syscall('which', 'which')
nilnilfalsefalse/usr/bin/which <— stdout from system('which', 'which')true <- p system('which', 'which')"/usr/bin/which" <- p syscall('which', 'which')
- 3 回答
- 0 关注
- 1584 浏览
添加回答
举报