Skip to content

Commit b70b4bc

Browse files
committed
net: shell: Add network interface up/down command
User is able to take a network interface down or bring it up. The command syntax is "net iface [up|down] [index]" Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 95f1432 commit b70b4bc

File tree

1 file changed

+69
-5
lines changed

1 file changed

+69
-5
lines changed

subsys/net/ip/net_shell.c

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,14 +1581,76 @@ int net_shell_cmd_http(int argc, char *argv[])
15811581

15821582
int net_shell_cmd_iface(int argc, char *argv[])
15831583
{
1584-
ARG_UNUSED(argc);
1585-
ARG_UNUSED(argv);
1584+
int arg = 0;
1585+
bool up = false;
1586+
char *endptr = NULL;
1587+
struct net_if *iface;
1588+
int idx, ret;
1589+
1590+
if (strcmp(argv[arg], "iface") == 0) {
1591+
arg++;
1592+
}
15861593

1594+
if (!argv[arg]) {
15871595
#if defined(CONFIG_NET_HOSTNAME_ENABLE)
1588-
printk("Hostname: %s\n\n", net_hostname_get());
1596+
printk("Hostname: %s\n\n", net_hostname_get());
15891597
#endif
1598+
net_if_foreach(iface_cb, NULL);
1599+
1600+
return 0;
1601+
}
1602+
1603+
if (strcmp(argv[arg], "up") == 0) {
1604+
arg++;
1605+
up = true;
1606+
} else if (strcmp(argv[arg], "down") == 0) {
1607+
arg++;
1608+
}
1609+
1610+
if (!argv[arg]) {
1611+
printk("Usage: net iface [up|down] [index]\n");
1612+
return 0;
1613+
}
1614+
1615+
idx = strtol(argv[arg], &endptr, 10);
1616+
if (*endptr != '\0') {
1617+
printk("Invalid index %s\n", argv[arg]);
1618+
return 0;
1619+
}
1620+
1621+
if (idx < 0 || idx > 255) {
1622+
printk("Invalid index %d\n", idx);
1623+
return 0;
1624+
}
1625+
1626+
iface = net_if_get_by_index(idx);
1627+
if (!iface) {
1628+
printk("No such interface in index %d\n", idx);
1629+
return 0;
1630+
}
15901631

1591-
net_if_foreach(iface_cb, NULL);
1632+
if (up) {
1633+
if (net_if_is_up(iface)) {
1634+
printk("Interface %d is already up.\n", idx);
1635+
return 0;
1636+
}
1637+
1638+
ret = net_if_up(iface);
1639+
if (ret) {
1640+
printk("Cannot take interface %d up (%d)\n",
1641+
idx, ret);
1642+
} else {
1643+
printk("Interface %d is up\n", idx);
1644+
}
1645+
} else {
1646+
ret = net_if_down(iface);
1647+
if (ret) {
1648+
printk("Cannot take interface %d down (%d)\n",
1649+
idx, ret);
1650+
} else {
1651+
printk("Interface %d is down\n", idx);
1652+
}
1653+
}
15921654

15931655
return 0;
15941656
}
@@ -2786,7 +2848,9 @@ static struct shell_cmd net_commands[] = {
27862848
"http monitor\n\tStart monitoring HTTP connections\n"
27872849
"http\n\tTurn off HTTP connection monitoring" },
27882850
{ "iface", net_shell_cmd_iface,
2789-
"\n\tPrint information about network interfaces" },
2851+
"\n\tPrint information about network interfaces\n"
2852+
"iface up [idx]\n\tTake network interface up\n"
2853+
"iface down [idx]\n\tTake network interface down" },
27902854
{ "mem", net_shell_cmd_mem,
27912855
"\n\tPrint information about network interfaces" },
27922856
{ "nbr", net_shell_cmd_nbr, "\n\tPrint neighbor information\n"

0 commit comments

Comments
 (0)