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

  IFS='.' read -ra IPARR <<< "$IP"
  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?

  • 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

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

ssh blah@$IP "/sbin/route -v del default gw $Gateway;"
  • 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.