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 iptables
if you can match the script e.g. by uid/gid.
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:
As root:
ip4_address=... # fill in your ipv4 address
group=secondaryip # or any other name
groupadd $group
usermod your-user -aG $group # not needed for the root user
iptables -t nat -A POSTROUTING \
-m owner --gid-owner $group \
-j SNAT --to-source $ip4_address
Run the script using:
sg secondaryip "/path/to/your/script arg1 arg2"
Alternatively you can invoke individual commands inside your scripts using sg
. Read its manual page for more details. Additionally there are tricks to make your script run itself with sg
command but you shouldn't need that much. You can use the classic newgrp
command and pass it a shell script via stdin
but that won't work for perl (mentioned by your comment).
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. connect()
with your own version that would perform additional actions such as binding to a specific source address. But it's a bit advanced C programming and you'd better stick with conventional waepons :).
That depends on the commands your script is invoking. – Der Hochstapler – 2013-10-14T17:43:09.753
It doesn't actually use any commands as one of the script is network related and keeps track of data, scans a list of servers off a master IP and updates data. Pure programming I can say. I have tried using "bind-address" commands which you might be referring to but they are command related, true. – Asad Moeen – 2013-10-14T17:45:40.140