Search code examples
linuxbashshiptables

Auto accept "Configuring iptables-persistent"


i'm created bash script where i need to configure network, but in one of stage he ask me to save new rule, how to prevent it and set "Yes" automatically.

sudo iptables -t nat -A PREROUTING -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 1443

sudo apt-get -y install iptables-persistent

sudo service netfilter-persistent save

message where he ask to save


Solution

  • For non-terminal programs

    Actually, you can always automatize input of eny word with several methods:

     yes word | command
    

    (in this word will be entered as an input of command) or using expect, if you need more sophisticated dialog.

    For terminal programs (your case)

    For interactive sessions you can try it this way:

    1. start it in a tmux session
    2. send "Enter" using tmux send-keys

    That is exactly what you want.

    tmux send-keys Enter
    

    Everything combined:

    sudo tmux -L dialog-session new-session -d service netfilter-persistent save
    sudo tmux -L dialog-session send-keys Enter
    

    (actually you don't need sudo here, but I use sudo because of your sudo)