vtysh.c
来自「zebra测试源代码用于 SOCKET 通信」· C语言 代码 · 共 2,119 行 · 第 1/4 页
C
2,119 行
|| vty->node == BGP_IPV6_NODE) vty->node = BGP_NODE; return CMD_SUCCESS;}DEFUNSH (VTYSH_ZEBRA, vtysh_exit_zebra, vtysh_exit_zebra_cmd, "exit", "Exit current mode and down to previous mode\n"){ return vtysh_exit (vty);}ALIAS (vtysh_exit_zebra, vtysh_quit_zebra_cmd, "quit", "Exit current mode and down to previous mode\n");DEFUNSH (VTYSH_RIPD, vtysh_exit_ripd, vtysh_exit_ripd_cmd, "exit", "Exit current mode and down to previous mode\n"){ return vtysh_exit (vty);}ALIAS (vtysh_exit_ripd, vtysh_quit_ripd_cmd, "quit", "Exit current mode and down to previous mode\n");DEFUNSH (VTYSH_RIPNGD, vtysh_exit_ripngd, vtysh_exit_ripngd_cmd, "exit", "Exit current mode and down to previous mode\n"){ return vtysh_exit (vty);}ALIAS (vtysh_exit_ripngd, vtysh_quit_ripngd_cmd, "quit", "Exit current mode and down to previous mode\n");DEFUNSH (VTYSH_RMAP, vtysh_exit_rmap, vtysh_exit_rmap_cmd, "exit", "Exit current mode and down to previous mode\n"){ return vtysh_exit (vty);}ALIAS (vtysh_exit_rmap, vtysh_quit_rmap_cmd, "quit", "Exit current mode and down to previous mode\n");DEFUNSH (VTYSH_BGPD, vtysh_exit_bgpd, vtysh_exit_bgpd_cmd, "exit", "Exit current mode and down to previous mode\n"){ return vtysh_exit (vty);}ALIAS (vtysh_exit_bgpd, vtysh_quit_bgpd_cmd, "quit", "Exit current mode and down to previous mode\n");DEFUNSH (VTYSH_OSPFD, vtysh_exit_ospfd, vtysh_exit_ospfd_cmd, "exit", "Exit current mode and down to previous mode\n"){ return vtysh_exit (vty);}ALIAS (vtysh_exit_ospfd, vtysh_quit_ospfd_cmd, "quit", "Exit current mode and down to previous mode\n");DEFUNSH (VTYSH_OSPF6D, vtysh_exit_ospf6d, vtysh_exit_ospf6d_cmd, "exit", "Exit current mode and down to previous mode\n"){ return vtysh_exit (vty);}ALIAS (vtysh_exit_ospf6d, vtysh_quit_ospf6d_cmd, "quit", "Exit current mode and down to previous mode\n");DEFUNSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD|VTYSH_OSPF6D, vtysh_interface, vtysh_interface_cmd, "interface IFNAME", "Select an interface to configure\n" "Interface's name\n"){vty_out (vty, "sunl 3%s", VTY_NEWLINE); vty->node = INTERFACE_NODE; return CMD_SUCCESS;}DEFUNSH (VTYSH_ZEBRA|VTYSH_RIPD|VTYSH_OSPFD|VTYSH_OSPF6D, vtysh_exit_interface, vtysh_exit_interface_cmd, "exit", "Exit current mode and down to previous mode\n"){ return vtysh_exit (vty);}ALIAS (vtysh_exit_interface, vtysh_quit_interface_cmd, "quit", "Exit current mode and down to previous mode\n");DEFUN (vtysh_write_terminal, vtysh_write_terminal_cmd, "write terminal", "Write running configuration to memory, network, or terminal\n" "Write to terminal\n"){ int ret; char line[] = "write terminal\n"; FILE *fp = NULL; fp = stdout; vty_out (vty, "Building configuration...%s", VTY_NEWLINE); vty_out (vty, "%sCurrent configuration:%s", VTY_NEWLINE, VTY_NEWLINE); /*----------sunl------------*/ vty_out (vty, "!%s", VTY_NEWLINE);/*--------------------------*/ vtysh_config_write (fp); ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ZEBRA], line); ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIP], line); ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIPNG], line); ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF], line); ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF6], line); ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_BGP], line); vtysh_config_dump (fp);/*-------sunl--move to here-----*/#if 1/*----------sunl------------*/ vty_out (vty, "!%s", VTY_NEWLINE); /*lan negotiation*/ vty_out (vty, "lan negotiation %s%s", lanneg[0].mode, VTY_NEWLINE); vty_out (vty, "!%s", VTY_NEWLINE);/*----------sunl------------*/#endifif (vtysh_pager_name) { fp = popen ("more", "w"); if (fp == NULL) { perror ("popen"); exit (1); } } else fp = stdout;/*-------------------------*/ if (vtysh_pager_name && fp) { fflush (fp); if (pclose (fp) == -1) { perror ("pclose"); exit (1); } fp = NULL; } return CMD_SUCCESS;}DEFUN (vtysh_write_memory, vtysh_write_memory_cmd, "write memory", "Write running configuration to memory, network, or terminal\n" "Write configuration to the file (same as write file)\n"){ int ret; mode_t old_umask; char line[] = "write terminal\n"; FILE *fp; char *integrate_sav = NULL; /* config files have 0600 perms... */ old_umask = umask (0077); integrate_sav = malloc (strlen (integrate_default) + strlen (CONF_BACKUP_EXT) + 1); strcpy (integrate_sav, integrate_default); strcat (integrate_sav, CONF_BACKUP_EXT); printf ("Building Configuration...\n"); /* Move current configuration file to backup config file */ unlink (integrate_sav); rename (integrate_default, integrate_sav); fp = fopen (integrate_default, "w"); if (fp == NULL) { printf ("%% Can't open configuration file %s.\n", integrate_default); umask (old_umask); return CMD_SUCCESS; } else printf ("[OK]\n"); vtysh_config_write (fp); ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ZEBRA], line); ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIP], line); ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIPNG], line); ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF], line); ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF6], line); ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_BGP], line); vtysh_config_dump (fp); fclose (fp); umask (old_umask); return CMD_SUCCESS;}ALIAS (vtysh_write_memory, vtysh_copy_runningconfig_startupconfig_cmd, "copy running-config startup-config", "Copy from one file to another\n" "Copy from current system configuration\n" "Copy to startup configuration\n");ALIAS (vtysh_write_memory, vtysh_write_file_cmd, "write file", "Write running configuration to memory, network, or terminal\n" "Write configuration to the file (same as write memory)\n");ALIAS (vtysh_write_terminal, vtysh_show_running_config_cmd, "show running-config", SHOW_STR "Current operating configuration\n");/* Execute command in child process. */intexecute_command (char *command, int argc, char *arg1, char *arg2){ int ret; pid_t pid; int status; /* Call fork(). */ pid = fork (); if (pid < 0) { /* Failure of fork(). */ fprintf (stderr, "Can't fork: %s\n", strerror (errno)); exit (1); } else if (pid == 0) { /* This is child process. */ switch (argc) { case 0: ret = execlp (command, command, NULL); break; case 1: ret = execlp (command, command, arg1, NULL); break; case 2: ret = execlp (command, command, arg1, arg2, NULL); break; } /* When execlp suceed, this part is not executed. */ fprintf (stderr, "Can't execute %s: %s\n", command, strerror (errno)); exit (1); } else { /* This is parent. */ execute_flag = 1; ret = wait4 (pid, &status, 0, NULL); execute_flag = 0; } return 0;}DEFUN (vtysh_ping, vtysh_ping_cmd, "ping WORD", "send echo messages\n" "Ping destination address or hostname\n"){ execute_command ("ping", 1, argv[0], NULL); return CMD_SUCCESS;}DEFUN (vtysh_traceroute, vtysh_traceroute_cmd, "traceroute WORD", "Trace route to destination\n" "Trace route to destination address or hostname\n"){ execute_command ("traceroute", 1, argv[0], NULL); return CMD_SUCCESS;}DEFUN (vtysh_telnet, vtysh_telnet_cmd, "telnet WORD", "Open a telnet connection\n" "IP address or hostname of a remote system\n"){ execute_command ("telnet", 1, argv[0], NULL); return CMD_SUCCESS;}DEFUN (vtysh_telnet_port, vtysh_telnet_port_cmd, "telnet WORD PORT", "Open a telnet connection\n" "IP address or hostname of a remote system\n" "TCP Port number\n"){ execute_command ("telnet", 2, argv[0], argv[1]); return CMD_SUCCESS;}DEFUN (vtysh_start_shell, vtysh_start_shell_cmd, "start-shell", "Start UNIX shell\n"){ execute_command ("sh", 0, NULL, NULL); return CMD_SUCCESS;}DEFUN (vtysh_start_bash, vtysh_start_bash_cmd, "start-shell bash", "Start UNIX shell\n" "Start bash\n"){ execute_command ("bash", 0, NULL, NULL); return CMD_SUCCESS;}DEFUN (vtysh_start_zsh, vtysh_start_zsh_cmd, "start-shell zsh", "Start UNIX shell\n" "Start Z shell\n"){ execute_command ("zsh", 0, NULL, NULL); return CMD_SUCCESS;}/* Route map node structure. */struct cmd_node rmap_node ={ RMAP_NODE, "%s(config-route-map)# "};/* Zebra node structure. */struct cmd_node zebra_node ={ ZEBRA_NODE, "%s(config-router)# "};struct cmd_node bgp_vpnv4_node ={ BGP_VPNV4_NODE, "%s(config-router-af)# "};struct cmd_node bgp_ipv4_node ={ BGP_IPV4_NODE, "%s(config-router-af)# "};struct cmd_node bgp_ipv4m_node ={ BGP_IPV4M_NODE, "%s(config-router-af)# "};struct cmd_node bgp_ipv6_node ={ BGP_IPV6_NODE, "%s(config-router-af)# "};struct cmd_node ospf_node ={ OSPF_NODE, "%s(config-router)# "};/* RIPng node structure. */struct cmd_node ripng_node ={ RIPNG_NODE, "%s(config-router)# "};/* OSPF6 node structure. */struct cmd_node ospf6_node ={ OSPF6_NODE, "%s(config-ospf6)# "};struct cmd_node keychain_node ={ KEYCHAIN_NODE, "%s(config-keychain)# "};struct cmd_node keychain_key_node ={ KEYCHAIN_KEY_NODE, "%s(config-keychain-key)# "};voidvtysh_install_default (enum node_type node){ install_element (node, &config_list_cmd);}/* Making connection to protocol daemon. */intvtysh_connect (struct vtysh_client *vclient, char *path){ int ret; int sock, len; struct sockaddr_un addr; struct stat s_stat; uid_t euid; gid_t egid; memset (vclient, 0, sizeof (struct vtysh_client)); vclient->fd = -1; /* Stat socket to see if we have permission to access it. */ euid = geteuid(); egid = getegid(); ret = stat (path, &s_stat); if (ret < 0 && errno != ENOENT) { fprintf (stderr, "vtysh_connect(%s): stat = %s\n", path, strerror(errno)); exit(1); } if (ret >= 0) { if (! S_ISSOCK(s_stat.st_mode)) { fprintf (stderr, "vtysh_connect(%s): Not a socket\n", path); exit (1); } if (euid != s_stat.st_uid || !(s_stat.st_mode & S_IWUSR) || !(s_stat.st_mode & S_IRUSR)) { fprintf (stderr, "vtysh_connect(%s): No permission to access socket\n", path); exit (1); } } sock = socket (AF_UNIX, SOCK_STREAM, 0); if (sock < 0) {#ifdef DEBUG fprintf(stderr, "vtysh_connect(%s): socket = %s\n", path, strerror(errno));#endif /* DEBUG */ return -1; } memset (&addr, 0, sizeof (struct sockaddr_un)); addr.sun_family = AF_UNIX; strncpy (addr.sun_path, path, strlen (path));#ifdef HAVE_SUN_LEN len = addr.sun_len = SUN_LEN(&addr);#else len = sizeof (addr.sun_family) + strlen (addr.sun_path);#endif /* HAVE_SUN_LEN */ ret = connect (sock, (struct sockaddr *) &addr, len); if (ret < 0) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?