(eval-when-compile
(require 'comint))
(defgroup net-utils nil
"Network utility functions."
:prefix "net-utils-"
:group 'comm
:version "20.3")
(defcustom net-utils-remove-ctl-m
(member system-type (list 'windows-nt 'msdos))
"If non-nil, remove control-Ms from output."
:group 'net-utils
:type 'boolean)
(defcustom traceroute-program
(if (eq system-type 'windows-nt)
"tracert"
"traceroute")
"Program to trace network hops to a destination."
:group 'net-utils
:type 'string)
(defcustom traceroute-program-options nil
"Options for the traceroute program."
:group 'net-utils
:type '(repeat string))
(defcustom ping-program "ping"
"Program to send network test packets to a host."
:group 'net-utils
:type 'string)
(defcustom ping-program-options
(and (memq system-type (list 'linux 'gnu/linux 'irix))
(list "-c" "4"))
"Options for the ping program.
These options can be used to limit how many ICMP packets are emitted."
:group 'net-utils
:type '(repeat string))
(defcustom ipconfig-program
(if (eq system-type 'windows-nt)
"ipconfig"
"ifconfig")
"Program to print network configuration information."
:group 'net-utils
:type 'string)
(defcustom ipconfig-program-options
(list
(if (eq system-type 'windows-nt)
"/all" "-a"))
"Options for ipconfig-program."
:group 'net-utils
:type '(repeat string))
(defcustom netstat-program "netstat"
"Program to print network statistics."
:group 'net-utils
:type 'string)
(defcustom netstat-program-options
(list "-a")
"Options for netstat-program."
:group 'net-utils
:type '(repeat string))
(defcustom arp-program "arp"
"Program to print IP to address translation tables."
:group 'net-utils
:type 'string)
(defcustom arp-program-options
(list "-a")
"Options for arp-program."
:group 'net-utils
:type '(repeat string))
(defcustom route-program
(if (eq system-type 'windows-nt)
"route"
"netstat")
"Program to print routing tables."
:group 'net-utils
:type 'string)
(defcustom route-program-options
(if (eq system-type 'windows-nt)
(list "print")
(list "-r"))
"Options for route-program."
:group 'net-utils
:type '(repeat string))
(defcustom nslookup-program "nslookup"
"Program to interactively query DNS information."
:group 'net-utils
:type 'string)
(defcustom nslookup-program-options nil
"List of options to pass to the nslookup program."
:group 'net-utils
:type '(repeat string))
(defcustom nslookup-prompt-regexp "^> "
"Regexp to match the nslookup prompt.
This variable is only used if the variable
`comint-use-prompt-regexp' is non-nil."
:group 'net-utils
:type 'regexp)
(defcustom dig-program "dig"
"Program to query DNS information."
:group 'net-utils
:type 'string)
(defcustom ftp-program "ftp"
"Progam to run to do FTP transfers."
:group 'net-utils
:type 'string)
(defcustom ftp-program-options nil
"List of options to pass to the FTP program."
:group 'net-utils
:type '(repeat string))
(defcustom ftp-prompt-regexp "^ftp>"
"Regexp which matches the FTP program's prompt.
This variable is only used if the variable
`comint-use-prompt-regexp' is non-nil."
:group 'net-utils
:type 'regexp)
(defcustom smbclient-program "smbclient"
"Smbclient program."
:group 'net-utils
:type 'string)
(defcustom smbclient-program-options nil
"List of options to pass to the smbclient program."
:group 'net-utils
:type '(repeat string))
(defcustom smbclient-prompt-regexp "^smb: \>"
"Regexp which matches the smbclient program's prompt.
This variable is only used if the variable
`comint-use-prompt-regexp' is non-nil."
:group 'net-utils
:type 'regexp)
(defcustom dns-lookup-program "host"
"Program to interactively query DNS information."
:group 'net-utils
:type 'string
)
(defcustom dns-lookup-program-options nil
"List of options to pass to the dns-lookup program."
:group 'net-utils
:type '(repeat string)
)
(defvar network-connection-service nil)
(defvar network-connection-host nil)
(defconst nslookup-font-lock-keywords
(progn
(defvar font-lock-type-face)
(defvar font-lock-keyword-face)
(defvar font-lock-variable-name-face)
(require 'font-lock)
(list
(list "^[A-Za-z0-9 _]+:" 0 font-lock-type-face)
(list "\\<\\(SOA\\|NS\\|MX\\|A\\|CNAME\\)\\>"
1 font-lock-keyword-face)
(list
(mapconcat 'identity
(make-list 4 "[0-9]+")
"\\.")
0 font-lock-variable-name-face)
(list
(let ((host-expression "[-A-Za-z0-9]+"))
(concat
(mapconcat 'identity
(make-list 2 host-expression)
"\\.")
"\\(\\." host-expression "\\)*"))
0 font-lock-variable-name-face)))
"Expressions to font-lock for nslookup.")
(defun net-utils-machine-at-point ()
(let ((pt (point)))
(buffer-substring-no-properties
(save-excursion
(skip-chars-backward "-a-zA-Z0-9.")
(point))
(save-excursion
(skip-chars-forward "-a-zA-Z0-9.")
(skip-chars-backward "." pt)
(point)))))
(defun net-utils-url-at-point ()
(let ((pt (point)))
(buffer-substring-no-properties
(save-excursion
(skip-chars-backward "--:=&?$+@-Z_a-z~#,%")
(skip-chars-forward "^A-Za-z0-9" pt)
(point))
(save-excursion
(skip-chars-forward "--:=&?$+@-Z_a-z~#,%")
(skip-chars-backward ":;.,!?" pt)
(point)))))
(defun net-utils-remove-ctrl-m-filter (process output-string)
"Remove trailing control Ms."
(let ((old-buffer (current-buffer))
(filtered-string output-string))
(unwind-protect
(let ((moving))
(set-buffer (process-buffer process))
(setq moving (= (point) (process-mark process)))
(while (string-match "\r" filtered-string)
(setq filtered-string
(replace-match "" nil nil filtered-string)))
(save-excursion
(goto-char (process-mark process))
(insert filtered-string)
(set-marker (process-mark process) (point)))
(if moving (goto-char (process-mark process))))
(set-buffer old-buffer))))
(defmacro net-utils-run-program (name header program &rest args)
"Run a network information program."
` (let ((buf (get-buffer-create (concat "*" ,name "*"))))
(set-buffer buf)
(erase-buffer)
(insert ,header "\n")
(set-process-filter
(apply 'start-process ,name buf ,program ,@args)
'net-utils-remove-ctrl-m-filter)
(display-buffer buf)
buf))
(defun traceroute (target)
"Run traceroute program for TARGET."
(interactive "sTarget: ")
(let ((options
(if traceroute-program-options
(append traceroute-program-options (list target))
(list target))))
(net-utils-run-program
(concat "Traceroute" " " target)
(concat "** Traceroute ** " traceroute-program " ** " target)
traceroute-program
options)))
(defun ping (host)
"Ping HOST.
If your system's ping continues until interrupted, you can try setting
`ping-program-options'."
(interactive
(list (read-from-minibuffer "Ping host: " (net-utils-machine-at-point))))
(let ((options
(if ping-program-options
(append ping-program-options (list host))
(list host))))
(net-utils-run-program
(concat "Ping" " " host)
(concat "** Ping ** " ping-program " ** " host)
ping-program
options)))
(defun ipconfig ()
"Run ipconfig program."
(interactive)
(net-utils-run-program
"Ipconfig"
(concat "** Ipconfig ** " ipconfig-program " ** ")
ipconfig-program
ipconfig-program-options))
(defalias 'ifconfig 'ipconfig)
(defun netstat ()
"Run netstat program."
(interactive)
(net-utils-run-program
"Netstat"
(concat "** Netstat ** " netstat-program " ** ")
netstat-program
netstat-program-options))
(defun arp ()
"Run the arp program."
(interactive)
(net-utils-run-program
"Arp"
(concat "** Arp ** " arp-program " ** ")
arp-program
arp-program-options))
(defun route ()
"Run the route program."
(interactive)
(net-utils-run-program
"Route"
(concat "** Route ** " route-program " ** ")
route-program
route-program-options))
(defun nslookup-host (host)
"Lookup the DNS information for HOST."
(interactive
(list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
(let ((options
(if nslookup-program-options
(append nslookup-program-options (list host))
(list host))))
(net-utils-run-program
"Nslookup"
(concat "** "
(mapconcat 'identity
(list "Nslookup" host nslookup-program)
" ** "))
nslookup-program
options)))
(defun nslookup ()
"Run nslookup program."
(interactive)
(require 'comint)
(comint-run nslookup-program)
(nslookup-mode))
(define-derived-mode nslookup-mode comint-mode "Nslookup"
"Major mode for interacting with the nslookup program."
(set
(make-local-variable 'font-lock-defaults)
'((nslookup-font-lock-keywords)))
(setq comint-prompt-regexp nslookup-prompt-regexp)
(setq comint-input-autoexpand t))
(define-key nslookup-mode-map "\t" 'comint-dynamic-complete)
(defun dns-lookup-host (host)
"Lookup the DNS information for HOST (name or IP address)."
(interactive
(list (read-from-minibuffer "Lookup host: " (net-utils-machine-at-point))))
(let ((options
(if dns-lookup-program-options
(append dns-lookup-program-options (list host))
(list host))))
(net-utils-run-program
(concat "DNS Lookup [" host "]")
(concat "** "
(mapconcat 'identity
(list "DNS Lookup" host dns-lookup-program)
" ** "))
dns-lookup-program
options
)))
(defun run-dig (host)
"Run dig program."
(interactive
(list
(progn
(require 'ffap)
(read-from-minibuffer
"Lookup host: "
(with-no-warnings
(or (ffap-string-at-point 'machine) ""))))))
(net-utils-run-program
"Dig"
(concat "** "
(mapconcat 'identity
(list "Dig" host dig-program)
" ** "))
dig-program
(list host)))
(defun ftp (host)
"Run ftp program."
(interactive
(list
(read-from-minibuffer
"Ftp to Host: " (net-utils-machine-at-point))))
(require 'comint)
(let ((buf (get-buffer-create (concat "*ftp [" host "]*"))))
(set-buffer buf)
(ftp-mode)
(comint-exec buf (concat "ftp-" host) ftp-program nil
(if ftp-program-options
(append (list host) ftp-program-options)
(list host)))
(pop-to-buffer buf)))
(define-derived-mode ftp-mode comint-mode "FTP"
"Major mode for interacting with the ftp program."
(setq comint-prompt-regexp ftp-prompt-regexp)
(setq comint-input-autoexpand t)
(unless (memq 'comint-watch-for-password-prompt
(default-value 'comint-output-filter-functions))
(add-hook 'comint-output-filter-functions 'comint-watch-for-password-prompt
nil t)))
(define-key ftp-mode-map "\t" 'comint-dynamic-complete)
(defun smbclient (host service)
"Connect to SERVICE on HOST via SMB."
(interactive
(list
(read-from-minibuffer
"Connect to Host: " (net-utils-machine-at-point))
(read-from-minibuffer "SMB Service: ")))
(require 'comint)
(let* ((name (format "smbclient [%s\\%s]" host service))
(buf (get-buffer-create (concat "*" name "*")))
(service-name (concat "\\\\" host "\\" service)))
(set-buffer buf)
(smbclient-mode)
(comint-exec buf name smbclient-program nil
(if smbclient-program-options
(append (list service-name) smbclient-program-options)
(list service-name)))
(pop-to-buffer buf)))
(defun smbclient-list-shares (host)
"List services on HOST."
(interactive
(list
(read-from-minibuffer
"Connect to Host: " (net-utils-machine-at-point))))
(let ((buf (get-buffer-create (format "*SMB Shares on %s*" host))))
(set-buffer buf)
(smbclient-mode)
(comint-exec buf "smbclient-list-shares"
smbclient-program nil (list "-L" host))
(pop-to-buffer buf)))
(define-derived-mode smbclient-mode comint-mode "smbclient"
"Major mode for interacting with the smbclient program."
(setq comint-prompt-regexp smbclient-prompt-regexp)
(setq comint-input-autoexpand t)
(unless (memq 'comint-watch-for-password-prompt
(default-value 'comint-output-filter-functions))
(add-hook 'comint-output-filter-functions 'comint-watch-for-password-prompt
nil t)))
(defvar network-connection-service-alist
(list
(cons 'echo 7)
(cons 'active-users 11)
(cons 'daytime 13)
(cons 'chargen 19)
(cons 'ftp 21)
(cons 'telnet 23)
(cons 'smtp 25)
(cons 'time 37)
(cons 'whois 43)
(cons 'gopher 70)
(cons 'finger 79)
(cons 'www 80)
(cons 'pop2 109)
(cons 'pop3 110)
(cons 'sun-rpc 111)
(cons 'nntp 119)
(cons 'ntp 123)
(cons 'netbios-name 137)
(cons 'netbios-data 139)
(cons 'irc 194)
(cons 'https 443)
(cons 'rlogin 513))
"Alist of services and associated TCP port numbers.
This list is not complete.")
(defmacro run-network-program (process-name host port
&optional initial-string)
`(let ((tcp-connection)
(buf))
(setq buf (get-buffer-create (concat "*" ,process-name "*")))
(set-buffer buf)
(or
(setq tcp-connection
(open-network-stream
,process-name
buf
,host
,port))
(error "Could not open connection to %s" ,host))
(erase-buffer)
(set-marker (process-mark tcp-connection) (point-min))
(set-process-filter tcp-connection 'net-utils-remove-ctrl-m-filter)
(and ,initial-string
(process-send-string tcp-connection
(concat ,initial-string "\r\n")))
(display-buffer buf)))
(defcustom finger-X.500-host-regexps nil
"A list of regular expressions matching host names.
If a host name passed to `finger' matches one of these regular
expressions, it is assumed to be a host that doesn't accept
queries of the form USER@HOST, and wants a query containing USER only."
:group 'net-utils
:type '(repeat regexp)
:version "21.1")
(defun finger (user host)
"Finger USER on HOST."
(interactive
(let* ((answer (read-from-minibuffer "Finger User: "
(net-utils-url-at-point)))
(index (string-match (regexp-quote "@") answer)))
(if index
(list (substring answer 0 index)
(substring answer (1+ index)))
(list answer
(read-from-minibuffer "At Host: "
(net-utils-machine-at-point))))))
(let* ((user-and-host (concat user "@" host))
(process-name (concat "Finger [" user-and-host "]"))
(regexps finger-X.500-host-regexps)
found)
(and regexps
(while (not (string-match (car regexps) host))
(setq regexps (cdr regexps)))
(when regexps
(setq user-and-host user)))
(run-network-program
process-name
host
(cdr (assoc 'finger network-connection-service-alist))
user-and-host)))
(defcustom whois-server-name "rs.internic.net"
"Default host name for the whois service."
:group 'net-utils
:type 'string)
(defcustom whois-server-list
'(("whois.arin.net") ("rs.internic.net") ("whois.publicinterestregistry.net")
("whois.abuse.net")
("whois.apnic.net")
("nic.ddn.mil")
("whois.nic.mil")
("whois.nic.gov")
("whois.ripe.net"))
"A list of whois servers that can be queried."
:group 'net-utils
:type '(repeat (list string)))
(defcustom whois-server-tld
'(("rs.internic.net" . "com")
("whois.publicinterestregistry.net" . "org")
("whois.ripe.net" . "be")
("whois.ripe.net" . "de")
("whois.ripe.net" . "dk")
("whois.ripe.net" . "it")
("whois.ripe.net" . "fi")
("whois.ripe.net" . "fr")
("whois.ripe.net" . "uk")
("whois.apnic.net" . "au")
("whois.apnic.net" . "ch")
("whois.apnic.net" . "hk")
("whois.apnic.net" . "jp")
("whois.nic.gov" . "gov")
("whois.nic.mil" . "mil"))
"Alist to map top level domains to whois servers."
:group 'net-utils
:type '(repeat (cons string string)))
(defcustom whois-guess-server t
"If non-nil then whois will try to deduce the appropriate whois
server from the query. If the query doesn't look like a domain or hostname
then the server named by `whois-server-name' is used."
:group 'net-utils
:type 'boolean)
(defun whois-get-tld (host)
"Return the top level domain of `host', or nil if it isn't a domain name."
(let ((i (1- (length host)))
(max-len (- (length host) 5)))
(while (not (or (= i max-len) (char-equal (aref host i) ?.)))
(setq i (1- i)))
(if (= i max-len)
nil
(substring host (1+ i)))))
(defun whois (arg search-string)
"Send SEARCH-STRING to server defined by the `whois-server-name' variable.
If `whois-guess-server' is non-nil, then try to deduce the correct server
from SEARCH-STRING. With argument, prompt for whois server."
(interactive "P\nsWhois: ")
(let* ((whois-apropos-host (if whois-guess-server
(rassoc (whois-get-tld search-string)
whois-server-tld)
nil))
(server-name (if whois-apropos-host
(car whois-apropos-host)
whois-server-name))
(host
(if arg
(completing-read "Whois server name: "
whois-server-list nil nil "whois.")
server-name)))
(run-network-program
"Whois"
host
(cdr (assoc 'whois network-connection-service-alist))
search-string)))
(defcustom whois-reverse-lookup-server "whois.arin.net"
"Server which provides inverse DNS mapping."
:group 'net-utils
:type 'string)
(defun whois-reverse-lookup ()
(interactive)
(let ((whois-server-name whois-reverse-lookup-server))
(call-interactively 'whois)))
(define-derived-mode
network-connection-mode comint-mode "Network-Connection"
"Major mode for interacting with the network-connection program.")
(defun network-connection-mode-setup (host service)
(make-local-variable 'network-connection-host)
(setq network-connection-host host)
(make-local-variable 'network-connection-service)
(setq network-connection-service service))
(defun network-connection-to-service (host service)
"Open a network connection to SERVICE on HOST."
(interactive
(list
(read-from-minibuffer "Host: " (net-utils-machine-at-point))
(completing-read "Service: "
(mapcar
(function
(lambda (elt)
(list (symbol-name (car elt)))))
network-connection-service-alist))))
(network-connection
host
(cdr (assoc (intern service) network-connection-service-alist))))
(defun network-connection (host port)
"Open a network connection to HOST on PORT."
(interactive "sHost: \nnPort: ")
(network-service-connection host (number-to-string port)))
(defun network-service-connection (host service)
"Open a network connection to SERVICE on HOST."
(require 'comint)
(let* ((process-name (concat "Network Connection [" host " " service "]"))
(portnum (string-to-number service))
(buf (get-buffer-create (concat "*" process-name "*"))))
(or (zerop portnum) (setq service portnum))
(make-comint
process-name
(cons host service))
(set-buffer buf)
(network-connection-mode)
(network-connection-mode-setup host service)
(pop-to-buffer buf)))
(defun network-connection-reconnect ()
"Reconnect a network connection, preserving the old input ring."
(interactive)
(let ((proc (get-buffer-process (current-buffer)))
(old-comint-input-ring comint-input-ring)
(host network-connection-host)
(service network-connection-service))
(if (not (or (not proc)
(eq (process-status proc) 'closed)))
(message "Still connected")
(goto-char (point-max))
(insert (format "Reopening connection to %s\n" host))
(network-connection host
(if (numberp service)
service
(cdr (assoc service network-connection-service-alist))))
(and old-comint-input-ring
(setq comint-input-ring old-comint-input-ring)))))
(provide 'net-utils)