Script expect/tcl para validar :
- Si paso un solo argumento
- Si el argumento es una IP v4 valida o la palabra “all”.
#!/bin/expect -f
set timeout 10
#Dont show any output from programs
log_user 0
#check the # of parameters. should be 1
if {[llength $argv] != 1} {
send_user "Exactly 1 argument expected. Valid argments: IP or all\n"
send_user "Usage: scriptname IP | all \n"
send_user "Example 1: scriptname 10.254.254.12\n"
send_user "Example 2: scriptname all\n"
exit 1
}
set argument [lindex $argv 0];
#This function check if the parameter is a valid ipv4
#from Mystic Odin https://stackoverflow.com/questions/10633281/validation-of-ip-address-using-tcl
proc isValidIp {IP} {
if {[regexp {^(?:(\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))(?:\.((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))){3}$} $IP]} {
return 1
} else {
return 0
}
}
#Evaluate the parameter
if {$argument eq "all"} {
set argument "all"
} elseif {[isValidIp $argument] eq 1 } {
set argument $argument
} else {
send_user "Invalid argument. \n";
exit 1
}
send_user "\n\nAfter check your parameter is valid ($argument) is OK you can do next steps ...\n\n";
Ejemplo de uso
[aurlen@monitor expect]$ ./check-ip.tcl all
After check your parameter is valid (all) is OK you can do next steps ...
[aurlen@monitor expect]$
[aurlen@monitor expect]$
[aurlen@monitor expect]$ ./check-ip.tcl 12.23.34.224
After check your parameter is valid (12.23.34.224) is OK you can do next steps ...
[aurlen@monitor expect]$
[aurlen@monitor expect]$ ./check-ip.tcl 12.23a.34.224
Invalid argument.
[aurlen@monitor expect]$