2

Need to remove default gateway. For example, there is an IP 192.168.4.15 with default gateway 192.168.4.14. I connect to WLAN with gw 10.0.0.1 and after that I would like do remove previous gw.

  IFS='.' read -ra IPARR <<< "$IP"
  Gateway="${IPARR[0]}.${IPARR[1]}.${IPARR[2]}.14"
  ssh blah@$IP '/sbin/route -v del default gw $Gateway;'
  #ssh blah@$IP '/sbin/ip route delete $Gateway dev rndis0;'
  #ssh blah@$IP '/sbin/route -n'

Both ways don't work. However, it is possible to remove it if I ssh into machine. My guess is that something wrong with passing $Gateway variable. Any suggestions?

5
  • You're using the wrong quotes. '' prevent variable expansion.
    – muru
    Dec 30 '14 at 10:52
  • 1
    @muru, or with ssh it will mean that the variable will get expanded on the remote host rather than locally where the Gateway variable has been set.
    – Graeme
    Dec 30 '14 at 11:10
  • @Graeme Indeed.
    – muru
    Dec 30 '14 at 11:12
  • Thanks guys, I have been trying to figure out what is wrong for a few hours already. Dec 30 '14 at 11:17
  • duplicate: stackoverflow.com/q/27703579/7552 Dec 30 '14 at 13:54
2

So, the answer is to use double quotes when ssh'ing into machine:

ssh blah@$IP "/sbin/route -v del default gw $Gateway;"
2
  • You should stop using obsolete commands, and start using ip instead: ssh blah@IP "/sbin/ip route del default via $Gateway" Dec 30 '14 at 14:29
  • @MariusMatutiae, could you explain more what is the difference between route and ip route? Dec 31 '14 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.