1 ;;; net-utils.el --- network functions
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Peter Breton <pbreton@cs.umb.edu>
7 ;; Created: Sun Mar 16 1997
8 ;; Keywords: network comm
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
30 ;; There are three main areas of functionality:
32 ;; * Wrap common network utility programs (ping, traceroute, netstat,
33 ;; nslookup, arp, route). Note that these wrappers are of the diagnostic
34 ;; functions of these programs only.
36 ;; * Implement some very basic protocols in Emacs Lisp (finger and whois)
38 ;; * Support connections to HOST/PORT, generally for debugging and the like.
39 ;; In other words, for doing much the same thing as "telnet HOST PORT", and
40 ;; then typing commands.
44 ;; On some systems, some of these programs are not in normal user path,
45 ;; but rather in /sbin, /usr/sbin, and so on.
52 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
53 ;; Customization Variables
54 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
56 (defgroup net-utils nil
57 "Network utility functions."
62 (defcustom net-utils-remove-ctl-m
63 (member system-type
(list 'windows-nt
'msdos
))
64 "If non-nil, remove control-Ms from output."
68 (defcustom traceroute-program
69 (if (eq system-type
'windows-nt
)
72 "Program to trace network hops to a destination."
76 (defcustom traceroute-program-options nil
77 "Options for the traceroute program."
79 :type
'(repeat string
))
81 (defcustom ping-program
"ping"
82 "Program to send network test packets to a host."
86 ;; On GNU/Linux and Irix, the system's ping program seems to send packets
87 ;; indefinitely unless told otherwise
88 (defcustom ping-program-options
89 (and (memq system-type
(list 'linux
'gnu
/linux
'irix
))
91 "Options for the ping program.
92 These options can be used to limit how many ICMP packets are emitted."
94 :type
'(repeat string
))
96 (defcustom ipconfig-program
97 (if (eq system-type
'windows-nt
)
100 "Program to print network configuration information."
104 (defcustom ipconfig-program-options
106 (if (eq system-type
'windows-nt
)
108 "Options for ipconfig-program."
110 :type
'(repeat string
))
112 (defcustom netstat-program
"netstat"
113 "Program to print network statistics."
117 (defcustom netstat-program-options
119 "Options for netstat-program."
121 :type
'(repeat string
))
123 (defcustom arp-program
"arp"
124 "Program to print IP to address translation tables."
128 (defcustom arp-program-options
130 "Options for arp-program."
132 :type
'(repeat string
))
134 (defcustom route-program
135 (if (eq system-type
'windows-nt
)
138 "Program to print routing tables."
142 (defcustom route-program-options
143 (if (eq system-type
'windows-nt
)
146 "Options for route-program."
148 :type
'(repeat string
))
150 (defcustom nslookup-program
"nslookup"
151 "Program to interactively query DNS information."
155 (defcustom nslookup-program-options nil
156 "List of options to pass to the nslookup program."
158 :type
'(repeat string
))
160 (defcustom nslookup-prompt-regexp
"^> "
161 "Regexp to match the nslookup prompt.
163 This variable is only used if the variable
164 `comint-use-prompt-regexp' is non-nil."
168 (defcustom dig-program
"dig"
169 "Program to query DNS information."
173 (defcustom ftp-program
"ftp"
174 "Progam to run to do FTP transfers."
178 (defcustom ftp-program-options nil
179 "List of options to pass to the FTP program."
181 :type
'(repeat string
))
183 (defcustom ftp-prompt-regexp
"^ftp>"
184 "Regexp which matches the FTP program's prompt.
186 This variable is only used if the variable
187 `comint-use-prompt-regexp' is non-nil."
191 (defcustom smbclient-program
"smbclient"
196 (defcustom smbclient-program-options nil
197 "List of options to pass to the smbclient program."
199 :type
'(repeat string
))
201 (defcustom smbclient-prompt-regexp
"^smb: \>"
202 "Regexp which matches the smbclient program's prompt.
204 This variable is only used if the variable
205 `comint-use-prompt-regexp' is non-nil."
209 (defcustom dns-lookup-program
"host"
210 "Program to interactively query DNS information."
215 (defcustom dns-lookup-program-options nil
216 "List of options to pass to the dns-lookup program."
218 :type
'(repeat string
)
221 ;; Internal variables
222 (defvar network-connection-service nil
)
223 (defvar network-connection-host nil
)
225 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
227 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
229 (defconst nslookup-font-lock-keywords
231 (defvar font-lock-type-face
)
232 (defvar font-lock-keyword-face
)
233 (defvar font-lock-variable-name-face
)
236 (list "^[A-Za-z0-9 _]+:" 0 font-lock-type-face
)
237 (list "\\<\\(SOA\\|NS\\|MX\\|A\\|CNAME\\)\\>"
238 1 font-lock-keyword-face
)
242 (make-list 4 "[0-9]+")
244 0 font-lock-variable-name-face
)
247 (let ((host-expression "[-A-Za-z0-9]+"))
250 (make-list 2 host-expression
)
252 "\\(\\." host-expression
"\\)*"))
253 0 font-lock-variable-name-face
)))
254 "Expressions to font-lock for nslookup.")
256 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
258 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
260 ;; Simplified versions of some at-point functions from ffap.el.
261 ;; It's not worth loading all of ffap just for these.
262 (defun net-utils-machine-at-point ()
264 (buffer-substring-no-properties
266 (skip-chars-backward "-a-zA-Z0-9.")
269 (skip-chars-forward "-a-zA-Z0-9.")
270 (skip-chars-backward "." pt
)
273 (defun net-utils-url-at-point ()
275 (buffer-substring-no-properties
277 (skip-chars-backward "--:=&?$+@-Z_a-z~#,%")
278 (skip-chars-forward "^A-Za-z0-9" pt
)
281 (skip-chars-forward "--:=&?$+@-Z_a-z~#,%")
282 (skip-chars-backward ":;.,!?" pt
)
286 (defun net-utils-remove-ctrl-m-filter (process output-string
)
287 "Remove trailing control Ms."
288 (let ((old-buffer (current-buffer))
289 (filtered-string output-string
))
292 (set-buffer (process-buffer process
))
293 (setq moving
(= (point) (process-mark process
)))
295 (while (string-match "\r" filtered-string
)
296 (setq filtered-string
297 (replace-match "" nil nil filtered-string
)))
300 ;; Insert the text, moving the process-marker.
301 (goto-char (process-mark process
))
302 (insert filtered-string
)
303 (set-marker (process-mark process
) (point)))
304 (if moving
(goto-char (process-mark process
))))
305 (set-buffer old-buffer
))))
307 (defmacro net-utils-run-program
(name header program
&rest args
)
308 "Run a network information program."
309 ` (let ((buf (get-buffer-create (concat "*" ,name
"*"))))
312 (insert ,header
"\n")
314 (apply 'start-process
,name buf
,program
,@args
)
315 'net-utils-remove-ctrl-m-filter
)
319 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
320 ;; Wrappers for external network programs
321 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
324 (defun traceroute (target)
325 "Run traceroute program for TARGET."
326 (interactive "sTarget: ")
328 (if traceroute-program-options
329 (append traceroute-program-options
(list target
))
331 (net-utils-run-program
332 (concat "Traceroute" " " target
)
333 (concat "** Traceroute ** " traceroute-program
" ** " target
)
340 If your system's ping continues until interrupted, you can try setting
341 `ping-program-options'."
343 (list (read-from-minibuffer "Ping host: " (net-utils-machine-at-point))))
345 (if ping-program-options
346 (append ping-program-options
(list host
))
348 (net-utils-run-program
349 (concat "Ping" " " host
)
350 (concat "** Ping ** " ping-program
" ** " host
)
356 "Run ipconfig program."
358 (net-utils-run-program
360 (concat "** Ipconfig ** " ipconfig-program
" ** ")
362 ipconfig-program-options
))
364 ;; This is the normal name on most Unixes.
366 (defalias 'ifconfig
'ipconfig
)
370 "Run netstat program."
372 (net-utils-run-program
374 (concat "** Netstat ** " netstat-program
" ** ")
376 netstat-program-options
))
380 "Run the arp program."
382 (net-utils-run-program
384 (concat "** Arp ** " arp-program
" ** ")
386 arp-program-options
))
390 "Run the route program."
392 (net-utils-run-program
394 (concat "** Route ** " route-program
" ** ")
396 route-program-options
))
398 ;; FIXME -- Needs to be a process filter
399 ;; (defun netstat-with-filter (filter)
400 ;; "Run netstat program."
401 ;; (interactive "sFilter: ")
403 ;; (set-buffer (get-buffer "*Netstat*"))
404 ;; (goto-char (point-min))
405 ;; (delete-matching-lines filter))
408 (defun nslookup-host (host)
409 "Lookup the DNS information for HOST."
411 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
413 (if nslookup-program-options
414 (append nslookup-program-options
(list host
))
416 (net-utils-run-program
420 (list "Nslookup" host nslookup-program
)
427 "Run nslookup program."
430 (comint-run nslookup-program
)
433 ;; Using a derived mode gives us keymaps, hooks, etc.
434 (define-derived-mode nslookup-mode comint-mode
"Nslookup"
435 "Major mode for interacting with the nslookup program."
437 (make-local-variable 'font-lock-defaults
)
438 '((nslookup-font-lock-keywords)))
439 (setq comint-prompt-regexp nslookup-prompt-regexp
)
440 (setq comint-input-autoexpand t
))
442 (define-key nslookup-mode-map
"\t" 'comint-dynamic-complete
)
445 (defun dns-lookup-host (host)
446 "Lookup the DNS information for HOST (name or IP address)."
448 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
450 (if dns-lookup-program-options
451 (append dns-lookup-program-options
(list host
))
453 (net-utils-run-program
454 (concat "DNS Lookup [" host
"]")
457 (list "DNS Lookup" host dns-lookup-program
)
464 (defun run-dig (host)
470 (read-from-minibuffer
473 (or (ffap-string-at-point 'machine
) ""))))))
474 (net-utils-run-program
478 (list "Dig" host dig-program
)
483 ;; This is a lot less than ange-ftp, but much simpler.
489 (read-from-minibuffer
490 "Ftp to Host: " (net-utils-machine-at-point))))
492 (let ((buf (get-buffer-create (concat "*ftp [" host
"]*"))))
495 (comint-exec buf
(concat "ftp-" host
) ftp-program nil
496 (if ftp-program-options
497 (append (list host
) ftp-program-options
)
499 (pop-to-buffer buf
)))
501 (define-derived-mode ftp-mode comint-mode
"FTP"
502 "Major mode for interacting with the ftp program."
503 (setq comint-prompt-regexp ftp-prompt-regexp
)
504 (setq comint-input-autoexpand t
)
505 ;; Only add the password-prompting hook if it's not already in the
506 ;; global hook list. This stands a small chance of losing, if it's
507 ;; later removed from the global list (very small, since any
508 ;; password prompts will probably immediately follow the initial
509 ;; connection), but it's better than getting prompted twice for the
511 (unless (memq 'comint-watch-for-password-prompt
512 (default-value 'comint-output-filter-functions
))
513 (add-hook 'comint-output-filter-functions
'comint-watch-for-password-prompt
516 ;; Occasionally useful
517 (define-key ftp-mode-map
"\t" 'comint-dynamic-complete
)
519 (defun smbclient (host service
)
520 "Connect to SERVICE on HOST via SMB."
523 (read-from-minibuffer
524 "Connect to Host: " (net-utils-machine-at-point))
525 (read-from-minibuffer "SMB Service: ")))
527 (let* ((name (format "smbclient [%s\\%s]" host service
))
528 (buf (get-buffer-create (concat "*" name
"*")))
529 (service-name (concat "\\\\" host
"\\" service
)))
532 (comint-exec buf name smbclient-program nil
533 (if smbclient-program-options
534 (append (list service-name
) smbclient-program-options
)
535 (list service-name
)))
536 (pop-to-buffer buf
)))
538 (defun smbclient-list-shares (host)
539 "List services on HOST."
542 (read-from-minibuffer
543 "Connect to Host: " (net-utils-machine-at-point))))
544 (let ((buf (get-buffer-create (format "*SMB Shares on %s*" host
))))
547 (comint-exec buf
"smbclient-list-shares"
548 smbclient-program nil
(list "-L" host
))
549 (pop-to-buffer buf
)))
551 (define-derived-mode smbclient-mode comint-mode
"smbclient"
552 "Major mode for interacting with the smbclient program."
553 (setq comint-prompt-regexp smbclient-prompt-regexp
)
554 (setq comint-input-autoexpand t
)
555 ;; Only add the password-prompting hook if it's not already in the
556 ;; global hook list. This stands a small chance of losing, if it's
557 ;; later removed from the global list (very small, since any
558 ;; password prompts will probably immediately follow the initial
559 ;; connection), but it's better than getting prompted twice for the
561 (unless (memq 'comint-watch-for-password-prompt
562 (default-value 'comint-output-filter-functions
))
563 (add-hook 'comint-output-filter-functions
'comint-watch-for-password-prompt
567 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
568 ;; Network Connections
569 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
571 ;; Full list is available at:
572 ;; http://www.iana.org/assignments/port-numbers
573 (defvar network-connection-service-alist
576 (cons 'active-users
11)
592 (cons 'netbios-name
137)
593 (cons 'netbios-data
139)
597 "Alist of services and associated TCP port numbers.
598 This list is not complete.")
601 (defmacro run-network-program
(process-name host port
602 &optional initial-string
)
603 `(let ((tcp-connection)
605 (setq buf
(get-buffer-create (concat "*" ,process-name
"*")))
614 (error "Could not open connection to %s" ,host
))
616 (set-marker (process-mark tcp-connection
) (point-min))
617 (set-process-filter tcp-connection
'net-utils-remove-ctrl-m-filter
)
619 (process-send-string tcp-connection
620 (concat ,initial-string
"\r\n")))
621 (display-buffer buf
)))
623 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
625 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
627 (defcustom finger-X
.500-host-regexps nil
628 "A list of regular expressions matching host names.
629 If a host name passed to `finger' matches one of these regular
630 expressions, it is assumed to be a host that doesn't accept
631 queries of the form USER@HOST, and wants a query containing USER only."
633 :type
'(repeat regexp
)
638 (defun finger (user host
)
639 "Finger USER on HOST."
640 ;; One of those great interactive statements that's actually
641 ;; longer than the function call! The idea is that if the user
642 ;; uses a string like "pbreton@cs.umb.edu", we won't ask for the
643 ;; host name. If we don't see an "@", we'll prompt for the host.
645 (let* ((answer (read-from-minibuffer "Finger User: "
646 (net-utils-url-at-point)))
647 (index (string-match (regexp-quote "@") answer
)))
649 (list (substring answer
0 index
)
650 (substring answer
(1+ index
)))
652 (read-from-minibuffer "At Host: "
653 (net-utils-machine-at-point))))))
654 (let* ((user-and-host (concat user
"@" host
))
655 (process-name (concat "Finger [" user-and-host
"]"))
656 (regexps finger-X
.500-host-regexps
)
659 (while (not (string-match (car regexps
) host
))
660 (setq regexps
(cdr regexps
)))
662 (setq user-and-host user
)))
666 (cdr (assoc 'finger network-connection-service-alist
))
669 (defcustom whois-server-name
"rs.internic.net"
670 "Default host name for the whois service."
674 (defcustom whois-server-list
675 '(("whois.arin.net") ; Networks, ASN's, and related POC's (numbers)
676 ("rs.internic.net") ; domain related info
677 ("whois.publicinterestregistry.net")
684 "A list of whois servers that can be queried."
686 :type
'(repeat (list string
)))
688 ;; FIXME: modern whois clients include a much better tld <-> whois server
689 ;; list, Emacs should probably avoid specifying the server as the client
690 ;; will DTRT anyway... -rfr
691 (defcustom whois-server-tld
692 '(("rs.internic.net" .
"com")
693 ("whois.publicinterestregistry.net" .
"org")
694 ("whois.ripe.net" .
"be")
695 ("whois.ripe.net" .
"de")
696 ("whois.ripe.net" .
"dk")
697 ("whois.ripe.net" .
"it")
698 ("whois.ripe.net" .
"fi")
699 ("whois.ripe.net" .
"fr")
700 ("whois.ripe.net" .
"uk")
701 ("whois.apnic.net" .
"au")
702 ("whois.apnic.net" .
"ch")
703 ("whois.apnic.net" .
"hk")
704 ("whois.apnic.net" .
"jp")
705 ("whois.nic.gov" .
"gov")
706 ("whois.nic.mil" .
"mil"))
707 "Alist to map top level domains to whois servers."
709 :type
'(repeat (cons string string
)))
711 (defcustom whois-guess-server t
712 "If non-nil then whois will try to deduce the appropriate whois
713 server from the query. If the query doesn't look like a domain or hostname
714 then the server named by `whois-server-name' is used."
718 (defun whois-get-tld (host)
719 "Return the top level domain of `host', or nil if it isn't a domain name."
720 (let ((i (1- (length host
)))
721 (max-len (- (length host
) 5)))
722 (while (not (or (= i max-len
) (char-equal (aref host i
) ?.
)))
726 (substring host
(1+ i
)))))
730 (defun whois (arg search-string
)
731 "Send SEARCH-STRING to server defined by the `whois-server-name' variable.
732 If `whois-guess-server' is non-nil, then try to deduce the correct server
733 from SEARCH-STRING. With argument, prompt for whois server."
734 (interactive "P\nsWhois: ")
735 (let* ((whois-apropos-host (if whois-guess-server
736 (rassoc (whois-get-tld search-string
)
739 (server-name (if whois-apropos-host
740 (car whois-apropos-host
)
744 (completing-read "Whois server name: "
745 whois-server-list nil nil
"whois.")
750 (cdr (assoc 'whois network-connection-service-alist
))
753 (defcustom whois-reverse-lookup-server
"whois.arin.net"
754 "Server which provides inverse DNS mapping."
759 (defun whois-reverse-lookup ()
761 (let ((whois-server-name whois-reverse-lookup-server
))
762 (call-interactively 'whois
)))
764 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
765 ;;; General Network connection
766 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
768 ;; Using a derived mode gives us keymaps, hooks, etc.
770 network-connection-mode comint-mode
"Network-Connection"
771 "Major mode for interacting with the network-connection program.")
773 (defun network-connection-mode-setup (host service
)
774 (make-local-variable 'network-connection-host
)
775 (setq network-connection-host host
)
776 (make-local-variable 'network-connection-service
)
777 (setq network-connection-service service
))
780 (defun network-connection-to-service (host service
)
781 "Open a network connection to SERVICE on HOST."
784 (read-from-minibuffer "Host: " (net-utils-machine-at-point))
785 (completing-read "Service: "
789 (list (symbol-name (car elt
)))))
790 network-connection-service-alist
))))
793 (cdr (assoc (intern service
) network-connection-service-alist
))))
796 (defun network-connection (host port
)
797 "Open a network connection to HOST on PORT."
798 (interactive "sHost: \nnPort: ")
799 (network-service-connection host
(number-to-string port
)))
801 (defun network-service-connection (host service
)
802 "Open a network connection to SERVICE on HOST."
804 (let* ((process-name (concat "Network Connection [" host
" " service
"]"))
805 (portnum (string-to-number service
))
806 (buf (get-buffer-create (concat "*" process-name
"*"))))
807 (or (zerop portnum
) (setq service portnum
))
812 (network-connection-mode)
813 (network-connection-mode-setup host service
)
814 (pop-to-buffer buf
)))
816 (defun network-connection-reconnect ()
817 "Reconnect a network connection, preserving the old input ring."
819 (let ((proc (get-buffer-process (current-buffer)))
820 (old-comint-input-ring comint-input-ring
)
821 (host network-connection-host
)
822 (service network-connection-service
))
823 (if (not (or (not proc
)
824 (eq (process-status proc
) 'closed
)))
825 (message "Still connected")
826 (goto-char (point-max))
827 (insert (format "Reopening connection to %s\n" host
))
828 (network-connection host
829 (if (numberp service
)
831 (cdr (assoc service network-connection-service-alist
))))
832 (and old-comint-input-ring
833 (setq comint-input-ring old-comint-input-ring
)))))
837 ;;; arch-tag: 97119e91-9edb-4376-838b-bf7058fa1314
838 ;;; net-utils.el ends here