Ruby Base64 编码包括变量中的换行符
Ruby Base64 encode include linebreaks in variable
base64 编码行 returns
试图了解如何通过 Base64 module 在 Base64 中附加行 returns,但我无法让编码执行我想要的操作...
简单地说,考虑到我的错误代码:
require "base64"
#
enc = Base64.encode64(%q[
#!/bin/bash
echo 'this is a test of nesting quotes using ruby's %q thing-a-ma-bob'
echo 'this should return a base64 formattes version of this "file" for the purposes of cloudconfig formation'
echo "i'm not quite certain what this script should do... so for now it does a lot of nothing ... and i don't care"
df -h |awk '{print "[ ]""{ }"" -- "}'
])
#
plain = Base64.decode64(enc)
#
#
puts "base64: "+enc
puts
puts "plain:"
puts plain
puts
puts "let's run the script now for testing:"
puts
exec({"code" => plain}, "echo ; echo bash ; echo $code")
returns:
base64: CiAgICAjIS9iaW4vYmFzaAogICAgZWNobyAndGhpcyBpcyBhIHRlc3Qgb2Yg
bmVzdGluZyBxdW90ZXMgdXNpbmcgcnVieSdzICVxIHRoaW5nLWEtbWEtYm9i
JwogICAgZWNobyAndGhpcyBzaG91bGQgcmV0dXJuIGEgYmFzZTY0IGZvcm1h
dHRlcyB2ZXJzaW9uIG9mIHRoaXMgImZpbGUiIGZvciB0aGUgcHVycG9zZXMg
b2YgY2xvdWRjb25maWcgZm9ybWF0aW9uJwogICAgZWNobyAiaSdtIG5vdCBx
dWl0ZSBjZXJ0YWluIHdoYXQgdGhpcyBzY3JpcHQgc2hvdWxkIGRvLi4uIHNv
IGZvciBub3cgaXQgZG9lcyBhIGxvdCBvZiBub3RoaW5nIC4uLiBhbmQgaSBk
b24ndCBjYXJlIgogICAgZGYgLWggfGF3ayAne3ByaW50ICQxIlsgXSIkMiJ7
IH0iJDMiIC0tICIkNH0nCiAgICA=
plain:
#!/bin/bash
echo 'this is a test of nesting quotes using ruby's %q thing-a-ma-bob'
echo 'this should return a base64 formattes version of this "file" for the purposes of cloudconfig formation'
echo "i'm not quite certain what this script should do... so for now it does a lot of nothing ... and i don't care"
df -h |awk '{print "[ ]""{ }"" -- "}'
let's run the script now for testing:
bash
#!/bin/bash echo 'this is a test of nesting quotes using ruby's %q thing-a-ma-bob' echo 'this should return a base64 formattes version of this "file" for the purposes of cloudconfig formation' echo "i'm not quite certain what this script should do... so for now it does a lot of nothing ... and i don't care" df -h |awk '{print "[ ]""{ }"" -- "}'
我想试着了解一下
如何将换行符带入我传递给系统命令的变量中,而不必采用如下格式:
plain = 'VGhpcyBpcyBsaW5lIG9uZQpUaGlzIG' +
'lzIGxpbmUgdHdvClRoaXMgaXMgbGlu' +
'ZSB0aHJlZQpBbmQgc28gb24uLi4K'
谢谢
对负载进行编码时,您可能需要在通过 Base64 编码之前包含换行符。请看下面:
[7] pry(main)> require "base64"
=> true
[8] pry(main)> Base64.encode64("Apple\nBacon")
=> "QXBwbGUKQmFjb24=\n"
[9] pry(main)> Base64.decode64(_)
=> "Apple\nBacon"
[10] pry(main)> Base64.encode64("Apple\nBacon")
=> "QXBwbGUKQmFjb24=\n"
[11] pry(main)> puts Base64.decode64(_)
Apple
Bacon
在编码前将"\n"放在字符串中,解码时返回并打印出来
问题在于 echo
如何处理其输入,它与 base 64 编码/解码无关。
您对 exec
的呼叫有 echo $code
。这里 $code
被扩展,然后在作为字符串列表传递给 echo
之前按空格拆分。 echo
然后打印出每一个以空格分隔。
为防止这种情况,您可以确保将整个 $code
变量作为单个字符串直接传递,方法是将其括在引号中。将 exec
行更改为(注意 $code
周围的额外引号):
exec({"code" => plain}, "echo ; echo bash ; echo \"$code\"")
这将打印出包含换行符的块。
base64 编码行 returns
试图了解如何通过 Base64 module 在 Base64 中附加行 returns,但我无法让编码执行我想要的操作...
简单地说,考虑到我的错误代码:
require "base64"
#
enc = Base64.encode64(%q[
#!/bin/bash
echo 'this is a test of nesting quotes using ruby's %q thing-a-ma-bob'
echo 'this should return a base64 formattes version of this "file" for the purposes of cloudconfig formation'
echo "i'm not quite certain what this script should do... so for now it does a lot of nothing ... and i don't care"
df -h |awk '{print "[ ]""{ }"" -- "}'
])
#
plain = Base64.decode64(enc)
#
#
puts "base64: "+enc
puts
puts "plain:"
puts plain
puts
puts "let's run the script now for testing:"
puts
exec({"code" => plain}, "echo ; echo bash ; echo $code")
returns:
base64: CiAgICAjIS9iaW4vYmFzaAogICAgZWNobyAndGhpcyBpcyBhIHRlc3Qgb2Yg
bmVzdGluZyBxdW90ZXMgdXNpbmcgcnVieSdzICVxIHRoaW5nLWEtbWEtYm9i
JwogICAgZWNobyAndGhpcyBzaG91bGQgcmV0dXJuIGEgYmFzZTY0IGZvcm1h
dHRlcyB2ZXJzaW9uIG9mIHRoaXMgImZpbGUiIGZvciB0aGUgcHVycG9zZXMg
b2YgY2xvdWRjb25maWcgZm9ybWF0aW9uJwogICAgZWNobyAiaSdtIG5vdCBx
dWl0ZSBjZXJ0YWluIHdoYXQgdGhpcyBzY3JpcHQgc2hvdWxkIGRvLi4uIHNv
IGZvciBub3cgaXQgZG9lcyBhIGxvdCBvZiBub3RoaW5nIC4uLiBhbmQgaSBk
b24ndCBjYXJlIgogICAgZGYgLWggfGF3ayAne3ByaW50ICQxIlsgXSIkMiJ7
IH0iJDMiIC0tICIkNH0nCiAgICA=
plain:
#!/bin/bash
echo 'this is a test of nesting quotes using ruby's %q thing-a-ma-bob'
echo 'this should return a base64 formattes version of this "file" for the purposes of cloudconfig formation'
echo "i'm not quite certain what this script should do... so for now it does a lot of nothing ... and i don't care"
df -h |awk '{print "[ ]""{ }"" -- "}'
let's run the script now for testing:
bash
#!/bin/bash echo 'this is a test of nesting quotes using ruby's %q thing-a-ma-bob' echo 'this should return a base64 formattes version of this "file" for the purposes of cloudconfig formation' echo "i'm not quite certain what this script should do... so for now it does a lot of nothing ... and i don't care" df -h |awk '{print "[ ]""{ }"" -- "}'
我想试着了解一下
如何将换行符带入我传递给系统命令的变量中,而不必采用如下格式:
plain = 'VGhpcyBpcyBsaW5lIG9uZQpUaGlzIG' +
'lzIGxpbmUgdHdvClRoaXMgaXMgbGlu' +
'ZSB0aHJlZQpBbmQgc28gb24uLi4K'
谢谢
对负载进行编码时,您可能需要在通过 Base64 编码之前包含换行符。请看下面:
[7] pry(main)> require "base64"
=> true
[8] pry(main)> Base64.encode64("Apple\nBacon")
=> "QXBwbGUKQmFjb24=\n"
[9] pry(main)> Base64.decode64(_)
=> "Apple\nBacon"
[10] pry(main)> Base64.encode64("Apple\nBacon")
=> "QXBwbGUKQmFjb24=\n"
[11] pry(main)> puts Base64.decode64(_)
Apple
Bacon
在编码前将"\n"放在字符串中,解码时返回并打印出来
问题在于 echo
如何处理其输入,它与 base 64 编码/解码无关。
您对 exec
的呼叫有 echo $code
。这里 $code
被扩展,然后在作为字符串列表传递给 echo
之前按空格拆分。 echo
然后打印出每一个以空格分隔。
为防止这种情况,您可以确保将整个 $code
变量作为单个字符串直接传递,方法是将其括在引号中。将 exec
行更改为(注意 $code
周围的额外引号):
exec({"code" => plain}, "echo ; echo bash ; echo \"$code\"")
这将打印出包含换行符的块。