I have three machines
Source Machine
Destination Machine - IP 10.20.30.40
Intermediate Machine - Host server1.test.com
Source machine has SSH access to Intermediate machine and Intermediate machine has SSH access to Destination Machine but no direct access from source to destination
There is a script running on source which needs to SSH to destination. Script has IP address of destination configured and I cannot change it to a host name or localhost (then I could've used SSH port forward using intermediate server)
I checked some articles and everywhere its suggested to use iptables to forward IP to different destination
So I've decided to use iptables on source to forward destination IP to localhost on port 1234 using this:
iptables -t nat -A PREROUTING -p tcp -d 10.20.30.40 --dport 22 -j DNAT --to 127.0.0.1:1234
and then forward local port 1234 via intermediate machine to destination like this:
ssh server1.test.com -L 1234:10.20.30.40:22
Is this a good way to do this or is there any simple solution available?