Add --host file to restrict fetches

This commit is contained in:
2025-07-06 00:36:38 +00:00
parent 9a1d39a4e8
commit f1d007122c
2 changed files with 27 additions and 3 deletions

View File

@ -8,6 +8,7 @@ ipng-router-backup \- SSH Router Backup Tool
.RI [ --password " PASSWORD" ] .RI [ --password " PASSWORD" ]
.RI [ --key-file " KEYFILE" ] .RI [ --key-file " KEYFILE" ]
.RI [ --port " PORT" ] .RI [ --port " PORT" ]
.RI [ --host " HOSTNAME" ]...
.SH DESCRIPTION .SH DESCRIPTION
.B router_backup .B router_backup
is a tool for backing up router configurations via SSH. It connects to multiple routers defined in a YAML configuration file and executes commands, saving the output to files. is a tool for backing up router configurations via SSH. It connects to multiple routers defined in a YAML configuration file and executes commands, saving the output to files.
@ -30,6 +31,9 @@ SSH private key file path
.BR --port " \fIPORT\fR" .BR --port " \fIPORT\fR"
SSH port number (default: 22) SSH port number (default: 22)
.TP .TP
.BR --host " \fIHOSTNAME\fR"
Specific host(s) to process (can be repeated, processes all if not specified)
.TP
.BR --help .BR --help
Show help message Show help message
.SH CONFIGURATION .SH CONFIGURATION
@ -81,6 +85,11 @@ Using password authentication:
.EX .EX
ipng-router-backup --config config.yaml --password mysecretpass ipng-router-backup --config config.yaml --password mysecretpass
.EE .EE
.TP
Process specific hosts only:
.EX
ipng-router-backup --config config.yaml --host asw100 --host asw120
.EE
.SH FILES .SH FILES
.TP .TP
.I /etc/ipng-router-backup/config.yaml.example .I /etc/ipng-router-backup/config.yaml.example

View File

@ -221,6 +221,7 @@ func main() {
var keyFile string var keyFile string
var port int var port int
var outputDir string var outputDir string
var hostFilter []string
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "ipng-router-backup", Use: "ipng-router-backup",
@ -250,10 +251,23 @@ func main() {
log.Fatal("No devices found in config file") log.Fatal("No devices found in config file")
} }
successCount := 0 // Filter devices if --host flags are provided
totalCount := len(config.Devices) devicesToProcess := config.Devices
if len(hostFilter) > 0 {
devicesToProcess = make(map[string]Device)
for _, hostname := range hostFilter {
if deviceConfig, exists := config.Devices[hostname]; exists {
devicesToProcess[hostname] = deviceConfig
} else {
fmt.Printf("Warning: Host '%s' not found in config file\n", hostname)
}
}
}
for hostname, deviceConfig := range config.Devices { successCount := 0
totalCount := len(devicesToProcess)
for hostname, deviceConfig := range devicesToProcess {
fmt.Printf("\nProcessing device: %s (type: %s)\n", hostname, deviceConfig.Type) fmt.Printf("\nProcessing device: %s (type: %s)\n", hostname, deviceConfig.Type)
user := deviceConfig.User user := deviceConfig.User
@ -306,6 +320,7 @@ func main() {
rootCmd.Flags().StringVar(&keyFile, "key-file", "", "SSH private key file path") rootCmd.Flags().StringVar(&keyFile, "key-file", "", "SSH private key file path")
rootCmd.Flags().IntVar(&port, "port", 22, "SSH port") rootCmd.Flags().IntVar(&port, "port", 22, "SSH port")
rootCmd.Flags().StringVar(&outputDir, "output-dir", "/tmp", "Output directory for command output files") rootCmd.Flags().StringVar(&outputDir, "output-dir", "/tmp", "Output directory for command output files")
rootCmd.Flags().StringSliceVar(&hostFilter, "host", []string{}, "Specific host(s) to process (can be repeated, processes all if not specified)")
rootCmd.MarkFlagRequired("config") rootCmd.MarkFlagRequired("config")