- •Advanced Bash-Scripting Guide
- •Dedication
- •Table of Contents
- •Part 1. Introduction
- •Advanced Bash-Scripting Guide
- •Chapter 2. Starting Off With a Sha-Bang
- •2.1. Invoking the script
- •2.2. Preliminary Exercises
- •Part 2. Basics
- •Chapter 3. Exit and Exit Status
- •Chapter 4. Special Characters
- •Chapter 5. Introduction to Variables and Parameters
- •5.1. Variable Substitution
- •5.2. Variable Assignment
- •5.3. Bash Variables Are Untyped
- •5.4. Special Variable Types
- •Chapter 6. Quoting
- •Chapter 7. Tests
- •7.1. Test Constructs
- •7.2. File test operators
- •7.3. Comparison operators (binary)
- •7.4. Nested if/then Condition Tests
- •7.5. Testing Your Knowledge of Tests
- •8.1. Operators
- •8.2. Numerical Constants
- •Part 3. Beyond the Basics
- •Chapter 9. Variables Revisited
- •9.1. Internal Variables
- •9.2. Manipulating Strings
- •9.3. Parameter Substitution
- •9.4. Typing variables: declare or typeset
- •9.5. Indirect References to Variables
- •9.6. $RANDOM: generate random integer
- •9.7. The Double Parentheses Construct
- •Chapter 10. Loops and Branches
- •10.1. Loops
- •10.2. Nested Loops
- •10.3. Loop Control
- •10.4. Testing and Branching
- •Chapter 11. Internal Commands and Builtins
- •12.1. Basic Commands
- •12.2. Complex Commands
- •12.3. Time / Date Commands
- •12.4. Text Processing Commands
- •12.5. File and Archiving Commands
- •12.6. Communications Commands
- •12.7. Terminal Control Commands
- •12.8. Math Commands
- •12.9. Miscellaneous Commands
- •Chapter 13. System and Administrative Commands
- •Chapter 14. Command Substitution
- •Chapter 15. Arithmetic Expansion
- •Chapter 16. I/O Redirection
- •16.1. Using exec
- •16.2. Redirecting Code Blocks
- •16.3. Applications
- •Chapter 17. Here Documents
- •Chapter 18. Recess Time
- •Part 4. Advanced Topics
- •Chapter 19. Regular Expressions
- •19.1. A Brief Introduction to Regular Expressions
- •19.2. Globbing
- •Chapter 20. Subshells
- •Chapter 21. Restricted Shells
- •Chapter 22. Process Substitution
- •Chapter 23. Functions
- •23.1. Complex Functions and Function Complexities
- •23.2. Local Variables
- •Chapter 24. Aliases
- •Chapter 25. List Constructs
- •Chapter 26. Arrays
- •Chapter 27. Files
- •Chapter 28. /dev and /proc
- •28.2. /proc
- •Chapter 29. Of Zeros and Nulls
- •Chapter 30. Debugging
- •Chapter 31. Options
- •Chapter 32. Gotchas
- •Chapter 33. Scripting With Style
- •Chapter 34. Miscellany
- •34.2. Shell Wrappers
- •34.3. Tests and Comparisons: Alternatives
- •34.4. Optimizations
- •34.5. Assorted Tips
- •34.6. Oddities
- •34.7. Security Issues
- •34.8. Portability Issues
- •34.9. Shell Scripting Under Windows
- •Chapter 35. Bash, version 2
- •36. Endnotes
- •36.1. Author's Note
- •36.2. About the Author
- •36.3. Tools Used to Produce This Book
- •36.4. Credits
- •List of Tables
- •List of Examples
- •Bibliography
Communications Commands
|
Advanced Bash-Scripting Guide: |
|
Prev |
Chapter 12. External Filters, Programs and Commands |
Next |
12.6. Communications Commands
Information and Statistics
host
Searches for information about an Internet host by name or IP address, using DNS.
vrfy
Verify an Internet e-mail address. nslookup
Do an Internet "name server lookup" on a host by IP address. This may be run either interactively or noninteractively, i.e., from within a script.
dig
Similar to nslookup, do an Internet "name server lookup" on a host. May be run either interactively or noninteractively, i.e., from within a script.
traceroute
Trace the route taken by packets sent to a remote host. This command works within a LAN, WAN, or over the Internet. The remote host may be specified by an IP address. The output of this command may be filtered by grep or sed in a pipe.
ping
Broadcast an "ICMP ECHO_REQUEST" packet to other machines, either on a local or remote network. This is a diagnostic tool for testing network connections, and it should be used with caution.
A successful ping returns an exit status of 0. This can be tested for in a script.
bash$ ping localhost
PING localhost.localdomain (127.0.0.1) from 127.0.0.1 : 56(84) bytes of data. Warning: time of day goes back, taking countermeasures.
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=0 ttl=255 time=709 usec 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=255 time=286 usec
--- localhost.localdomain ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max/mdev = 0.286/0.497/0.709/0.212 ms
whois
Perform a DNS (Domain Name System) lookup. The -h option permits specifying which whois server to query. See Example 5-6.
finger
Retrieve information about a particular user on a network. Optionally, this command can display the user's ~/.plan,
~/.project, and ~/.forward files, if present.
http://tldp.org/LDP/abs/html/communications.html (1 of 4) [7/15/2002 6:35:03 PM]
Communications Commands
bash$ finger bozo |
|
|
Login: bozo |
Name: Bozo Bozeman |
|
Directory: /home/bozo |
|
Shell: /bin/bash |
On since Fri Aug 31 20:13 (MST) on tty1 |
|
1 hour 38 minutes idle |
On since Fri Aug 31 20:13 (MST) on pts/0 |
12 seconds idle |
|
On since Fri Aug 31 20:13 (MST) on pts/1 |
|
|
On since Fri Aug 31 20:31 (MST) on pts/2 |
1 hour 16 minutes idle |
|
No mail. |
|
|
No Plan. |
|
|
|
|
|
Out of security considerations, many networks disable finger and its associated daemon. [1]
Remote Host Access
sx, rx
The sx and rx command set serves to transfer files to and from a remote host using the xmodem protocol. These are generally part of a communications package, such as minicom.
sz, rz
The sz and rz command set serves to transfer files to and from a remote host using the zmodem protocol. Zmodem has certain advantages over xmodem, such as greater transmission rate and resumption of interrupted file transfers. Like sx and rx, these are generally part of a communications package.
ftp
Utility and protocol for uploading / downloading files to / from a remote host. An ftp session can be automated in a script (see Example 17-7, Example A-5, and Example A-13).
cu
Call Up a remote system and connect as a simple terminal. This is a sort of dumbed-down version of telnet.
uucp
UNIX to UNIX copy. This is a communications package for transferring files between UNIX servers. A shell script is an effective way to handle a uucp command sequence.
Since the advent of the Internet and e-mail, uucp seems to have faded into obscurity, but it still exists and remains perfectly workable in situations where an Internet connection is not available or appropriate.
telnet
Utility and protocol for connecting to a remote host.
The telnet protocol contains security holes and should therefore probably be avoided.
rlogin
Remote login, initates a session on a remote host. This command has security issues, so use ssh instead.
rsh
Remote shell, executes command(s) on a remote host. This has security issues, so use ssh instead.
rcp
http://tldp.org/LDP/abs/html/communications.html (2 of 4) [7/15/2002 6:35:03 PM]
Communications Commands
Remote copy, copies files between two different networked machines. Using rcp and similar utilities with security implications in a shell script may not be advisable. Consider, instead, using ssh or an expect script.
ssh
Secure shell, logs onto a remote host and executes commands there. This secure replacement for telnet, rlogin, rcp, and rsh uses identity authentication and encryption. See its manpage for details.
Local Network
write
This is a utility for terminal-to-terminal communication. It allows sending lines from your terminal (console or xterm) to that of another user. The mesg command may, of course, be used to disable write access to a terminal
Since write is interactive, it would not normally find use in a script.
Send an e-mail message to a user.
This stripped-down command-line mail client works fine as a command embedded in a script.
Example 12-31. A script that mails itself
#!/bin/sh
# self-mailer.sh: Self-mailing script
ARGCOUNT=1 # Need name of addressee. E_WRONGARGS=65
if [ $# -ne "$ARGCOUNT" ] then
echo "Usage: `basename $0` addressee" exit $E_WRONGARGS
fi
#========================================================================
cat $0 | mail -s "Script \"`basename $0`\" has mailed itself to you." "$1"
#========================================================================
#--------------------------------------------
#Greetings from the self-mailing script.
#A mischievous person has run this script,
#+ which has caused it to mail itself to you.
#Apparently, some people have nothing better #+ to do with their time.
#--------------------------------------------
exit 0
vacation
This utility automatically replies to e-mails that the intended recipient is on vacation and temporarily unavailable. This runs on a network, in conjunction with sendmail, and is not applicable to a dial-up POPmail account.
http://tldp.org/LDP/abs/html/communications.html (3 of 4) [7/15/2002 6:35:03 PM]
Communications Commands
Notes
[1]A daemon is a background process not attached to a terminal session. Daemons perform designated services either at specified times or explicitly triggered by certain events.
The word "daemon" means ghost in Greek, and there is certainly something mysterious, almost supernatural, about the way UNIX daemons silently wander about behind the scenes, carrying out their appointed tasks.
Prev |
Home |
Next |
File and Archiving Commands |
Up |
Terminal Control Commands |
http://tldp.org/LDP/abs/html/communications.html (4 of 4) [7/15/2002 6:35:03 PM]