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-11-13 10: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 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 (defcustom smbclient-program
"smbclient"
209 (defcustom smbclient-program-options nil
210 "List of options to pass to the smbclient program."
212 :type
'(repeat string
)
215 (defcustom smbclient-prompt-regexp
"^smb: \>"
216 "Regexp which matches the smbclient program's prompt."
221 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
225 (defconst nslookup-font-lock-keywords
230 (list nslookup-prompt-regexp
0 font-lock-reference-face
)
231 (list "^[A-Za-z0-9 _]+:" 0 font-lock-type-face
)
232 (list "\\<\\(SOA\\|NS\\|MX\\|A\\|CNAME\\)\\>"
233 1 font-lock-keyword-face
)
237 (make-list 4 "[0-9]+")
239 0 font-lock-variable-name-face
)
242 (let ((host-expression "[-A-Za-z0-9]+"))
245 (make-list 2 host-expression
)
247 "\\(\\." host-expression
"\\)*")
249 0 font-lock-variable-name-face
)
251 "Expressions to font-lock for nslookup.")
253 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
255 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
257 (defconst ftp-font-lock-keywords
262 (list ftp-prompt-regexp
0 font-lock-reference-face
)))))
264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
266 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
268 (defconst smbclient-font-lock-keywords
273 (list smbclient-prompt-regexp
0 font-lock-reference-face
)))))
275 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
277 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
279 ;; Simplified versions of some at-point functions from ffap.el.
280 ;; It's not worth loading all of ffap just for these.
281 (defun net-utils-machine-at-point ()
283 (buffer-substring-no-properties
285 (skip-chars-backward "-a-zA-Z0-9.")
288 (skip-chars-forward "-a-zA-Z0-9.")
289 (skip-chars-backward "." pt
)
292 (defun net-utils-url-at-point ()
294 (buffer-substring-no-properties
296 (skip-chars-backward "--:=&?$+@-Z_a-z~#,%")
297 (skip-chars-forward "^A-Za-z0-9" pt
)
300 (skip-chars-forward "--:=&?$+@-Z_a-z~#,%")
301 (skip-chars-backward ":;.,!?" pt
)
305 (defun net-utils-remove-ctrl-m-filter (process output-string
)
306 "Remove trailing control Ms."
307 (let ((old-buffer (current-buffer))
308 (filtered-string output-string
))
311 (set-buffer (process-buffer process
))
312 (setq moving
(= (point) (process-mark process
)))
314 (while (string-match "\r" filtered-string
)
315 (setq filtered-string
316 (replace-match "" nil nil filtered-string
)))
319 ;; Insert the text, moving the process-marker.
320 (goto-char (process-mark process
))
321 (insert filtered-string
)
322 (set-marker (process-mark process
) (point)))
323 (if moving
(goto-char (process-mark process
))))
324 (set-buffer old-buffer
))))
326 (defmacro net-utils-run-program
(name header program
&rest args
)
327 "Run a network information program."
328 ` (let ((buf (get-buffer-create (concat "*" ,name
"*"))))
331 (insert ,header
"\n")
333 (apply 'start-process
,name buf
,program
,@args
)
334 'net-utils-remove-ctrl-m-filter
)
335 (display-buffer buf
)))
337 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
338 ;; Wrappers for external network programs
339 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
342 (defun traceroute (target)
343 "Run traceroute program for TARGET."
344 (interactive "sTarget: ")
346 (if traceroute-program-options
347 (append traceroute-program-options
(list target
))
349 (net-utils-run-program
350 (concat "Traceroute" " " target
)
351 (concat "** Traceroute ** " traceroute-program
" ** " target
)
359 If your system's ping continues until interrupted, you can try setting
360 `ping-program-options'."
362 (list (read-from-minibuffer "Ping host: " (net-utils-machine-at-point))))
364 (if ping-program-options
365 (append ping-program-options
(list host
))
367 (net-utils-run-program
368 (concat "Ping" " " host
)
369 (concat "** Ping ** " ping-program
" ** " host
)
376 "Run ipconfig program."
378 (net-utils-run-program
380 (concat "** Ipconfig ** " ipconfig-program
" ** ")
382 ipconfig-program-options
385 ;; This is the normal name on most Unixes.
387 (defalias 'ifconfig
'ipconfig
)
391 "Run netstat program."
393 (net-utils-run-program
395 (concat "** Netstat ** " netstat-program
" ** ")
397 netstat-program-options
402 "Run the arp program."
404 (net-utils-run-program
406 (concat "** Arp ** " arp-program
" ** ")
413 "Run the route program."
415 (net-utils-run-program
417 (concat "** Route ** " route-program
" ** ")
419 route-program-options
422 ;; FIXME -- Needs to be a process filter
423 ;; (defun netstat-with-filter (filter)
424 ;; "Run netstat program."
425 ;; (interactive "sFilter: ")
427 ;; (set-buffer (get-buffer "*Netstat*"))
428 ;; (goto-char (point-min))
429 ;; (delete-matching-lines filter)
433 (defun nslookup-host (host)
434 "Lookup the DNS information for HOST."
436 (list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
438 (if nslookup-program-options
439 (append nslookup-program-options
(list host
))
441 (net-utils-run-program
445 (list "Nslookup" host nslookup-program
)
454 "Run nslookup program."
457 (comint-run nslookup-program
)
458 (set-process-filter (get-buffer-process "*nslookup*")
459 'net-utils-remove-ctrl-m-filter
)
463 ;; Using a derived mode gives us keymaps, hooks, etc.
465 nslookup-mode comint-mode
"Nslookup"
466 "Major mode for interacting with the nslookup program."
468 (make-local-variable 'font-lock-defaults
)
469 '((nslookup-font-lock-keywords)))
470 (setq local-abbrev-table nslookup-mode-abbrev-table
)
472 (make-local-variable 'comint-prompt-regexp
)
473 (setq comint-prompt-regexp nslookup-prompt-regexp
)
474 (make-local-variable 'comint-input-autoexpand
)
475 (setq comint-input-autoexpand t
)
478 (define-key nslookup-mode-map
"\t" 'comint-dynamic-complete
)
480 (define-abbrev nslookup-mode-abbrev-table
"e" "exit")
481 (define-abbrev nslookup-mode-abbrev-table
"f" "finger")
482 (define-abbrev nslookup-mode-abbrev-table
"h" "help")
483 (define-abbrev nslookup-mode-abbrev-table
"lse" "lserver")
484 (define-abbrev nslookup-mode-abbrev-table
"q" "exit")
485 (define-abbrev nslookup-mode-abbrev-table
"r" "root")
486 (define-abbrev nslookup-mode-abbrev-table
"s" "set")
487 (define-abbrev nslookup-mode-abbrev-table
"se" "server")
488 (define-abbrev nslookup-mode-abbrev-table
"v" "viewer")
497 (read-from-minibuffer
499 (or (ffap-string-at-point 'machine
) "")))))
500 (net-utils-run-program
504 (list "Dig" host dig-program
)
510 ;; This is a lot less than ange-ftp, but much simpler.
516 (read-from-minibuffer
517 "Ftp to Host: " (net-utils-machine-at-point))))
519 (let ((buf (get-buffer-create (concat "*ftp [" host
"]*"))))
522 (comint-exec buf
(concat "ftp-" host
) ftp-program nil
523 (if ftp-program-options
524 (append (list host
) ftp-program-options
)
527 (switch-to-buffer-other-window buf
)
531 ftp-mode comint-mode
"FTP"
532 "Major mode for interacting with the ftp program."
535 (make-local-variable 'font-lock-defaults
)
536 '((ftp-font-lock-keywords)))
538 (make-local-variable 'comint-prompt-regexp
)
539 (setq comint-prompt-regexp ftp-prompt-regexp
)
541 (make-local-variable 'comint-input-autoexpand
)
542 (setq comint-input-autoexpand t
)
544 ;; Already buffer local!
545 (setq comint-output-filter-functions
546 (list 'comint-watch-for-password-prompt
))
548 (setq local-abbrev-table ftp-mode-abbrev-table
)
552 (define-abbrev ftp-mode-abbrev-table
"q" "quit")
553 (define-abbrev ftp-mode-abbrev-table
"g" "get")
554 (define-abbrev ftp-mode-abbrev-table
"p" "prompt")
555 (define-abbrev ftp-mode-abbrev-table
"anon" "anonymous")
557 ;; Occasionally useful
558 (define-key ftp-mode-map
"\t" 'comint-dynamic-complete
)
560 (defun smbclient (host service
)
561 "Connect to SERVICE on HOST via SMB."
564 (read-from-minibuffer
565 "Connect to Host: " (net-utils-machine-at-point))
566 (read-from-minibuffer "SMB Service: ")))
568 (let* ((name (format "smbclient [%s\\%s]" host service
))
569 (buf (get-buffer-create (concat "*" name
"*")))
570 (service-name (concat "\\\\" host
"\\" service
)))
573 (comint-exec buf name smbclient-program nil
574 (if smbclient-program-options
575 (append (list service-name
) smbclient-program-options
)
576 (list service-name
)))
578 (switch-to-buffer-other-window buf
)
581 (defun smbclient-list-shares (host)
582 "List services on HOST."
585 (read-from-minibuffer
586 "Connect to Host: " (net-utils-machine-at-point))
588 (let ((buf (get-buffer-create (format "*SMB Shares on %s*" host
))))
593 "smbclient-list-shares"
599 (switch-to-buffer-other-window buf
)))
602 smbclient-mode comint-mode
"smbclient"
603 "Major mode for interacting with the smbclient program."
606 (make-local-variable 'font-lock-defaults
)
607 '((smbclient-font-lock-keywords)))
609 (make-local-variable 'comint-prompt-regexp
)
610 (setq comint-prompt-regexp smbclient-prompt-regexp
)
612 (make-local-variable 'comint-input-autoexpand
)
613 (setq comint-input-autoexpand t
)
615 ;; Already buffer local!
616 (setq comint-output-filter-functions
617 (list 'comint-watch-for-password-prompt
))
619 (setq local-abbrev-table smbclient-mode-abbrev-table
)
623 (define-abbrev smbclient-mode-abbrev-table
"q" "quit")
626 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
627 ;; Network Connections
628 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
630 ;; Full list is available at:
631 ;; ftp://ftp.isi.edu/in-notes/iana/assignments/port-numbers
632 (defvar network-connection-service-alist
635 (cons 'active-users
11)
651 (cons 'netbios-name
137)
652 (cons 'netbios-data
139)
657 "Alist of services and associated TCP port numbers.
658 This list in not complete.")
661 (defmacro run-network-program
(process-name host port
662 &optional initial-string
)
664 (let ((tcp-connection)
667 (setq buf
(get-buffer-create (concat "*" ,process-name
"*")))
677 (error "Could not open connection to %s" ,host
))
679 (set-marker (process-mark tcp-connection
) (point-min))
680 (set-process-filter tcp-connection
'net-utils-remove-ctrl-m-filter
)
682 (process-send-string tcp-connection
683 (concat ,initial-string
"\r\n")))
684 (display-buffer buf
)))
686 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
688 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
692 (defun finger (user host
)
693 "Finger USER on HOST."
694 ;; One of those great interactive statements that's actually
695 ;; longer than the function call! The idea is that if the user
696 ;; uses a string like "pbreton@cs.umb.edu", we won't ask for the
697 ;; host name. If we don't see an "@", we'll prompt for the host.
699 (let* ((answer (read-from-minibuffer "Finger User: "
700 (net-utils-url-at-point)))
701 (index (string-match (regexp-quote "@") answer
)))
704 (substring answer
0 index
)
705 (substring answer
(1+ index
)))
708 (read-from-minibuffer "At Host: " (net-utils-machine-at-point))))))
710 (user-and-host (concat user
"@" host
))
712 (concat "Finger [" user-and-host
"]"))
717 (cdr (assoc 'finger network-connection-service-alist
))
721 (defcustom whois-server-name
"rs.internic.net"
722 "Default host name for the whois service."
727 (defcustom whois-server-list
728 '(("whois.arin.net") ; Networks, ASN's, and related POC's (numbers)
729 ("rs.internic.net") ; domain related info
736 "A list of whois servers that can be queried."
738 :type
'(repeat (list string
)))
740 (defcustom whois-server-tld
741 '(("rs.internic.net" .
"com")
742 ("rs.internic.net" .
"org")
743 ("whois.ripe.net" .
"be")
744 ("whois.ripe.net" .
"de")
745 ("whois.ripe.net" .
"dk")
746 ("whois.ripe.net" .
"it")
747 ("whois.ripe.net" .
"fi")
748 ("whois.ripe.net" .
"fr")
749 ("whois.ripe.net" .
"uk")
750 ("whois.apnic.net" .
"au")
751 ("whois.apnic.net" .
"ch")
752 ("whois.apnic.net" .
"hk")
753 ("whois.apnic.net" .
"jp")
754 ("whois.nic.gov" .
"gov")
755 ("whois.nic.mil" .
"mil"))
756 "Alist to map top level domains to whois servers."
758 :type
'(repeat (cons string string
)))
760 (defcustom whois-guess-server t
761 "If non-nil then whois will try to deduce the appropriate whois
762 server from the query. If the query doesn't look like a domain or hostname
763 then the server named by whois-server-name is used."
767 (defun whois-get-tld (host)
768 "Return the top level domain of `host', or nil if it isn't a domain name."
769 (let ((i (1- (length host
)))
770 (max-len (- (length host
) 5)))
771 (while (not (or (= i max-len
) (char-equal (aref host i
) ?.
)))
775 (substring host
(1+ i
)))))
779 (defun whois (arg search-string
)
780 "Send SEARCH-STRING to server defined by the `whois-server-name' variable.
781 If `whois-guess-server' is non-nil, then try to deduce the correct server
782 from SEARCH-STRING. With argument, prompt for whois server."
783 (interactive "P\nsWhois: ")
784 (let* ((whois-apropos-host (if whois-guess-server
785 (rassoc (whois-get-tld search-string
)
788 (server-name (if whois-apropos-host
789 (car whois-apropos-host
)
793 (completing-read "Whois server name: "
794 whois-server-list nil nil
"whois.")
799 (cdr (assoc 'whois network-connection-service-alist
))
803 (defcustom whois-reverse-lookup-server
"whois.arin.net"
804 "Server which provides inverse DNS mapping."
810 (defun whois-reverse-lookup ()
812 (let ((whois-server-name whois-reverse-lookup-server
))
813 (call-interactively 'whois
)))
815 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
816 ;;; General Network connection
817 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
820 (defun network-connection-to-service (host service
)
821 "Open a network connection to SERVICE on HOST."
824 (read-from-minibuffer "Host: " (net-utils-machine-at-point))
825 (completing-read "Service: "
829 (list (symbol-name (car elt
)))))
830 network-connection-service-alist
))))
833 (cdr (assoc (intern service
) network-connection-service-alist
)))
837 (defun network-connection (host port
)
838 "Open a network connection to HOST on PORT."
839 (interactive "sHost: \nnPort: ")
840 (network-service-connection host
(number-to-string port
)))
842 (defun network-service-connection (host service
)
843 "Open a network connection to SERVICE on HOST."
846 (process-name (concat "Network Connection [" host
" " service
"]"))
847 (portnum (string-to-number service
))
849 (or (zerop portnum
) (setq service portnum
))
853 (pop-to-buffer (get-buffer (concat "*" process-name
"*")))
858 ;;; net-utils.el ends here