1 ;;; net-utils.el --- Network functions
3 ;; Author: Peter Breton <pbreton@cs.umb.edu>
4 ;; Created: Sun Mar 16 1997
5 ;; Keywords: network communications
6 ;; Time-stamp: <1999-10-15 23:14:59 pbreton>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; There are three main areas of functionality:
29 ;; * Wrap common network utility programs (ping, traceroute, netstat,
30 ;; nslookup, arp, route). Note that these wrappers are of the diagnostic
31 ;; functions of these programs only.
33 ;; * Implement some very basic protocols in Emacs Lisp (finger and whois)
35 ;; * Support connections to HOST/PORT, generally for debugging and the like.
36 ;; In other words, for doing much the same thing as "telnet HOST PORT", and
37 ;; then typing commands.
41 ;; On some systems, some of these programs are not in normal user path,
42 ;; but rather in /sbin, /usr/sbin, and so on.
49 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
50 ;; Customization Variables
51 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
53 (defgroup net-utils nil
54 "Network utility functions."
60 (defcustom net-utils-remove-ctl-m
61 (member system-type
(list 'windows-nt
'msdos
))
62 "If non-nil, remove control-Ms from output."
67 (defcustom traceroute-program
68 (if (eq system-type
'windows-nt
)
71 "Program to trace network hops to a destination."
76 (defcustom traceroute-program-options nil
77 "Options for the traceroute program."
79 :type
'(repeat string
)
82 (defcustom ping-program
"ping"
83 "Program to send network test packets to a host."
88 ;; On Linux and Irix, the system's ping program seems to send packets
89 ;; indefinitely unless told otherwise
90 (defcustom ping-program-options
91 (and (memq system-type
(list 'linux
'gnu
/linux
'irix
))
93 "Options for the ping program.
94 These options can be used to limit how many ICMP packets are emitted."
96 :type
'(repeat string
)
99 (defcustom ipconfig-program
100 (if (eq system-type
'windows-nt
)
103 "Program to print network configuration information."
108 (defcustom ipconfig-program-options
110 (if (eq system-type
'windows-nt
)
112 "Options for ipconfig-program."
114 :type
'(repeat string
)
117 (defcustom netstat-program
"netstat"
118 "Program to print network statistics."
123 (defcustom netstat-program-options
125 "Options for netstat-program."
127 :type
'(repeat string
)
130 (defcustom arp-program
"arp"
131 "Program to print IP to address translation tables."
136 (defcustom arp-program-options
138 "Options for arp-program."
140 :type
'(repeat string
)
143 (defcustom route-program
144 (if (eq system-type
'windows-nt
)
147 "Program to print routing tables."
152 (defcustom route-program-options
153 (if (eq system-type
'windows-nt
)
156 "Options for route-program."
158 :type
'(repeat string
)
161 (defcustom nslookup-program
"nslookup"
162 "Program to interactively query DNS information."
167 (defcustom nslookup-program-options nil
168 "List of options to pass to the nslookup program."
170 :type
'(repeat string
)
173 (defcustom nslookup-prompt-regexp
"^> "
174 "Regexp to match the nslookup prompt."
179 (defcustom dig-program
"dig"
180 "Program to query DNS information."
185 (defcustom ftp-program
"ftp"
186 "Progam to run to do FTP transfers."
191 (defcustom ftp-program-options nil
192 "List of options to pass to the FTP program."
194 :type
'(repeat string
)
197 (defcustom ftp-prompt-regexp
"^ftp>"
198 "Regexp which matches the FTP program's prompt."
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
205 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
207 (defconst nslookup-font-lock-keywords
212 (list nslookup-prompt-regexp
0 font-lock-reference-face
)
213 (list "^[A-Za-z0-9 _]+:" 0 font-lock-type-face
)
214 (list "\\<\\(SOA\\|NS\\|MX\\|A\\|CNAME\\)\\>"
215 1 font-lock-keyword-face
)
219 (make-list 4 "[0-9]+")
221 0 font-lock-variable-name-face
)
224 (let ((host-expression "[-A-Za-z0-9]+"))
227 (make-list 2 host-expression
)
229 "\\(\\." host-expression
"\\)*")
231 0 font-lock-variable-name-face
)
233 "Expressions to font-lock for nslookup.")
235 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
237 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
239 (defconst ftp-font-lock-keywords
244 (list ftp-prompt-regexp
0 font-lock-reference-face
)))))
246 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
250 ;; Simplified versions of some at-point functions from ffap.el.
251 ;; It's not worth loading all of ffap just for these.
252 (defun net-utils-machine-at-point ()
254 (buffer-substring-no-properties
256 (skip-chars-backward "-a-zA-Z0-9.")
259 (skip-chars-forward "-a-zA-Z0-9.")
260 (skip-chars-backward "." pt
)
263 (defun net-utils-url-at-point ()
265 (buffer-substring-no-properties
267 (skip-chars-backward "--:=&?$+@-Z_a-z~#,%")
268 (skip-chars-forward "^A-Za-z0-9" pt
)
271 (skip-chars-forward "--:=&?$+@-Z_a-z~#,%")
272 (skip-chars-backward ":;.,!?" pt
)
276 (defun net-utils-remove-ctrl-m-filter (process output-string
)
277 "Remove trailing control Ms."
278 (let ((old-buffer (current-buffer))
279 (filtered-string output-string
))
282 (set-buffer (process-buffer process
))
283 (setq moving
(= (point) (process-mark process
)))
285 (while (string-match "\r" filtered-string
)
286 (setq filtered-string
287 (replace-match "" nil nil filtered-string
)))
290 ;; Insert the text, moving the process-marker.
291 (goto-char (process-mark process
))
292 (insert filtered-string
)
293 (set-marker (process-mark process
) (point)))
294 (if moving
(goto-char (process-mark process
))))
295 (set-buffer old-buffer
))))
297 (defmacro net-utils-run-program
(name header program
&rest args
)
298 "Run a network information program."
300 (let ((buf (get-buffer-create (concat "*" (, name
) "*"))))
303 (insert (, header
) "\n")
305 (apply 'start-process
(, name
) buf
(, program
) (,@ args
))
306 'net-utils-remove-ctrl-m-filter
)
307 (display-buffer buf
))))
309 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
310 ;; Wrappers for external network programs
311 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
314 (defun traceroute (target)
315 "Run traceroute program for TARGET."
316 (interactive "sTarget: ")
318 (if traceroute-program-options
319 (append traceroute-program-options
(list target
))
321 (net-utils-run-program
322 (concat "Traceroute" " " target
)
323 (concat "** Traceroute ** " traceroute-program
" ** " target
)
331 If your system's ping continues until interrupted, you can try setting
332 `ping-program-options'."
334 (list (read-from-minibuffer "Ping host: " (net-utils-machine-at-point))))
336 (if ping-program-options
337 (append ping-program-options
(list host
))
339 (net-utils-run-program
340 (concat "Ping" " " host
)
341 (concat "** Ping ** " ping-program
" ** " host
)
348 "Run ipconfig program."
350 (net-utils-run-program
352 (concat "** Ipconfig ** " ipconfig-program
" ** ")
354 ipconfig-program-options
357 ;; This is the normal name on most Unixes.
359 (defalias 'ifconfig
'ipconfig
)
363 "Run netstat program."
365 (net-utils-run-program
367 (concat "** Netstat ** " netstat-program
" ** ")
369 netstat-program-options
374 "Run the arp program."
376 (net-utils-run-program
378 (concat "** Arp ** " arp-program
" ** ")
385 "Run the route program."
387 (net-utils-run-program
389 (concat "** Route ** " route-program
" ** ")
391 route-program-options
394 ;; FIXME -- Needs to be a process filter
395 ;; (defun netstat-with-filter (filter)
396 ;; "Run netstat program."
397 ;; (interactive "sFilter: ")
399 ;; (set-buffer (get-buffer "*Netstat*"))
400 ;; (goto-char (point-min))
401 ;; (delete-matching-lines filter)
405 (defun nslookup-host (host)
406 "Lookup the DNS information for HOST."
408 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
410 (if nslookup-program-options
411 (append nslookup-program-options
(list host
))
413 (net-utils-run-program
417 (list "Nslookup" host nslookup-program
)
426 "Run nslookup program."
429 (comint-run nslookup-program
)
430 (set-process-filter (get-buffer-process "*nslookup*")
431 'net-utils-remove-ctrl-m-filter
)
435 ;; Using a derived mode gives us keymaps, hooks, etc.
437 nslookup-mode comint-mode
"Nslookup"
438 "Major mode for interacting with the nslookup program."
440 (make-local-variable 'font-lock-defaults
)
441 '((nslookup-font-lock-keywords)))
442 (setq local-abbrev-table nslookup-mode-abbrev-table
)
444 (make-local-variable 'comint-prompt-regexp
)
445 (setq comint-prompt-regexp nslookup-prompt-regexp
)
446 (make-local-variable 'comint-input-autoexpand
)
447 (setq comint-input-autoexpand t
)
450 (define-key nslookup-mode-map
"\t" 'comint-dynamic-complete
)
452 (define-abbrev nslookup-mode-abbrev-table
"e" "exit")
453 (define-abbrev nslookup-mode-abbrev-table
"f" "finger")
454 (define-abbrev nslookup-mode-abbrev-table
"h" "help")
455 (define-abbrev nslookup-mode-abbrev-table
"lse" "lserver")
456 (define-abbrev nslookup-mode-abbrev-table
"q" "exit")
457 (define-abbrev nslookup-mode-abbrev-table
"r" "root")
458 (define-abbrev nslookup-mode-abbrev-table
"s" "set")
459 (define-abbrev nslookup-mode-abbrev-table
"se" "server")
460 (define-abbrev nslookup-mode-abbrev-table
"v" "viewer")
469 (read-from-minibuffer
471 (or (ffap-string-at-point 'machine
) "")))))
472 (net-utils-run-program
476 (list "Dig" host dig-program
)
482 ;; This is a lot less than ange-ftp, but much simpler.
488 (read-from-minibuffer
489 "Ftp to Host: " (net-utils-machine-at-point))))
491 (let ((buf (get-buffer-create (concat "*ftp [" host
"]*"))))
494 (comint-exec buf
(concat "ftp-" host
) ftp-program nil
495 (if ftp-program-options
496 (append (list host
) ftp-program-options
)
499 (switch-to-buffer-other-window buf
)
503 ftp-mode comint-mode
"FTP"
504 "Major mode for interacting with the ftp program."
507 (make-local-variable 'font-lock-defaults
)
508 '((ftp-font-lock-keywords)))
510 (make-local-variable 'comint-prompt-regexp
)
511 (setq comint-prompt-regexp ftp-prompt-regexp
)
513 (make-local-variable 'comint-input-autoexpand
)
514 (setq comint-input-autoexpand t
)
516 ;; Already buffer local!
517 (setq comint-output-filter-functions
518 (list 'comint-watch-for-password-prompt
))
520 (setq local-abbrev-table ftp-mode-abbrev-table
)
524 (define-abbrev ftp-mode-abbrev-table
"q" "quit")
525 (define-abbrev ftp-mode-abbrev-table
"g" "get")
526 (define-abbrev ftp-mode-abbrev-table
"p" "prompt")
527 (define-abbrev ftp-mode-abbrev-table
"anon" "anonymous")
529 ;; Occasionally useful
530 (define-key ftp-mode-map
"\t" 'comint-dynamic-complete
)
532 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
533 ;; Network Connections
534 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
536 ;; Full list is available at:
537 ;; ftp://ftp.isi.edu/in-notes/iana/assignments/port-numbers
538 (defvar network-connection-service-alist
541 (cons 'active-users
11)
557 (cons 'netbios-name
137)
558 (cons 'netbios-data
139)
563 "Alist of services and associated TCP port numbers.
564 This list in not complete.")
567 (defmacro run-network-program
(process-name host port
568 &optional initial-string
)
570 (let ((tcp-connection)
573 (setq buf
(get-buffer-create (concat "*" (, process-name
) "*")))
583 (error "Could not open connection to %s" (, host
)))
585 (set-marker (process-mark tcp-connection
) (point-min))
586 (set-process-filter tcp-connection
'net-utils-remove-ctrl-m-filter
)
587 (and (, initial-string
)
588 (process-send-string tcp-connection
589 (concat (, initial-string
) "\r\n")))
590 (display-buffer buf
))))
592 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
594 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
598 (defun finger (user host
)
599 "Finger USER on HOST."
600 ;; One of those great interactive statements that's actually
601 ;; longer than the function call! The idea is that if the user
602 ;; uses a string like "pbreton@cs.umb.edu", we won't ask for the
603 ;; host name. If we don't see an "@", we'll prompt for the host.
605 (let* ((answer (read-from-minibuffer "Finger User: "
606 (net-utils-url-at-point)))
607 (index (string-match (regexp-quote "@") answer
)))
610 (substring answer
0 index
)
611 (substring answer
(1+ index
)))
614 (read-from-minibuffer "At Host: " (net-utils-machine-at-point))))))
616 (user-and-host (concat user
"@" host
))
618 (concat "Finger [" user-and-host
"]"))
623 (cdr (assoc 'finger network-connection-service-alist
))
627 (defcustom whois-server-name
"rs.internic.net"
628 "Default host name for the whois service."
633 (defcustom whois-server-list
634 '(("whois.arin.net") ; Networks, ASN's, and related POC's (numbers)
635 ("rs.internic.net") ; domain related info
642 "A list of whois servers that can be queried."
644 :type
'(repeat (list string
)))
646 (defcustom whois-server-tld
647 '(("rs.internic.net" .
"com")
648 ("rs.internic.net" .
"org")
649 ("whois.ripe.net" .
"be")
650 ("whois.ripe.net" .
"de")
651 ("whois.ripe.net" .
"dk")
652 ("whois.ripe.net" .
"it")
653 ("whois.ripe.net" .
"fi")
654 ("whois.ripe.net" .
"fr")
655 ("whois.ripe.net" .
"uk")
656 ("whois.apnic.net" .
"au")
657 ("whois.apnic.net" .
"ch")
658 ("whois.apnic.net" .
"hk")
659 ("whois.apnic.net" .
"jp")
660 ("whois.nic.gov" .
"gov")
661 ("whois.nic.mil" .
"mil"))
662 "Alist to map top level domains to whois servers."
664 :type
'(repeat (cons string string
)))
666 (defcustom whois-guess-server t
667 "If non-nil then whois will try to deduce the appropriate whois
668 server from the query. If the query doesn't look like a domain or hostname
669 then the server named by whois-server-name is used."
673 (defun whois-get-tld (host)
674 "Return the top level domain of `host', or nil if it isn't a domain name."
675 (let ((i (1- (length host
)))
676 (max-len (- (length host
) 5)))
677 (while (not (or (= i max-len
) (char-equal (aref host i
) ?.
)))
681 (substring host
(1+ i
)))))
685 (defun whois (arg search-string
)
686 "Send SEARCH-STRING to server defined by the `whois-server-name' variable.
687 If `whois-guess-server' is non-nil, then try to deduce the correct server
688 from SEARCH-STRING. With argument, prompt for whois server."
689 (interactive "P\nsWhois: ")
690 (let* ((whois-apropos-host (if whois-guess-server
691 (rassoc (whois-get-tld search-string
)
694 (server-name (if whois-apropos-host
695 (car whois-apropos-host
)
699 (completing-read "Whois server name: "
700 whois-server-list nil nil
"whois.")
705 (cdr (assoc 'whois network-connection-service-alist
))
709 (defcustom whois-reverse-lookup-server
"whois.arin.net"
710 "Server which provides inverse DNS mapping."
716 (defun whois-reverse-lookup ()
718 (let ((whois-server-name whois-reverse-lookup-server
))
719 (call-interactively 'whois
)))
721 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
722 ;;; General Network connection
723 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
726 (defun network-connection-to-service (host service
)
727 "Open a network connection to SERVICE on HOST."
730 (read-from-minibuffer "Host: " (net-utils-machine-at-point))
731 (completing-read "Service: "
735 (list (symbol-name (car elt
)))))
736 network-connection-service-alist
))))
739 (cdr (assoc (intern service
) network-connection-service-alist
)))
743 (defun network-connection (host port
)
744 "Open a network connection to HOST on PORT."
745 (interactive "sHost: \nnPort: ")
746 (network-service-connection host
(number-to-string port
)))
748 (defun network-service-connection (host service
)
749 "Open a network connection to SERVICE on HOST."
752 (process-name (concat "Network Connection [" host
" " service
"]"))
753 (portnum (string-to-number service
))
755 (or (zerop portnum
) (setq service portnum
))
759 (pop-to-buffer (get-buffer (concat "*" process-name
"*")))
764 ;;; net-utils.el ends here