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: <1998-06-13 06:19:01 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 ftp-program
"ftp"
180 "Progam to run to do FTP transfers."
185 (defcustom ftp-program-options nil
186 "List of options to pass to the FTP program."
188 :type
'(repeat string
)
191 (defcustom ftp-prompt-regexp
"^ftp>"
192 "Regexp which matches the FTP program's prompt."
197 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
199 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
201 (defconst nslookup-font-lock-keywords
206 (list nslookup-prompt-regexp
0 font-lock-reference-face
)
207 (list "^[A-Za-z0-9 _]+:" 0 font-lock-type-face
)
208 (list "\\<\\(SOA\\|NS\\|MX\\|A\\|CNAME\\)\\>"
209 1 font-lock-keyword-face
)
213 (make-list 4 "[0-9]+")
215 0 font-lock-variable-name-face
)
218 (let ((host-expression "[-A-Za-z0-9]+"))
221 (make-list 2 host-expression
)
223 "\\(\\." host-expression
"\\)*")
225 0 font-lock-variable-name-face
)
227 "Expressions to font-lock for nslookup.")
229 (defvar nslookup-abbrev-table
(make-abbrev-table)
230 "Abbrev table for nslookup.")
232 (define-abbrev nslookup-abbrev-table
"e" "exit")
233 (define-abbrev nslookup-abbrev-table
"f" "finger")
234 (define-abbrev nslookup-abbrev-table
"h" "help")
235 (define-abbrev nslookup-abbrev-table
"lse" "lserver")
236 (define-abbrev nslookup-abbrev-table
"r" "root")
237 (define-abbrev nslookup-abbrev-table
"s" "set")
238 (define-abbrev nslookup-abbrev-table
"se" "server")
239 (define-abbrev nslookup-abbrev-table
"v" "viewer")
241 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
243 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
245 (defvar ftp-abbrev-table
(make-abbrev-table)
246 "Abbrev table for ftp.")
248 (define-abbrev ftp-abbrev-table
"q" "quit")
249 (define-abbrev ftp-abbrev-table
"g" "get")
250 (define-abbrev ftp-abbrev-table
"p" "prompt")
251 (define-abbrev ftp-abbrev-table
"anon" "anonymous")
253 (defconst ftp-font-lock-keywords
258 (list ftp-prompt-regexp
0 font-lock-reference-face
)))))
261 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
263 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265 ;; Simplified versions of some at-point functions from ffap.el.
266 ;; It's not worth loading all of ffap just for these.
267 (defun net-utils-machine-at-point ()
269 (buffer-substring-no-properties
271 (skip-chars-backward "-a-zA-Z0-9.")
274 (skip-chars-forward "-a-zA-Z0-9.")
275 (skip-chars-backward "." pt
)
278 (defun net-utils-url-at-point ()
280 (buffer-substring-no-properties
282 (skip-chars-backward "--:=&?$+@-Z_a-z~#,%")
283 (skip-chars-forward "^A-Za-z0-9" pt
)
286 (skip-chars-forward "--:=&?$+@-Z_a-z~#,%")
287 (skip-chars-backward ":;.,!?" pt
)
291 (defun net-utils-remove-ctrl-m-filter (process output-string
)
292 "Remove trailing control Ms."
293 (let ((old-buffer (current-buffer))
294 (filtered-string output-string
))
297 (set-buffer (process-buffer process
))
298 (setq moving
(= (point) (process-mark process
)))
300 (while (string-match "\r" filtered-string
)
301 (setq filtered-string
302 (replace-match "" nil nil filtered-string
)))
305 ;; Insert the text, moving the process-marker.
306 (goto-char (process-mark process
))
307 (insert filtered-string
)
308 (set-marker (process-mark process
) (point)))
309 (if moving
(goto-char (process-mark process
))))
310 (set-buffer old-buffer
))))
312 (defmacro net-utils-run-program
(name header program
&rest args
)
313 "Run a network information program."
315 (let ((buf (get-buffer-create (concat "*" (, name
) "*"))))
318 (insert (, header
) "\n")
320 (apply 'start-process
(, name
) buf
(, program
) (,@ args
))
321 'net-utils-remove-ctrl-m-filter
)
322 (display-buffer buf
))))
324 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
325 ;; Wrappers for external network programs
326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
329 (defun traceroute (target)
330 "Run traceroute program for TARGET."
331 (interactive "sTarget: ")
333 (if traceroute-program-options
334 (append traceroute-program-options
(list target
))
336 (net-utils-run-program
337 (concat "Traceroute" " " target
)
338 (concat "** Traceroute ** " traceroute-program
" ** " target
)
346 If your system's ping continues until interrupted, you can try setting
347 `ping-program-options'."
349 (list (read-from-minibuffer "Ping host: " (net-utils-machine-at-point))))
351 (if ping-program-options
352 (append ping-program-options
(list host
))
354 (net-utils-run-program
355 (concat "Ping" " " host
)
356 (concat "** Ping ** " ping-program
" ** " host
)
363 "Run ipconfig program."
365 (net-utils-run-program
367 (concat "** Ipconfig ** " ipconfig-program
" ** ")
369 ipconfig-program-options
372 ;; This is the normal name on most Unixes.
374 (defalias 'ifconfig
'ipconfig
)
378 "Run netstat program."
380 (net-utils-run-program
382 (concat "** Netstat ** " netstat-program
" ** ")
384 netstat-program-options
389 "Run the arp program."
391 (net-utils-run-program
393 (concat "** Arp ** " arp-program
" ** ")
400 "Run the route program."
402 (net-utils-run-program
404 (concat "** Route ** " route-program
" ** ")
406 route-program-options
409 ;; FIXME -- Needs to be a process filter
410 ;; (defun netstat-with-filter (filter)
411 ;; "Run netstat program."
412 ;; (interactive "sFilter: ")
414 ;; (set-buffer (get-buffer "*Netstat*"))
415 ;; (goto-char (point-min))
416 ;; (delete-matching-lines filter)
420 (defun nslookup-host (host)
421 "Lookup the DNS information for HOST."
423 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
425 (if nslookup-program-options
426 (append nslookup-program-options
(list host
))
428 (net-utils-run-program
432 (list "Nslookup" host nslookup-program
)
441 "Run nslookup program."
444 (comint-run nslookup-program
)
445 (set-process-filter (get-buffer-process "*nslookup*")
446 'net-utils-remove-ctrl-m-filter
)
448 (make-local-variable 'font-lock-defaults
)
449 '((nslookup-font-lock-keywords)))
451 (make-local-variable 'local-abbrev-table
)
452 nslookup-abbrev-table
)
454 (make-local-variable 'comint-prompt-regexp
)
455 (setq comint-prompt-regexp nslookup-prompt-regexp
)
458 ;; This is a lot less than ange-ftp, but much simpler.
462 (interactive "sFtp to Host: ")
464 (let ((buf (get-buffer-create (concat "*ftp [" host
"]*"))))
467 (comint-exec buf
(concat "ftp-" host
) ftp-program nil
468 (if ftp-program-options
469 (append (list host
) ftp-program-options
)
472 (make-local-variable 'font-lock-defaults
)
473 '((ftp-font-lock-keywords)))
475 (make-local-variable 'comint-prompt-regexp
)
476 (setq comint-prompt-regexp ftp-prompt-regexp
)
478 ;; Already buffer local!
479 (setq comint-output-filter-functions
480 (list 'comint-watch-for-password-prompt
))
482 (make-local-variable 'local-abbrev-table
)
485 (switch-to-buffer-other-window buf
)
488 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
489 ;; Network Connections
490 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
492 ;; Full list is available at:
493 ;; ftp://ftp.isi.edu/in-notes/iana/assignments/port-numbers
494 (defvar network-connection-service-alist
497 (cons 'active-users
11)
513 (cons 'netbios-name
137)
514 (cons 'netbios-data
139)
519 "Alist of services and associated TCP port numbers.
520 This list in not complete.")
523 (defmacro run-network-program
(process-name host port
524 &optional initial-string
)
526 (let ((tcp-connection)
529 (setq buf
(get-buffer-create (concat "*" (, process-name
) "*")))
539 (error "Could not open connection to %s" (, host
)))
541 (set-marker (process-mark tcp-connection
) (point-min))
542 (set-process-filter tcp-connection
'net-utils-remove-ctrl-m-filter
)
543 (and (, initial-string
)
544 (process-send-string tcp-connection
545 (concat (, initial-string
) "\r\n")))
546 (display-buffer buf
))))
548 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
550 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
554 (defun finger (user host
)
555 "Finger USER on HOST."
556 ;; One of those great interactive statements that's actually
557 ;; longer than the function call! The idea is that if the user
558 ;; uses a string like "pbreton@cs.umb.edu", we won't ask for the
559 ;; host name. If we don't see an "@", we'll prompt for the host.
561 (let* ((answer (read-from-minibuffer "Finger User: "
562 (net-utils-url-at-point)))
563 (index (string-match (regexp-quote "@") answer
)))
566 (substring answer
0 index
)
567 (substring answer
(1+ index
)))
570 (read-from-minibuffer "At Host: " (net-utils-machine-at-point))))))
572 (user-and-host (concat user
"@" host
))
574 (concat "Finger [" user-and-host
"]"))
579 (cdr (assoc 'finger network-connection-service-alist
))
583 (defcustom whois-server-name
"whois.arin.net"
584 "Default host name for the whois service."
589 (defcustom whois-server-list
590 '(("whois.arin.net") ; Networks, ASN's, and related POC's (numbers)
591 ("rs.internic.net") ; domain related info
598 "A list of whois servers that can be queried."
600 :type
'(repeat (list string
)))
602 (defcustom whois-server-tld
603 '(("rs.internic.net" .
"com")
604 ("rs.internic.net" .
"org")
605 ("whois.ripe.net" .
"be")
606 ("whois.ripe.net" .
"de")
607 ("whois.ripe.net" .
"dk")
608 ("whois.ripe.net" .
"it")
609 ("whois.ripe.net" .
"fi")
610 ("whois.ripe.net" .
"fr")
611 ("whois.ripe.net" .
"uk")
612 ("whois.apnic.net" .
"au")
613 ("whois.apnic.net" .
"ch")
614 ("whois.apnic.net" .
"hk")
615 ("whois.apnic.net" .
"jp")
616 ("whois.nic.gov" .
"gov")
617 ("whois.nic.mil" .
"mil"))
618 "Alist to map top level domains to whois servers."
620 :type
'(repeat (cons string string
)))
622 (defcustom whois-guess-server t
623 "If non-nil then whois will try to deduce the appropriate whois
624 server from the query. If the query doesn't look like a domain or hostname
625 then the server named by whois-server-name is used."
629 (defun whois-get-tld (host)
630 "Return the top level domain of `host', or nil if it isn't a domain name."
631 (let ((i (1- (length host
)))
632 (max-len (- (length host
) 5)))
633 (while (not (or (= i max-len
) (char-equal (aref host i
) ?.
)))
637 (substring host
(1+ i
)))))
641 (defun whois (arg search-string
)
642 "Send SEARCH-STRING to server defined by the `whois-server-name' variable.
643 If `whois-guess-server' is non-nil, then try to deduce the correct server
644 from SEARCH-STRING. With argument, prompt for whois server."
645 (interactive "P\nsWhois: ")
646 (let* ((whois-apropos-host (if whois-guess-server
647 (rassoc (whois-get-tld search-string
)
650 (server-name (if whois-apropos-host
651 (car whois-apropos-host
)
655 (completing-read "Whois server name: "
656 whois-server-list nil nil
"whois.")
661 (cdr (assoc 'whois network-connection-service-alist
))
665 (defcustom whois-reverse-lookup-server
"whois.arin.net"
666 "Server which provides inverse DNS mapping."
672 (defun whois-reverse-lookup ()
674 (let ((whois-server-name whois-reverse-lookup-server
))
675 (call-interactively 'whois
)))
677 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
678 ;;; General Network connection
679 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
682 (defun network-connection-to-service (host service
)
683 "Open a network connection to SERVICE on HOST."
686 (read-from-minibuffer "Host: " (net-utils-machine-at-point))
687 (completing-read "Service: "
691 (list (symbol-name (car elt
)))))
692 network-connection-service-alist
))))
695 (cdr (assoc (intern service
) network-connection-service-alist
)))
699 (defun network-connection (host port
)
700 "Open a network connection to HOST on PORT."
701 (interactive "sHost: \nnPort: ")
702 (network-service-connection host
(number-to-string port
)))
704 (defun network-service-connection (host service
)
705 "Open a network connection to SERVICE on HOST."
708 (process-name (concat "Network Connection [" host
" " service
"]"))
709 (portnum (string-to-number service
))
711 (or (zerop portnum
) (setq service portnum
))
715 (pop-to-buffer (get-buffer (concat "*" process-name
"*")))
720 ;;; net-utils.el ends here