Kaveen K
Kaveen K

Reputation: 33

SSH and run commands mid-script

I'm writing a bash script to setup a GRE Tunnel, on both local and a remote machine.

How would I be able to (in the middle of the script) be able to have a piece of code that logs into the remote machine, runs the required iptables commands, and logs out, then continues with the setup on the LOCAL machine?

Upvotes: 0

Views: 175

Answers (1)

MeetTitan
MeetTitan

Reputation: 3568

If the client machine is running bash as well, and has the OpenSshClient installed: you can just run ssh user@host yourCommandToRunWithoutPty. This runs the command WITHOUT a pty/tty, which is important is some cases, such as sudo (sudo expects a tty to ask for password).

Because of this, I would suggest adding passwordless access to that command by that user in your server's /etc/sudoers, if (securely!) possible.

If configured correctly, your client should be able to just run ssh user@host sudo iptables --some-iptables-switches.

NOTE When adding passwordless commands to your /etc/sudoers, remember to always be as explicit as possible with your arguments, so no one can abuse arguments unintented to be ran without a sudo password.

Upvotes: 1

Related Questions