I have a Primary IP set for all the system related tasks. Sometimes I have to use my system to carry out different tests and most of the time I have to deal with bash/shell scripts. I want the tests/bandwidth used to be completely apart from the Primary IP so I want to carry out the operations using the Secondary IP. How can I achieve this?
This is not much about bash and not even so much about the commands you use in the script. Typically, the source IP is chosen according to the target IP. Some tools may let you specify the source IP or you can possibly rewrite the source IP using
AFAIK there's no way to attach a source IP addresses to a processes (and you would need also the subprocesses to inherit it). If you are ok with the source IP rewriting, you can switch the script process to a special group id:
Run the script using:
Alternatively you can invoke individual commands inside your scripts using
Note: The commands used in the script won't know about the modified source IP but that shouldn't generally be a problem.
For the sake of completeness, there's yet another way. You could use the LD_PRELOAD environment variable to replace some libc symbols, e.g.