-2

I have a VM in VirtualBox. Thanks to DHCP, it obtains a specific local IP. For example 192.168.18.16

I use upnpc (miniupnpc package) in order to create a redirection from internet to a specific port. For example, for FTP (port 21) I can create a redirection from 1621 external port to 21 port of the VM. I use cron with the line :

* * * * * upnpc -a 192.168.10.16 21 1621 TCP

My VM can be run in different network and obtains different IP such as 192.168.19.16, 192.168.30.16 ...

I don't want to modify manually the redirection depending of the networking where running. So I try in crontab -e :

* * * * * upnpc -a $(ifconfig eth0 | grep 'adr:' | cut -d: -f2 | awk '{ print $1}') 21 1621 TCP

But It's not working, why ? What could I do ?

Thx for your answers

Platypus
  • 3
  • 3
  • This site is intended for questions about programming. You're more likely to get answers to questions about using existing programs on [superuser](http://superuser.com) – simonc Jan 05 '16 at 09:12
  • Thx for your comment but I consider it as bash programming. Are you agreee ? – Platypus Jan 06 '16 at 14:07
  • Your `ifconfig eth0 | grep 'adr:' | cut -d: -f2 | awk '{ print $1}'` is probably buggy. Look at the output of ifconfig and fix it. For example on my machine, this yeld nothing, whereas `ifconfig eth0 | grep 'inet ' | awk '{ print $2}'` gives me 198.18.32.1, which is indeed the IP of my eth0. – jbm Jan 06 '16 at 17:02
  • @Platypus I hadn't realised you were asking about bash. There is a [bash] tag - it'd be worth adding this if you want to attract more feedback on your use of bash. – simonc Jan 06 '16 at 23:05
  • @simonc Excuse-me I change "command" tag to "bash" tag – Platypus Jan 07 '16 at 07:07
  • @jbm Thx for your answer. The command `ifconfig eth0 | grep 'adr:' | cut -d: -f2 | awk '{ print $1}'` works on a machine but not in others (debian one and ubuntu one) – Platypus Jan 07 '16 at 07:09

1 Answers1

0

I want to avoid to use a script for a one ligne command but this works :

vim redirect.sh

#!/bin/bash
IP=$(ifconfig eth0 | grep 'adr:' | cut -d: -f2 | awk '{ print $1}')

PORT_EXTERN=${$1:-0}
PORT_INTERN=${$2:-0}
PROTOCOL=${$3:-TCP}
COMMENT=$4
IP_DESTIN=${$5:-$IP}

upnpc -a $IP_DEST $PORT_INTERN PORT_EXTERN $PROTOCOL #-e $COMMENT

And in crontab -e :

* * * * * /path_to_script/redirect.sh 1621 21 TCP MY_REDIRECTION
Platypus
  • 3
  • 3