add a command to each command (e.g. proxychains each command by default)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












How can I execute each command pre-appended with another one?



Example when I run:



nmap -p 80 host


I want it to run



proxychains nmap -p 80 host


even when I do not add proxychains intentionally.



In other words: can I alias all commands at once with proxychains pre-appended?



Bonus if this is something I can switch on/off.







share|improve this question






















  • Each and every command? Not just every nmap but everything?
    – ilkkachu
    Dec 16 '17 at 14:51














up vote
1
down vote

favorite












How can I execute each command pre-appended with another one?



Example when I run:



nmap -p 80 host


I want it to run



proxychains nmap -p 80 host


even when I do not add proxychains intentionally.



In other words: can I alias all commands at once with proxychains pre-appended?



Bonus if this is something I can switch on/off.







share|improve this question






















  • Each and every command? Not just every nmap but everything?
    – ilkkachu
    Dec 16 '17 at 14:51












up vote
1
down vote

favorite









up vote
1
down vote

favorite











How can I execute each command pre-appended with another one?



Example when I run:



nmap -p 80 host


I want it to run



proxychains nmap -p 80 host


even when I do not add proxychains intentionally.



In other words: can I alias all commands at once with proxychains pre-appended?



Bonus if this is something I can switch on/off.







share|improve this question














How can I execute each command pre-appended with another one?



Example when I run:



nmap -p 80 host


I want it to run



proxychains nmap -p 80 host


even when I do not add proxychains intentionally.



In other words: can I alias all commands at once with proxychains pre-appended?



Bonus if this is something I can switch on/off.









share|improve this question













share|improve this question




share|improve this question








edited Dec 18 '17 at 22:16









Gilles

507k12010031530




507k12010031530










asked Dec 16 '17 at 14:11









AK_

75738




75738











  • Each and every command? Not just every nmap but everything?
    – ilkkachu
    Dec 16 '17 at 14:51
















  • Each and every command? Not just every nmap but everything?
    – ilkkachu
    Dec 16 '17 at 14:51















Each and every command? Not just every nmap but everything?
– ilkkachu
Dec 16 '17 at 14:51




Each and every command? Not just every nmap but everything?
– ilkkachu
Dec 16 '17 at 14:51










2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










Would it work to just run a full shell under proxychains? Assuming it can deal with processes started by the shell properly.



You could do with just



$ proxychains bash


and exit the shell at will.




But if you really want to, you can abuse the DEBUG trap (with extdebug set) to mangle the commands the shell runs. This would run every command with time:



$ shopt -s extdebug
$ d() eval "time $BASH_COMMAND"; return 1;
$ trap d DEBUG
$ sleep 2

real 0m2.010s
user 0m0.000s
sys 0m0.000s
$ trap - DEBUG # turn it off, this still prints the 'time' output


But the tricky part here is that it will also affect builtins, like trap or shopt themselves, so you'd probably want to add some exceptions for those... Also, stuff like cd somedir would turn into proxychains cd somedir, which probably will not work. This would also affect everything started from within functions etc. Maybe it's better to just have the function use proxychains only for those commands known to need it.






share|improve this answer






















  • this is genius solution! I wish I can give you 100 points for it. Thank you!
    – AK_
    Dec 17 '17 at 10:29










  • bonus of bonus: is there a way I can temporarily ask bash to escape one command to run it without proxychains ? example I don't want ls to be slow and wait for proxychains
    – AK_
    Dec 17 '17 at 10:31






  • 1




    @AK_, with the shell under proxychains, I don't think so. With the DEBUG trap you could of course do anything, but listing the programs you want to use proxychains with, or the ones without it might get annoying. Which leads me to think if it would be easier to just alias p=proxychains and then go with p nmap... when you need it...
    – ilkkachu
    Dec 17 '17 at 11:03

















up vote
1
down vote













You can just create an alias for your current sesion.



alias nmap="proxychains nmap"


Everytime you write nmap, it would be as writting "proxychains nmap".
You can turn it off with:



unalias nmap


You could also create a shell script to manage this. Just add it to your bashrc



function prefix 
if [ $2 != "with" ]; then
echo "You have to prefix something with something"
return
fi
echo "alias $1 = $3 $1"

alias $1="$3 $1"

function unprefix sed "s/=.b.*b /='/g")



So you can do:



prefix nmap with proxychains
unprefix nmap





