parsed.org

Tips by tag: loop

Ipchains Loop by xinu on Sep 28, 2005 10:57 AM

If you need to fool a machine into believing that a host:port pair is local, you can use ipchains to redirect traffic. For example, the desired destination is www.example.com:80 and you want it to go to localhost:8080:

# echo '1' > /proc/sys/net/ipv4/ip_forward
# ipchains -A input -j REDIRECT 8080 -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 80

Note: No one really uses ipchains anymore, but it can be found on older systems.

commandsfirewallip_forwardipchainsloopredirectshell
Variable Indirection by xinu on Jan 31, 2006 10:06 AM

Sometimes you need to iterate over variable names and access their values. In shell script, you would do something like this to get the values of FOO and BAR:

$ FOO=apple
$ BAR=orange
$ VARS="FOO BAR"
$ for v in $VARS ; do echo ${!v} ; done

The above works in bash. Use this in Zsh:

$ for v in $VARS ; do echo ${(P)v} ; done

(The 'P' flag on ${v} causes a further variable lookup before ${v} is evaluated.)

Thanks to Cliff for the tip!

bashcommandsenvironmentevaluationiterationloopshellzsh
Xargs & SSH by xinu on Apr 19, 2006 05:48 AM

Let's say you want to check the uptime on a list of servers. We're assuming that you've got a key on each machine otherwise you'll be entering your password often:

$ xargs -i ssh {} uptime < ./server.list
  5:46am  up 155 days, 17:49,  2 users,  load average: 0.12, 0.03, 0.01
  5:46am  up 147 days, 17:14,  2 users,  load average: 0.02, 0.05, 0.01
  5:46am  up 209 days, 17:26,  0 users,  load average: 0.00, 0.00, 0.00
  5:46am  up 89 days,  6:30,  0 users,  load average: 0.00, 0.00, 0.00
  5:46am  up 82 days,  6:40,  0 users,  load average: 0.07, 0.06, 0.01
  5:46am  up 104 days,  9:51,  0 users,  load average: 0.03, 0.03, 0.00
  5:50am  up 68 days,  9:17,  0 users,  load average: 0.00, 0.00, 0.00
  5:48am  up 68 days,  9:15,  0 users,  load average: 0.00, 0.00, 0.00
commandskeyloopremoteserversshellsshuptimexargs
RSS