I have Python code in a script like this -
def run_ssh_cmd(host, cmd):
global i
i += 1
ql(f"Running cmd {i}: {cmd}", "info", host)
cmds = ["ssh", "-t", "-p", "22", host, cmd]
process = Popen(cmds, stdout=PIPE, stderr=PIPE, stdin=PIPE)
stdout, stderr = process.communicate()
# Check if the command was successful
if process.returncode != 0:
ql(f"Error occurred on cmd {cmd}: {stderr}", "error", host)
raise CalledProcessError(process.returncode, cmds, stderr)
This code giving a timeout error on the 6th command, every time, when I run it from my PC. The error states "ssh: connect to host sub1.domain.io port 22: Connection timed out"
Why would my computer consistently get a connection timeout after 5 successful commands?
The issue was that UFW was installed on our server and it blocks the 6th SSH connection attempt within 30 seconds. Disabling it allows the commands to work. Looking into configuration options or an alternative firewall, but this at least solves the mystery.