Heaute: Hab schnell mal was gehckt um in mehreren routing tabllen automatisiert das ageway zu finden udn dherauszufinden ob bei ner multihomed anbindung eien ipv4 lokal ist für ipup/ip-down scripts
#!/bin/bash
DBG=1
#address
#IA=`ip ad sh | sed -ne /"inet .* scope"/p | sed s/".*inet "// | sed s/" scope \(global\|host\)"// | sed s/" peer "/"-"/ | sed s/" brd "/"="/ | sed s/"=.* "/" "/`
#echo "${IA}"
#route
#IR=`ip ro sh | sed s/" proto kernel "// | sed s/" scope link "// | sed s/" via "/"-"/ | sed s/"dev "/""/ | sed s/" src "/"@"/ | sed s/" @"/"@"/ | sed s/"^default"/"0\/0"/ | sed s/" metric "/" ="/`
#echo "${IR}"
al() {
if [ "${IA}" == "" ] && [ "${1}" == "" ]; then
IA=`ip ad sh | sed -ne /"inet .* scope"/p | sed s/".*inet "// | sed s/" scope \(global\|host\)"// | sed s/" peer "/"-"/ | sed s/" brd "/"="/ | sed s/"=.* "/" "/`
else
if [ "${IA}" == "" ] && [ "${1}" != "" ]; then
IA=`ip ad sh dev ${1} sed -ne /"inet .* scope"/p | sed s/".*inet "// | sed s/" scope \(global\|host\)"// | sed s/" peer "/"-"/ | sed s/" brd "/"="/ | sed s/"=.* "/" "/`
fi
fi
echo "${IA}"
}
ad() {
if [ "${IA}" == "" ]; then
IA=`al`
fi
#echo "${IA}"
fnd=0;
echo "${IA}" | while IFS= read -r ia; do
i1=`echo "${ia}" | sed s/" .*"//`
i2=`echo "${ia}" | sed s/".* "//`
if [ "${i1}" != "${i2}" ]; then
iD=`echo ${i2}`
else
iD="-"
fi
iC=`echo "${i1}" | sed -ne /"-"/p`
if [ "${iC}" != "" ]; then
iP=`echo "${i1}" | sed s/".*-"//`
iC=`echo "${i1}" | sed s/"-.*"//`
else
iC=`echo ${i1}`
iP="-"
fi
#echo -n I1 ${i1} I2 ${i2} IA ${ia} IP ${ip}
iA=`echo "${iC}" | sed -ne /"\/"/p`
if [ "${iA}" != "" ]; then
iA=`echo "${iC}" | sed s/"\/.*"//`
iM=`echo "${iC}" | sed s/".*\/"//`
else
iA=`echo ${iC}`
iM="-"
fi
if [ "${iM}" == "-" ] && [ "${iA}" != "" ] && [ "${iP}" != "-" ]; then
iM=`echo "${iP}" | sed s/".*\/"//`
iP=`echo "${iP}" | sed s/"\/.*"//`
fi
#echo IC (Cidr) ${iC} IA (IPv4) ${iA} IM (BinaryNetmask) ${iM} ${iP} ID (Device) ${iD} IP (PointToPoint)
if [ "${1}" != "" ]; then
if [ "${iC}" == "${1}" ] || [ "${iA}" == "${1}" ]; then
echo ${iD}
fnd=-1
break
fi
else
echo ${iA} ${iM} ${iD} ${iP}
fi
done
# if [ "${fnd}" != "0" ]; then
# echo ""
# fi
}
if [ "${DBG}" != "0" ]; then
IA=`al`
echo "$IA"
echo "IPv4Address BinaryNetMask Interface/Device PoinToPointPeer"
AD=`ad`
echo "${AD}"
IP="127.0.0.1"
AD=`ad ${IP}`
if [ "${AD}" != "" ]; then
echo "${AD}"
else
echo NOTFOUND
fi
#exit 0
fi
rt() {
if [ "${IR}" == "" ] && [ "${1}" == "" ]; then
IR=`ip ro sh | sed s/" proto kernel "// | sed s/" scope link "// | sed s/" via "/"-"/ | sed s/"dev "/""/ | sed s/" src "/"@"/ | sed s/" @"/"@"/ | sed s/"^default"/"0\/0"/ | sed s/" metric "/" ="/`
else
if [ "${IR}" == "" ] && [ "${1}" != "" ]; then
IR=`ip ro sh table "${1}" | sed s/" proto kernel "// | sed s/" scope link "// | sed s/" via "/"-"/ | sed s/"dev "/""/ | sed s/" src "/"@"/ | sed s/" @"/"@"/ | sed s/"^default"/"0\/0"/ | sed s/" metric "/" ="/`
fi
fi
#destinations
ID=`echo "${IR}" |sed s/" .*"// | sed s/"-.*"//`
OLD="${ID}"
NEW=""
for old in ${OLD}; do
fnd=0
for new in ${NEW}; do
if [ "${old}" == "${new}" ]; then
fnd=-1
break
fi
done
if [ "${fnd}" == "0" ]; then
if [ "${NEW}" != "" ]; then
NEW="${NEW} "
fi
NEW="${NEW}${old}"
fi
done
ID="${NEW}"
#echo "${ID}"
#pad missing metric
IM=`echo "${IR}" | sed -ne /"="/p`
IN=`echo "${IR}" | sed /"="/d`
IN=`echo "${IN}" | sed s/"$"/"=0"/`
if [ "${IM}" != "0" ] && [ "${IN}" != "" ]; then
IT="\n"
else
IT="";
fi
IR=`echo -e "${IN}${IT}${IM}"`
#tidy up per destination
Ir=""
for id in ${ID}; do
iS=`echo "${id}" | sed s@'\/'@'\\\/'@g`
iR=`echo "${IR}" | sed -ne /"^${iS}[-\ ]"/p`
if [ "${Ir}" != "" ]; then
Ir="${Ir}\n"
fi
Ir="${Ir}${iR}"
done
IR=`echo -e "${Ir}"`
echo "${IR}" | while IFS= read -r ir; do
im=`echo "${ir}" | sed s/".*="//`
im=`echo ${im}`
i2=`echo "${ir}" | sed s/"=${im}"//`
i2=`echo ${i2}`
i1=`echo "${i2}" | sed s/" .*"//`
i2=`echo "${i2}" | sed s/".* "//`
iv=`echo "${i1}" | sed -ne /"-"/p`
if [ "${iv}" != "" ]; then
iv=`echo "${i1}" | sed s/".*-"//`
id=`echo "${i1}" | sed s/"-.*"//`
else
id=`echo ${i1}`
iv="-"
fi
ii=`echo "${i2}" | sed -ne /"@"/p`
if [ "${ii}" != "" ]; then
is=`echo "${i2}" | sed s/".*@"//`
ii=`echo "${i2}" | sed s/"@.*"//`
else
ii=`echo ${i2}`
is="-"
fi
echo "${id} ${im} ${iv} ${ii} ${is}"
done
}
IT=`rt`
echo "IPv4Route/BinaryNetMask Metric Gateway Interface/Device SourceIPv4Addr"
echo "$IT"
#rt | sed -ne /'^0\/0'/p
rd() {
if [ "${1}" == "" ]; then
DA="0/0"
else
DA=$1
fi
da=`echo "${DA}" | sed s@'\/'@'\\\/'@g`
if [ "${IT}" == "" ]; then
IT=`rt`
fi
ix="32768";
echo "${IT}" | sed -ne /"^$da"/p | while IFS="\ " read id im iv ii is; do
im=`echo ${im}`
if [ "${im}" -lt "${ix}" ]; then
ix="${im}"
# echo "smaller ${id} ${im} ${iv} ${ii} ${is}"
echo "${id} ${im} ${iv} ${ii} ${is}"
# else
# echo "to big ${id} ${im} ${iv} ${ii} ${is}"
fi
done
}
if [ "${DBG}" != "0" ]; then
DA="0/0"
RA=`rd ${DA}`
if [ "${RA}" == "" ]; then
echo "no default route in table main trying table default"
IT=`rt default`
echo "IPv4Route/BinaryNetMask Metric Gateway Interface/Device SourceIPv4Addr"
echo "$IT"
DA="0/0"
RA=`rd ${DA}`
fi
fi
GW=""
IF=""
SA=""
rx=1
for ra in ${RA}; do
if [ "${DBG}" != "0" ]; then
# echo $rx $ra
case "${rx}" in
1) echo -n "destination" ;;
2) echo -n "metric" ;;
3) echo -n "gateway/via" ;;
4) echo -n "interface/device" ;;
5) echo -n "source-address" ;;
esac
echo -n " : \""
echo -n "${ra}"
echo "\""
fi
case "${rx}" in
3) GW=${ra} ;;
4) IF=${ra} ;;
5) SA=${ra} ;;
esac
rx=$((rx + 1))
done
if [ "${DBG}" != "0" ]; then
echo via $GW dev $IF src $SA
fi
exit 0
# ip li sh | sed -ne /"^\([0-9]*\): "/p | sed s/"^\([0-9]*\): "// | sed s/":
sovile zu up/down
hier noch was zum thema rinetd für udp - jetzt auch mit v6 unterstützung
cat udp_redir-v6.c | sed /^$/d #| sed -ne /'\/\*'/,/'\*\/'/d
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[]) {
if (argc!=4) {
printf("Usage: %s port ip<:port> ip<:port>\n",argv[0]);
exit(1);
}
int osck;
struct sockaddr_in sadr_bnd;
struct sockaddr_in6 sadr_bnd6;
struct sockaddr_in sadr_one;
struct sockaddr_in6 sadr_one6;
struct sockaddr_in sadr_two;
struct sockaddr_in6 sadr_two6;
int is6bnd, is6one, is6two;
// ip4len = (4*3+3(.))=15 + (1(:)+5)=6 + '\0'=1 = 22
// ip6len = (8*4+7(:))=39 + (1(:)+5)=6 + '\0'=1 = 46
char addrbnd[40], addrone[40], addrtwo[40];
int portbnd, portone, porttwo;
char addr[40], port[6];
int zero, copy, offs;
char stda[40],stdp[6];
stda[0]='0'; stda[1]='.'; stda[2]='0'; stda[3]='.'; stda[4]='0'; stda[5]='.'; stda[6]='0';
zero=7; while (zero 45) {
printf("%s is not a valid address\n",argv[argx]);
exit(1);
}
argp=0;
argn=0;
argi=0;
while (argi = 1) && (argp > 0)) {
copy = 0;
while ((copy 0) || ((argp==0) && (argn==1))) {
offs = argp + 1;
}
copy = 0;
while (((copy + offs) = 1) {
copy = 0;
while ((copy 0) || ((argp==0) && (argn==1))) {
offs = argp + 1;
}
copy = 0;
while (((copy + offs) 1) {
if (!strcmp(addr,"0.0.0.0")) {
addr[0]='0'; addr[1]=':'; addr[2]='0'; addr[3]=':'; addr[4]='0'; addr[5]=':'; addr[6]='0'; addr[7]=':';
addr[8]='0'; addr[9]=':'; addr[10]='0'; addr[11]=':'; addr[12]='0'; addr[13]=':'; addr[14]='0';
}
}
if (argx == 1) {
printf("=== Bind ===\n",addr);
}
if (argx == 2) {
printf("--- First Peer ---\n",addr);
}
if (argx == 3) {
printf("--- Second Peer ---\n",addr);
}
// printf("Addr : %s\n",addr);
// if (argx == 1) printf(" : %s\n",stda);
if ((argx == 1) && (addr[0] == '\0')) {
copy=0; while (copy = 2) && (port[0] == '\0')) {
copy=0; while (copy = 2) printf(" : %s\n",stdp);
if (argx == 1) {
if (argn > 1) {
is6bnd=1;
osck=socket(PF_INET6,SOCK_DGRAM,IPPROTO_IP);
sadr_bnd6.sin6_family=AF_INET6;
if (inet_pton(AF_INET6, addr, &sadr_bnd6.sin6_addr) != 1) {
printf("Invalid IPv6 address (%s)\n", addr);
exit(1);
}
sadr_bnd6.sin6_port=htons(atoi(port));
if( bind(osck,(struct sockaddr *)&sadr_bnd6,sizeof(sadr_bnd6)) == -1) {
printf("Can't bind our address (%s:%s)\n", addr, port);
exit(1);
}
inet_ntop(AF_INET6, &sadr_bnd6.sin6_addr, addrbnd, INET6_ADDRSTRLEN);
} else {
is6bnd=0;
osck=socket(PF_INET,SOCK_DGRAM,IPPROTO_IP);
sadr_bnd.sin_family=AF_INET;
sadr_bnd.sin_addr.s_addr=inet_addr(addr);
sadr_bnd.sin_port=htons(atoi(port));
if( bind(osck,(struct sockaddr *)&sadr_bnd,sizeof(sadr_bnd)) == -1) {
printf("Can't bind our address (%s:%s)\n", addr, port);
exit(1);
}
copy = 0;
while ((copy 1) {
is6one=1;
sadr_one6.sin6_family=AF_INET6;
if (inet_pton(AF_INET6, addr, &sadr_one6.sin6_addr) != 1) {
printf("Invalid IPv6 address (%s)\n", addr);
exit(1);
}
sadr_one6.sin6_port=htons(atoi(port));
inet_ntop(AF_INET6, &sadr_one6.sin6_addr, addrone, INET6_ADDRSTRLEN);
} else {
is6one=0;
sadr_one.sin_family=AF_INET;
sadr_one.sin_addr.s_addr=inet_addr(addr);
sadr_one.sin_port=htons(atoi(port));
copy = 0;
while ((copy 1) {
is6two=1;
sadr_two6.sin6_family=AF_INET6;
if (inet_pton(AF_INET6, addr, &sadr_two6.sin6_addr) != 1) {
printf("Invalid IPv6 address (%s)\n", addr);
exit(1);
}
sadr_two6.sin6_port=htons(atoi(port));
inet_ntop(AF_INET6, &sadr_two6.sin6_addr, addrtwo, INET6_ADDRSTRLEN);
} else {
is6two=0;
sadr_two.sin_family=AF_INET;
sadr_two.sin_addr.s_addr=inet_addr(addr);
sadr_two.sin_port=htons(atoi(port));
copy = 0;
while ((copy */
}
#endif
m=0;
if (is6bnd != 0) {
if (is6one != 0) {
// if (sa6.sin6_addr==sadr_one6.sin6_addr) {
if (strcmp(addrone,addrsrc)==0) {
m=-1;
}
}
if (is6two != 0) {
// if (sa6.sin6_addr==sadr_two6.sin6_addr) {
if (strcmp(addrtwo,addrsrc)==0) {
m=-2;
}
}
} else {
if (is6one == 0) {
if (sa.sin_addr.s_addr==sadr_one.sin_addr.s_addr) {
m=1;
}
}
if (is6two == 0) {
if (sa.sin_addr.s_addr==sadr_two.sin_addr.s_addr) {
m=2;
}
}
}
if (m != 0) {
/*
#ifdef DEBUG
printf("match %d\n", m);
#endif
*/
if ((m == -1) || (m == 1)) {
if (is6two == 0) {
sendto(osck,buf,n,0,(struct sockaddr *)&sadr_two,sizeof(sadr_two));
} else {
sendto(osck,buf,n,0,(struct sockaddr *)&sadr_two6,sizeof(sadr_two6));
}
#ifdef DEBUG
printf("sendto (one2two) %s\n", addrtwo);
#endif
}
if ((m == -2) || (m == 2)) {
if (is6one == 0) {
sendto(osck,buf,n,0,(struct sockaddr *)&sadr_one,sizeof(sadr_one));
} else {
sendto(osck,buf,n,0,(struct sockaddr *)&sadr_one6,sizeof(sadr_one6));
}
#ifdef DEBUG
printf("sendto (two2one) %s\n", addrtwo);
#endif
}
}
/*
if ((sa.sin_addr.s_addr==sadr_one.sin_addr.s_addr) || (sa.sin_addr.s_addr==sadr_two.sin_addr.s_addr)) {
if (sa.sin_addr.s_addr==sadr_one.sin_addr.s_addr) {
sendto(osck,buf,n,0,(struct sockaddr *)&sadr_two,sizeof(sadr_two));
#ifdef DEBUG
printf("sendto (one2two) %s:%u\n", inet_ntoa(sadr_two.sin_addr), ntohs(sadr_two.sin_port));
#endif
}
if (sa.sin_addr.s_addr==sadr_two.sin_addr.s_addr) {
sendto(osck,buf,n,0,(struct sockaddr *)&sadr_one,sizeof(sadr_one));
#ifdef DEBUG
printf("sendto (two2one) %s:%u\n", inet_ntoa(sadr_one.sin_addr), ntohs(sadr_one.sin_port));
#endif
}
#ifdef DEBUG
} else {
//if(da.sin_addr.s_addr) sendto(osck,buf,n,0,(struct sockaddr *)&da,sizeof(da));
printf("nosendto (da) %s:%u\n", inet_ntoa(da.sin_addr), ntohs(da.sin_port));
printf("nosendto (sa) %s:%u\n", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
#endif
}
*/
}
exit(0);
}