Scenario:
A SIP server with a public IP address is always under attack. This may not be a big problem in terms of security. However, especially if we rely on log files for debugging, they generate a large amount of logs and are quite noisy. This example shows how to filter out attack IP addresses from a SIP server by looking at the output of tcpdump.
example.spy
import re
badips = set()
patIP = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
with $ as console:
## There is a "0" as the second parameter of sendline()
console.sendline('tcpdump -n port 5060',0)
for line in console.lines():
## attacking sample:
## IP 184.105.190.50.5060 > 193.107.216.98.49651: SIP: SIP/2.0 407 Proxy Authentication Required
## in above sample, the bad ip is 193.107.216.98
if not 'Proxy Authentication Required' in line: continue
badips.append(patIP.findadd(line)[1])
## you have to break this loop explicitly
## since "tcpdump" would not stop by itself.
if len(badips) >= 150: break
## kill the running process of "tcpdump"
console.shutdown()
## output rules for iptables
for badip in badips:
print(f'-A INPUT -s {badip}/32 -j DROP')
execution
$sshscript example.spy
Last Updated: 2026-07-25 16:59:40
Table of contents
- SSHScript v3.1 Documentation
- Accounts (legacy URL)
- Checking the syncing status of two MySQL servers
- Getting Started with SSHScript
- Page not found
- Q: What can it do that Netmiko doesn’t do? (origin)
- SSHScript Documentation
- SSHScript Documents
- SSHScript v2.0 CLI(Draft)
- SSHScript v2.0 Commands Execution and Console
- SSHScript v2.0 Console of sudo and su
- SSHScript v2.0 Getting Started
- SSHScript v2.0 Interactive Commands and Foreground Commands
- SSHScript v2.0 Make Connections
- SSHScript v2.0 Recipes
- SSHScript v2.0 Reference Guide
- SSHScript v2.0 Stdout, Stderr and Exitcode
- SSHScript v2.0 Threading Support
- SSHScript v2.0 Upload and Download Files
- SSHScript v2.0.2 Release Notes
- SSHScript v3.0 Interactive and Foreground Commands
- SSHScript v3.0 Reference Guide
- SSHScript v3.0.0 Release Notes
- SSHScript v3.1
- SSHScript vs. Ansible
- 用 Python 作系統自動化