share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f411260%2fadd-a-command-to-each-command-e-g-proxychains-each-command-by-default%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote



    accepted










    Would it work to just run a full shell under proxychains? Assuming it can deal with processes started by the shell properly.



    You could do with just



    $ proxychains bash


    and exit the shell at will.




    But if you really want to, you can abuse the DEBUG trap (with extdebug set) to mangle the commands the shell runs. This would run every command with time:



    $ shopt -s extdebug
    $ d() eval "time $BASH_COMMAND"; return 1;
    $ trap d DEBUG
    $ sleep 2

    real 0m2.010s
    user 0m0.000s
    sys 0m0.000s
    $ trap - DEBUG # turn it off, this still prints the 'time' output


    But the tricky part here is that it will also affect builtins, like trap or shopt themselves, so you'd probably want to add some exceptions for those... Also, stuff like cd somedir would turn into proxychains cd somedir, which probably will not work. This would also affect everything started from within functions etc. Maybe it's better to just have the function use proxychains only for those commands known to need it.






    share|improve this answer






















    • this is genius solution! I wish I can give you 100 points for it. Thank you!
      – AK_
      Dec 17 '17 at 10:29










    • bonus of bonus: is there a way I can temporarily ask bash to escape one command to run it without proxychains ? example I don't want ls to be slow and wait for proxychains
      – AK_
      Dec 17 '17 at 10:31






    • 1




      @AK_, with the shell under proxychains, I don't think so. With the DEBUG trap you could of course do anything, but listing the programs you want to use proxychains with, or the ones without it might get annoying. Which leads me to think if it would be easier to just alias p=proxychains and then go with p nmap... when you need it...
      – ilkkachu
      Dec 17 '17 at 11:03














    up vote
    4
    down vote



    accepted










    Would it work to just run a full shell under proxychains? Assuming it can deal with processes started by the shell properly.



    You could do with just



    $ proxychains bash


    and exit the shell at will.




    But if you really want to, you can abuse the DEBUG trap (with extdebug set) to mangle the commands the shell runs. This would run every command with time:



    $ shopt -s extdebug
    $ d() eval "time $BASH_COMMAND"; return 1;
    $ trap d DEBUG
    $ sleep 2

    real 0m2.010s
    user 0m0.000s
    sys 0m0.000s
    $ trap - DEBUG # turn it off, this still prints the 'time' output


    But the tricky part here is that it will also affect builtins, like trap or shopt themselves, so you'd probably want to add some exceptions for those... Also, stuff like cd somedir would turn into proxychains cd somedir, which probably will not work. This would also affect everything started from within functions etc. Maybe it's better to just have the function use proxychains only for those commands known to need it.






    share|improve this answer






















    • this is genius solution! I wish I can give you 100 points for it. Thank you!
      – AK_
      Dec 17 '17 at 10:29










    • bonus of bonus: is there a way I can temporarily ask bash to escape one command to run it without proxychains ? example I don't want ls to be slow and wait for proxychains
      – AK_
      Dec 17 '17 at 10:31






    • 1




      @AK_, with the shell under proxychains, I don't think so. With the DEBUG trap you could of course do anything, but listing the programs you want to use proxychains with, or the ones without it might get annoying. Which leads me to think if it would be easier to just alias p=proxychains and then go with p nmap... when you need it...
      – ilkkachu
      Dec 17 '17 at 11:03












    up vote
    4
    down vote



    accepted







    up vote
    4
    down vote



    accepted






    Would it work to just run a full shell under proxychains? Assuming it can deal with processes started by the shell properly.



    You could do with just



    $ proxychains bash


    and exit the shell at will.




    But if you really want to, you can abuse the DEBUG trap (with extdebug set) to mangle the commands the shell runs. This would run every command with time:



    $ shopt -s extdebug
    $ d() eval "time $BASH_COMMAND"; return 1;
    $ trap d DEBUG
    $ sleep 2

    real 0m2.010s
    user 0m0.000s
    sys 0m0.000s
    $ trap - DEBUG # turn it off, this still prints the 'time' output


    But the tricky part here is that it will also affect builtins, like trap or shopt themselves, so you'd probably want to add some exceptions for those... Also, stuff like cd somedir would turn into proxychains cd somedir, which probably will not work. This would also affect everything started from within functions etc. Maybe it's better to just have the function use proxychains only for those commands known to need it.






    share|improve this answer














    Would it work to just run a full shell under proxychains? Assuming it can deal with processes started by the shell properly.



    You could do with just



    $ proxychains bash


    and exit the shell at will.




    But if you really want to, you can abuse the DEBUG trap (with extdebug set) to mangle the commands the shell runs. This would run every command with time:



    $ shopt -s extdebug
    $ d() eval "time $BASH_COMMAND"; return 1;
    $ trap d DEBUG
    $ sleep 2

    real 0m2.010s
    user 0m0.000s
    sys 0m0.000s
    $ trap - DEBUG # turn it off, this still prints the 'time' output


    But the tricky part here is that it will also affect builtins, like trap or shopt themselves, so you'd probably want to add some exceptions for those... Also, stuff like cd somedir would turn into proxychains cd somedir, which probably will not work. This would also affect everything started from within functions etc. Maybe it's better to just have the function use proxychains only for those commands known to need it.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 17 '17 at 10:57

























    answered Dec 16 '17 at 14:55









    ilkkachu

    49.9k674137




    49.9k674137











    • this is genius solution! I wish I can give you 100 points for it. Thank you!
      – AK_
      Dec 17 '17 at 10:29










    • bonus of bonus: is there a way I can temporarily ask bash to escape one command to run it without proxychains ? example I don't want ls to be slow and wait for proxychains
      – AK_
      Dec 17 '17 at 10:31






    • 1




      @AK_, with the shell under proxychains, I don't think so. With the DEBUG trap you could of course do anything, but listing the programs you want to use proxychains with, or the ones without it might get annoying. Which leads me to think if it would be easier to just alias p=proxychains and then go with p nmap... when you need it...
      – ilkkachu
      Dec 17 '17 at 11:03
















    • this is genius solution! I wish I can give you 100 points for it. Thank you!
      – AK_
      Dec 17 '17 at 10:29










    • bonus of bonus: is there a way I can temporarily ask bash to escape one command to run it without proxychains ? example I don't want ls to be slow and wait for proxychains
      – AK_
      Dec 17 '17 at 10:31






    • 1




      @AK_, with the shell under proxychains, I don't think so. With the DEBUG trap you could of course do anything, but listing the programs you want to use proxychains with, or the ones without it might get annoying. Which leads me to think if it would be easier to just alias p=proxychains and then go with p nmap... when you need it...
      – ilkkachu
      Dec 17 '17 at 11:03















    this is genius solution! I wish I can give you 100 points for it. Thank you!
    – AK_
    Dec 17 '17 at 10:29




    this is genius solution! I wish I can give you 100 points for it. Thank you!
    – AK_
    Dec 17 '17 at 10:29












    bonus of bonus: is there a way I can temporarily ask bash to escape one command to run it without proxychains ? example I don't want ls to be slow and wait for proxychains
    – AK_
    Dec 17 '17 at 10:31




    bonus of bonus: is there a way I can temporarily ask bash to escape one command to run it without proxychains ? example I don't want ls to be slow and wait for proxychains
    – AK_
    Dec 17 '17 at 10:31




    1




    1




    @AK_, with the shell under proxychains, I don't think so. With the DEBUG trap you could of course do anything, but listing the programs you want to use proxychains with, or the ones without it might get annoying. Which leads me to think if it would be easier to just alias p=proxychains and then go with p nmap... when you need it...
    – ilkkachu
    Dec 17 '17 at 11:03




    @AK_, with the shell under proxychains, I don't think so. With the DEBUG trap you could of course do anything, but listing the programs you want to use proxychains with, or the ones without it might get annoying. Which leads me to think if it would be easier to just alias p=proxychains and then go with p nmap... when you need it...
    – ilkkachu
    Dec 17 '17 at 11:03












    up vote
    1
    down vote













    You can just create an alias for your current sesion.



    alias nmap="proxychains nmap"


    Everytime you write nmap, it would be as writting "proxychains nmap".
    You can turn it off with:



    unalias nmap


    You could also create a shell script to manage this. Just add it to your bashrc



    function prefix 
    if [ $2 != "with" ]; then
    echo "You have to prefix something with something"
    return
    fi
    echo "alias $1 = $3 $1"

    alias $1="$3 $1"

    function unprefix sed "s/=.b.*b /='/g")



    So you can do:



    prefix nmap with proxychains
    unprefix nmap





    share|improve this answer


























      up vote
      1
      down vote













      You can just create an alias for your current sesion.



      alias nmap="proxychains nmap"


      Everytime you write nmap, it would be as writting "proxychains nmap".
      You can turn it off with:



      unalias nmap


      You could also create a shell script to manage this. Just add it to your bashrc



      function prefix 
      if [ $2 != "with" ]; then
      echo "You have to prefix something with something"
      return
      fi
      echo "alias $1 = $3 $1"

      alias $1="$3 $1"

      function unprefix sed "s/=.b.*b /='/g")



      So you can do:



      prefix nmap with proxychains
      unprefix nmap





      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        You can just create an alias for your current sesion.



        alias nmap="proxychains nmap"


        Everytime you write nmap, it would be as writting "proxychains nmap".
        You can turn it off with:



        unalias nmap


        You could also create a shell script to manage this. Just add it to your bashrc



        function prefix 
        if [ $2 != "with" ]; then
        echo "You have to prefix something with something"
        return
        fi
        echo "alias $1 = $3 $1"

        alias $1="$3 $1"

        function unprefix sed "s/=.b.*b /='/g")



        So you can do:



        prefix nmap with proxychains
        unprefix nmap





        share|improve this answer














        You can just create an alias for your current sesion.



        alias nmap="proxychains nmap"


        Everytime you write nmap, it would be as writting "proxychains nmap".
        You can turn it off with:



        unalias nmap


        You could also create a shell script to manage this. Just add it to your bashrc



        function prefix 
        if [ $2 != "with" ]; then
        echo "You have to prefix something with something"
        return
        fi
        echo "alias $1 = $3 $1"

        alias $1="$3 $1"

        function unprefix sed "s/=.b.*b /='/g")



        So you can do:



        prefix nmap with proxychains
        unprefix nmap






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 16 '17 at 15:07

























        answered Dec 16 '17 at 14:29









        WooWapDaBug

        16411




        16411






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f411260%2fadd-a-command-to-each-command-e-g-proxychains-each-command-by-default%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)