hoder.org

October 21, 2008

vi usage

Filed under: freebsd — Tags: — admin @ 6:01 pm

Modes of Operation

vi, the Unix visual editor, has two modes of operation:

  1. Command mode–This is vi’s initial and normal state. In this mode, most commands you type are not displayed. Only commands preceded by :, /, or ! are displayed on the status line, which also gives feedback about input, output, and large changes. Execute these commands with <Return> or <Esc>. Use <Esc> to change from text input mode to command mode (when in doubt, press <Esc>).
  2. Text input (Insert) mode–This mode is entered by pressing a, A, i, I, o, or O. Press <Esc> to end text input mode and return to command mode.

For more information about using vi, see the online help note (on Uniform Access Unix computers, enter help vi).

The following notations are used in this document (variables appear in italics):

 
  x represents a single character
  <Control> x control character: while holding down the <Control> key, press x
  text one or more characters
  n represents a number
  pat text and pattern matching characters
  <Return> Return key on your terminal
  <Esc> Escape key on your terminal
 
Entering and Leaving vi Files
  vi filename(s) edit a file or files
  vi -r filename retrieve saved version of file after system or editor crash
  vi -x filename edit encrypted file
  vi -wn   filename set default window size to n (useful for dial-ups)
  :wq save (write) file and exit to system prompt
  ZZ save file and exit to system prompt
  :q! discard all changes and exit to system prompt
 
Basic Cursor Movement
Use h, j, k, and l to move the cursor–using arrow keys may result in undesirable consequences
  h move cursor left one character
  j move cursor down one line
  k move cursor up one line
  l move cursor right one character
(Any of the above commands preceded by n will move the cursor n spaces or lines in the indicated direction.)
 
Inserting Text
(If n precedes an insert character, n copies of inserted text are added upon escape from insertion mode.)
  a begin insert at right of cursor
  A begin insert at end of line
  i begin insert at left of cursor
  I insert at beginning of line
  o open line below, ready for insertion
  O open line above, ready for insertion
  S replace text with blank line; begin insertion at beginning of that line
  <Control>i insert tab
  <Control>v insert non-printing character
  <Backspace> erase character (invisible until over-typed or insert mode escaped)
  <Esc> terminate insert mode; also terminates unwanted commands
 
Deleting, Retrieving, and Undoing
  dw delete word
  dd delete line
  yw yank word into buffer
  yy yank line into buffer
  x delete character
  D delete characters from cursor to end of line
  ndw delete n words into buffer
  ndd delete n lines into buffer
  nyw yank n words into buffer
  nyy yank n lines into buffer
  nx delete n characters into buffer
  p put buffer contents after cursor
  P put buffer contents before cursor
  u undo last single change
  U restore current line
 
Changing, Replacing, and Copying Text
  . repeat last change
  n. repeat last change n times
  cwtext mark end of a word with $ and change to text (press <Esc> to end)
  rx replace character under cursor with character x
  nrx replace n characters with character x
  Rtext write over existing text, (<Esc> to end)
  J join succeeding line to current cursor line
  :s/pat1/pat2 on the current line, substitute the first occurence of pattern 1 with pattern 2
  :s/pat1/pat2/ g on the current line, substitute all occurences of pattern 1 with pattern 2
  :& ;repeat the last :s request
  :%s/pat1/pat2/ g substitute all occurences of pattern 1 with pattern 2 throughout the file
  :.,$s/pat1/pat2/ g substitute all occurences of pattern 1 with pattern 2 from cursor to end of file
 
Moving Around in a File
  <Control>g ascertain line number of current line
  G go to end of file
  nG go to line n
  <Return> or + move cursor to beginning of next line
  - move to beginning of previous line
  w or nw move one word or n words to the right
  b or nb move one word or n words to the left
  ) move to next sentence
  ( move to previous sentence
  } move to next paragraph
  { move to previous paragraph
  <Control>d scroll down one-half screen
  <Control>u scroll up one-half screen
  <Control>l clear and redraw the screen
  mx mark cursor position with character x
  `x move to position marked with x
  d`x delete text from marked x to cursor
  y`x yank text from marked x to cursor
Note: If you precede the mark letter with (apostrophe) instead of ` (grave accent), the action will apply to the entire line the mark is in, not the exact marked location.
 
File Manipulation
  :rfile read in a file beginning on the line below the current line
  :w save and remain in current file
  :wq save file and quit
  :q quit (leave unedited file)
  :q! quit and do not save changes
  :!command run single Unix command while editing (press <Return> to return to file)
  :sh obtain temporary shell (<Control>D to return to file being edited)
  :n,mm l move lines numbered n through m after line l
  :n,mt l make a copy of lines numbered n through m and put after line l
  :n,mw file write lines numbered n through m to file
  :n,mw>> file append lines numbered n through m to end of file
  :’a,’ bwfile write block, marked with a and b, to file
 
Searching For Text
  /pat go to pattern pat (forward in file from current cursor position)
  ?pat go to pattern pat (backward in file from current cursor position)
  n repeat last search, looking in direction of initial search
  N repeat last search, looking in reverse direction of initial search
  % find matching ( ) or { } or [ ] (can be used in combination with /,?,n, or N to search for matching brackets throughout file)
 
Setting Options
Options are either toggled on and off, or given values. When editing, set options for a file with the set command. If you want options to be permanent in a particular directory, create a .exrc file in that directory and set options in that file: set option option option=n. (For example, set ai sm sw=4.) If you want certain option settings to apply throughout your Unix environment, edit your .login file by entering setenv EXINIT’set option option option=n’ (for example, setenv EXINIT’set ai sw wm=10′).
The values in a .exrc file for a directory will override the values of EXINIT in the .login file. If no .exrc file exists, any option values set in the .login are used. If some option values are set in the .exrc file and others are set in the .login file, values from both files are used.
  :set all displays all option settings on your terminal
  :set displays settings set by EXINIT, the .exrc file, and any current changes
  :set option sets option
  :set option=n sets option and assigns it the value of n
  :set nooption unsets option
  :set option? displays setting of option on screen status line
 
Common Option Selections
(To see a complete list of options, enter :set all.)

  Option Name Default What Option Does
  autoindent (ai) noai provides automatic indentation during text entry
  autowrite (aw) noaw automatically saves file (write) before searches, control codes, escapes to shell
  ignorecase (ic) noic ignore case during searchs
  lisp nolisp modify )( }{ ][ to be compatible with lisp
  list nolist show tabs (^I) and ends of lines ($)
  magic magic allows metacharacters; with nomagic, these only include <Control> and $
  number (nu) nonu show line numbers
  readonly (ro) noro make file status read only
  redraw (re) nore simulate smart terminal on dumb
  shell sh=/bin/sh   pathname of new shell for ! and :sh (default from $SHELL if present)
  showmatch (sm) nosm show matching ( or { when ) or } is entered
  term $TERM name of terminal being used; set by Unix $TERM
  terse noterse provide shorter error diagnostics
  wrapmargin (wm) wm=0 cause lines to be broken at least n spaces from right edge of screen

September 11, 2008

Stop PHP nobody Spammers

Filed under: PHP, Uncategorized, email, freebsd — admin @ 5:14 pm

Stop PHP nobody Spammers
http://www.webhostgear.com/232.html
Update: May 25, 2005:
- Added Logrotation details
- Added Sample Log Output

PHP and Apache has a history of not being able to track which users are sending out mail through the PHP mail function from the nobody user causing leaks in formmail scripts and malicious users to spam from your server without you knowing who or where.

Watching your exim_mainlog doesn’t exactly help, you see th email going out but you can’t track from which user or script is sending it. This is a quick and dirty way to get around the nobody spam problem on your Linux server.

If you check out your PHP.ini file you’ll notice that your mail program is set to: /usr/sbin/sendmail and 99.99% of PHP scripts will just use the built in mail(); function for PHP - so everything will go through /usr/sbin/sendmail =)

Requirements:
We assume you’re using Apache 1.3x, PHP 4.3x and Exim. This may work on other systems but we’re only tested it on a Cpanel/WHM Red Hat Enterprise system.

Time:
10 Minutes, Root access required.

Step 1)
Login to your server and su - to root.

Step 2)
Turn off exim while we do this so it doesn’t freak out.
/etc/init.d/exim stop

Article provided by WebHostGear.com

Step 3)
Backup your original /usr/sbin/sendmail file. On systems using Exim MTA, the sendmail file is just basically a pointer to Exim itself.
mv /usr/sbin/sendmail /usr/sbin/sendmail.hidden 

Step 4)
Create the spam monitoring script for the new sendmail.
pico /usr/sbin/sendmail

Paste in the following:

#!/usr/local/bin/perl

 

# use strict;
 use Env;
 my $date = `date`;
 chomp $date;
 open (INFO, “>>/var/log/spam_log”) || die “Failed to open file ::$!”;
 my $uid = $>;
 my @info = getpwuid($uid);
 if($REMOTE_ADDR) {
         print INFO “$date - $REMOTE_ADDR ran $SCRIPT_NAME at $SERVER_NAME n”;
 }
 else {

        print INFO “$date - $PWD -  @infon”;

 }
 my $mailprog = ‘/usr/sbin/sendmail.hidden’;
 foreach  (@ARGV) {
         $arg=”$arg” . ” $_”;
 }

 open (MAIL,”|$mailprog $arg”) || die “cannot open $mailprog: $!n”;
 while (<STDIN> ) {
         print MAIL;
 }
 close (INFO);
 close (MAIL); 

Step 5)
Change the new sendmail permissions
chmod +x /usr/sbin/sendmail

Step 6)
Create a new log file to keep a history of all mail going out of the server using web scripts
touch /var/log/spam_logchmod 0777 /var/log/spam_log

 

 

Step 7)
Start Exim up again.
/etc/init.d/exim start 

Step 8)
Monitor your spam_log file for spam, try using any formmail or script that uses a mail function - a message board, a contact script.
tail - f /var/log/spam_log 

Sample Log Output

Mon Apr 11 07:12:21 EDT 2005 - /home/username/public_html/directory/subdirectory -  nobody x 99 99   Nobody / /sbin/nologin

Log Rotation Details
Your spam_log file isn’t set to be rotated so it might get to be very large quickly. Keep an eye on it and consider adding it to your logrotation.

pico /etc/logrotate.conf

FIND:
# no packages own wtmp — we’ll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
    rotate 1
}

ADD BELOW:

# SPAM LOG rotation
/var/log/spam_log {
    monthly
    create 0777 root root
    rotate 1
}

Notes:
You may also want to chattr + i /usr/sbin/sendmail so it doesn’t get overwritten.

service restart

Filed under: freebsd — admin @ 4:58 pm

location:

/etc/init.d/mysql start|stop

exim

Turn off exim while we do this so it doesn’t freak out.
/etc/init.d/exim stop

?

How about httpd?

July 15, 2008

定制Apache服务器(HTTPD)的日志

Filed under: freebsd — admin @ 4:46 pm

我们在很多情况下需要定制Apache服务器(HTTPD)的日志(LOG)。比如为了防止Apache LOG文件过大需要定制apache服务器的LOG文件名,
对于某些特定的URL比如图片等的访问不记录LOG等。

本文就此2例介绍apache的定制方法。

1,apache服务器LOG文件名的定制(按时间自动命名)

apache自带了名为一个rotatelogs的组件,可以通过配置达到LOG文件定制的目的。
打开httpd.conf文件:
例:

TransferLog “|/usr/local/apache2/bin/rotatelogs /usr/local/apache2/logs/access_log 86400
TransferLog “|/usr/local/apache2/bin/rotatelogs /usr/local/apache2/logs/error_log 86400

参数1:rotatelogs的路径/文件名。以 | 接头。
参数2:LOG文件的路径/文件名
参数3:LOG文件创建的滚动时间(単位:秒)。上面86400为每24小时重新写入新的LOG文件。

2,apache服务器不记录图片文件的访问LOG

很多情况,我们没必要让apache服务器记录图片/Javascript/CSS等文件的访问LOG,同样可以通过设置httpd.conf来达到目的。
打开httpd.conf文件:
例(Addmodule的以下任意位置):

LoadModule setenvif_module modules/mod_setenvif.so
AddModule mod_setenvif.c

SetEnvIf Request_URI “\.(gif)|(jpg)|(jpeg)|(js)|(css)|(png)$” no_access_log

将默认设置

CustomLog /var/log/httpd/access_log combined

修改为:

CustomLog /usr/local/apache2/logs/access_log combined env=!no_access_log

重新启动apache

#httpd restart

如此,Apache便会按时自动在新的LOG文件记录日志,也不会记录对图片/Javascript/css文件的访问日志。

apache2 设置

apache2的情况下,如果只有rotatelogs2,可以做类似如下设置:

LoadModule setenvif_module modules/mod_setenvif.so

SetEnvIf Request_Method “(GET)|(POST)|(PUT)|(DELETE)|(HEAD)” log
SetEnvIf Request_URI “(\.gif|\.jpe?g|\.png|\.css|\.js|\.ico|/image_thumb)$” !log

NameVirtualHost *

<VirtualHost *>
ServerName www.your-domain.com
ServerAlias your-domain.com *.your-domain.com
CustomLog “|/usr/sbin/rotatelogs2 /var/log/apache2/your-domain-access_log.%Y%m%d 86400 +540” combined env=log

</VirtualHost>

July 7, 2008

增加内存表的使用空间

Filed under: freebsd, mysql — admin @ 12:05 pm

如何增加内存表的使用空间

this is how to change it with my.cnf
/usr/local/mysql/bin/mysqld_safe –user=mysql    -O max_heap_table_size=320M  &

默认的是16M,可以根据自己的需要,增加到需要的大小

July 2, 2008

mstring installed as a port under FreeBSD

Filed under: PHP, freebsd — admin @ 11:25 pm

{\rtf1\ansi\ansicpg1252\deff0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}
{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\nowidctlpar\fi-4800\li4800\b\f0\fs20  Main >> Installing port in /usr/ports/converters/php5-mbstring\par
\par
Installing port in /usr/ports/converters/php5-mbstring\par
===>  Vulnerability check disabled, database not found\par
=> php-5.2.6.tar.bz2 doesn’t seem to exist in /usr/ports/distfiles/.\par
=> Attempting to fetch from http://br.php.net/distributions/.\par
php-5.2.6.tar.bz2                                     9346 kB  317 kBps\par
===>  Extracting for php5-mbstring-5.2.6\par
=> MD5 Checksum OK for php-5.2.6.tar.bz2.\par
=> SHA256 Checksum OK for php-5.2.6.tar.bz2.\par
===>  Patching for php5-mbstring-5.2.6\par
===>  Applying FreeBSD patches for php5-mbstring-5.2.6\par
===>   php5-mbstring-5.2.6 depends on file: /usr/local/bin/phpize - found\par
===>   php5-mbstring-5.2.6 depends on file: /usr/local/bin/autoconf-2.61 - found\par
===>  PHPizing for php5-mbstring-5.2.6\par
Configuring for:\par
PHP Api Version:         20041225\par
Zend Module Api No:      20060613\par
Zend Extension Api No:   220060519\par
===>  Configuring for php5-mbstring-5.2.6\par
configure: WARNING: you should use –build, –host, –target\par
checking for grep that handles long lines and -e… /usr/bin/grep\par
checking for egrep… /usr/bin/grep -E\par
checking for a sed that does not truncate output… /bin/sed\par
checking for amd64-portbld-freebsd6.2-gcc… cc\par
checking for C compiler default output file name… a.out\par
checking whether the C compiler works… yes\par
checking whether we are cross compiling… no\par
checking for suffix of executables… \par
checking for suffix of object files… o\par
checking whether we are using the GNU C compiler… yes\par
checking whether cc accepts -g… yes\par
checking for cc option to accept ISO C89… none needed\par
checking whether cc understands -c and -o together… yes\par
checking for system library directory… lib\par
checking if compiler supports -R… yes\par
checking build system type… amd64-portbld-freebsd6.2\par
checking host system type… amd64-portbld-freebsd6.2\par
checking target system type… amd64-portbld-freebsd6.2\par
checking for PHP prefix… /usr/local\par
checking for PHP includes… -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib\par
checking for PHP extension directory… /usr/local/lib/php/extensions/no-debug-non-zts-20060613\par
checking for PHP installed headers prefix… /usr/local/include/php\par
checking for re2c… re2c\par
checking for re2c version… 0.13.3 (ok)\par
checking for gawk… no\par
checking for nawk… nawk\par
checking if nawk is broken… no\par
checking whether to enable multibyte string support… yes, shared\par
checking whether to enable multibyte regex support… yes\par
checking whether to check multibyte regex backtrack… yes\par
checking for external libmbfl… no\par
checking how to run the C preprocessor… cc -E\par
checking for ANSI C header files… yes\par
checking for sys/types.h… yes\par
checking for sys/stat.h… yes\par
checking for stdlib.h… yes\par
checking for string.h… yes\par
checking for memory.h… yes\par
checking for strings.h… yes\par
checking for inttypes.h… yes\par
checking for stdint.h… yes\par
checking for unistd.h… yes\par
checking for variable length prototypes and stdarg.h… yes\par
checking for stdlib.h… (cached) yes\par
checking for string.h… (cached) yes\par
checking for strings.h… (cached) yes\par
checking for unistd.h… (cached) yes\par
checking sys/time.h usability… yes\par
checking sys/time.h presence… yes\par
checking for sys/time.h… yes\par
checking sys/times.h usability… yes\par
checking sys/times.h presence… yes\par
checking for sys/times.h… yes\par
checking stdarg.h usability… yes\par
checking stdarg.h presence… yes\par
checking for stdarg.h… yes\par
checking for int… yes\par
checking size of int… 4\par
checking for short… yes\par
checking size of short… 2\par

exim

Filed under: email, freebsd — admin @ 12:12 am

Main >> DNS Functions >> Edit MX Entry
/var/spool/exim/msglog/6

Current MX Entries
Domain   MX Entry Always Accept
hobid.com  0 hobid.com Delete
20 alt1.aspmx.l.google.com Delete
No Set To Yes
Main >> Service Configuration >> Exim Configuration Editor

Exim Configuration Editor
Configuration file passes test!  New configuration file was installed.
Enabled system filter options: attachments|fail_spam_score_over_200|spam_rewrite
Enabled ACL options in block ACL_RATELIMIT_BLOCK: 0tracksenders
Enabled ACL options in block ACL_RATELIMIT_SPAM_BLOCK: ratelimit_spam_score_over_200
Enabled ACL options in block ACL_RBL_BLOCK:
Enabled ACL options in block ACL_PRE_RECP_VERIFY_BLOCK: dictionary_attack
Enabled ACL options in block ACL_NOTQUIT_BLOCK: ratelimit
Enabled ACL options in block ACL_TRUSTEDLIST_BLOCK:
Enabled ACL options in block ACL_CONNECT_BLOCK: ratelimit|spammerlist
Enabled ACL options in block ACL_SPAM_BLOCK: deny_spam_score_over_200
Detected spam handling in acls, disabling spamassassin in routers & transports!.
SpamAssassin method remains unchanged
Configured options list is:
Provided options list is: hostlist senderverifybypass_hosts|hostlist skipsmtpcheck_hosts|hostlist spammeripblocks|hostlist backupmx_hosts|hostlist trustedmailhosts|domainlist user_domains|smtp_receive_timeout|ignore_bounce_errors_after|timeout_frozen_after|auto_thaw|callout_domain_negative_expire|callout_negative_expire|acl_smtp_connect|acl_smtp_notquit|spamd_address
Exim Insert Regex is: virtual_userdelivery|virtual_aliases|lookuphost|virtual_user|address_pipe|localuser
Exim Replace Regex is: virtual_sa_user|sa_localuser|virtual_sa_userdelivery|local_sa_delivery|central_filter|central_user_filter|democheck|fail_remote_domains|has_alias_but_no_mailbox_discarded_to_prevent_loop|literal|local_delivery|local_delivery_spam|localuser|localuser_spam|lookuphost|remote_smtp|userforward|virtual_aliases|virtual_aliases_nostar|virtual_user|virtual_user_spam|virtual_userdelivery|virtual_userdelivery_spam
Exim Match Insert Regex is: quota_directory|maildir_format
Exim version 4.69 #0 (FreeBSD 6.2) built 04-Jun-2008 21:38:13
Copyright (c) University of Cambridge 2006
Probably Berkeley DB version 1.8x (native mode)
Support for: crypteq iconv() IPv6 use_setclassresources PAM Perl Expand_dlfunc OpenSSL Content_Scanning Old_Demime
Lookups: lsearch wildlsearch nwildlsearch iplsearch cdb dbm dbmnz dnsdb dsearch nis nis0 passwd
Authenticators: cram_md5 dovecot plaintext spa
Routers: accept dnslookup ipliteral manualroute queryprogram redirect
Transports: appendfile/maildir/mailstore/mbx autoreply lmtp pipe smtp
Fixed never_users: 0
Size of off_t: 8
Exim Perl Load List is: spam_acl_support|checkuserquota|boxtrapper|safefile|fast_checkvalias|checkspam|checkspam2|fast_isdemo|fast_accountfunc|checkpass_cphulkd
/etc/exim.pl.local installed!
razor2 is not installed, disabling it in SpamAssassin to save memory
pyzor is not installed, disabling it in SpamAssassin to save memory
SPF is disabled in exim or unavailable, enabling SPF for SpamAssassin

Attempting to restart exim
Waiting for exim to restart…. . . . . . . . . . . finished.

exim statusmailnull 26474  0.0  0.1 15896  3484  ??  Ss    3:15PM   0:00.00 /usr/local/sbin/exim -bd -q30m (exim-4.69-0)
mailnull 26477  0.0  0.1 15896  3448  ??  Ss    3:15PM   0:00.00 /usr/local/sbin/exim -tls-on-connect -bd -oX 465 (exim-4.69-0)

exim started ok
Your configuration changes have been saved!

Main >> Hostname A Entry Missing!

Hostname A Entry Missing!
The server was unable to lookup an an A entry for its hostname (newinst.layeredtech.com). This is generally because the entry was never added. However this could also be the result of your nameserver(s) being down. If you would like to attempt to automatically add the entry, .

Main >> Software >> Update Server Software

Update Server Software
cPanel Package Upgrades in Progress…
Ftp Setup Script Version 6.1
This is the pure-ftpd installer
Searching ports for pure-ftpd ……………………………….found pure-ftpd in /usr/ports/ftp/pure-ftpd….Done
pure-ftpd (1.0.21-2) is already installed.
MySQL Setup Script Version 7.0
This is the MySQL installer for OS FreeBSD
Searching ports for mysql50-client ………………………..found mysql50-client in /usr/ports/databases/mysql50-client….Done
mysql50-client (5.0.51a) is already installed.
Searching ports for mysql50-server ………………………..found mysql50-server in /usr/ports/databases/mysql50-server….Done
mysql50-server (5.0.51a) is already installed.
Install Complete
bandmin Setup Script Version 1.0
courier-imap Setup Script Version 1.0
This is the courier-imap installer for OS FreeBSD
Source: packages-6.2-release
looking up ftp5.de.freebsd.org
connecting to ftp5.de.freebsd.org:21
fetch: ftp://ftp5.de.freebsd.org/pub/FreeBSD/ports/amd64/packages-6.2-release/INDEX: Operation timed out
Source: packages-6-stable
looking up ftp5.de.freebsd.org
connecting to ftp5.de.freebsd.org:21
fetch: ftp://ftp5.de.freebsd.org/pub/FreeBSD/ports/amd64/packages-6-stable/INDEX: Operation timed out
Source: packages-6.2-release
looking up ftp.ua.freebsd.org
connecting to ftp.ua.freebsd.org:21
binding data socket
initiating transfer
remote size / mtime: 8252233 / 1164048316
/root/.cpbsdpkgs/6-8-2008.INDEX                       8058 kB  380 kBps
gdbm (1.8.3_2) is already installed.
Searching ports for courier-authlib ……………………………………………………….found courier-authlib in /usr/ports/security/courier-authlib….Done
courier-authlib (0.60.2) is already installed.
Searching ports for courier-imap ………………………………………..found courier-imap in /usr/ports/mail/courier-imap….Done
courier-imap (4.3.1,2) is already installed.
No restart required
Install Complete
Exim (maildir) Setup Script Version 20.0
exim (4.69) is already installed.
exim (4.69) is already installed.
Searching ports for portupgrade …………………………………………………..found portupgrade in /usr/ports/ports-mgmt/portupgrade….Done
portupgrade (2.4.3-2,2) is already installed.
openssh is installed
exim is installed
rdate is installed
bash is installed
ncftp is installed
wget is installed
jpeg is installed
python is installed
imap-uw is installed
png is installed

June 24, 2008

Shell中的grep、awk和sed的常用命令和语法

Filed under: freebsd — Tags: , , , — admin @ 11:49 pm

Grep的常用命令语法1. 双引号引用和单引号引用
在g r e p命令中输入字符串参数时,最好将其用双引号括起来。例如:”m y s t r i n g”。这样做有两个原因,一是以防被误解为 s h e l l命令,二是可以用来查找多个单词组成的字符串,例如:”jet plane”,如果不用双引号将其括起来,那么单词 p l a n e将被误认为是一个文件,查询结果将返回”文件不存在”的错误信息。
在调用变量时,也应该使用双引号,诸如: g r e p”$ M Y VA R”文件名,如果不这样,将
没有返回结果。
在调用模式匹配时,应使用单引号.[root@mypc ]# echo `grep 123 111.txt`  (#注意是反单引号)

2. 常用的g r e p选项有:
-c   只输出匹配行的计数。
-i   不区分大小写(只适用于单字符)。
-h   查询多文件时不显示文件名。
-l   查询多文件时只输出包含匹配字符的文件名。
-n   显示匹配行及行号。
-s   不显示不存在或无匹配文本的错误信息。
-v   显示不包含匹配文本的所有行。

3. 特殊的–在多个文件中进行查询
$ grep “sort”*.doc     ( #在当前目录下所有. d o c文件中查找字符串”s o r t”)
 
$ grep “sort it” *      (#或在所有文件中查询单词”sort it”)
接下来的所有示例是指在单个文件中进行查询
4. 行匹配
$ grep -c “48″ data.f
$ 4                      (#g r e p返回数字4,意义是有4行包含字符串”4 8″。)
$ grep “48″ data.f          (#显示包含”4 8″字符串的4行文本)

5. 显示满足匹配模式的所有行行数:
[root@mypc oid2000]# grep -n 1234 111.txt
1:1234
3:1234ab

6. 精确匹配
[root@mypc oid2000]# grep “1234\>” 111.txt
1234

7. 查询空行,查询以某个条件开头或者结尾的行。
结合使用^和$可查询空行。使用- n参数显示实际行数
[root@mypc oid2000]# grep -n “^$” 111.txt    (返回结果 2:   #说明第二行是空行)
[root@mypc oid2000]# grep -n “^abc” 111.txt (#查询以abc开头的行)
[root@mypc oid2000]# grep -n “abc$” 111.txt  (#查询以abc结尾的行)

8. 匹配特殊字符,查询有特殊含义的字符,诸如$ . ‘ ” * [] ^ | \ + ? ,必须在特定字符前加\。
[root@mypc oid2000]# grep  “\.” 111.txt  (#在111.txt中查询包含”.”的所有行)
[root@mypc oid2000]# grep  “my\.conf”  111.txt (#查询有文件名my. c o n f的行)

9. 目录的查询
[root@mypc oid2000]# ls -l |grep “^d”      (#如果要查询目录列表中的目录)
[root@mypc oid2000]# ls -l |grep “^d[d]”    (#在一个目录中查询不包含目录的所有文件)
[root@mypc]# ls -l |grpe “^d…..x..x” (#查询其他用户和用户组成员有可执行权限的目录集合)

Awk的常用命令语法

awk命令擅长格式化报文或从一个大的文本文件中抽取数据包,下面是该命令的基本语法
awk [-F filed-separator] “commands” input-file(s)
[ - F域分隔符]是可选的,a w k使用空格作为缺省的域分隔符,如果在要处理的文件中是以冒号作为分割域的(如passwd文件),则在处理的时候要这样指明 awk -F: command input-file(s)

1.1域和记录
a w k执行时,其浏览域标记为$ 1,$ 2 . . . $ n。这种方法称为域标识。使用$ 1 , $ 3表示参照第1和第3域,注意这里用逗号做域分隔。如果希望打印一个有 5个域的记录的所有域,不必指明 $ 1 , $ 2 , $ 3 , $ 4 , $ 5,可使用$ 0,意即所有域。

1.2保存a w k输出
$ awk ‘{print $0}’ input-files > out-files    (#重定向保存输出)
$ awk ‘{print $0}’ input-files | tee out-files  (#使用t e e命令,输出到文件的同时输出到屏幕)

1.3 常用的awk命令举例
[root@mypc /]# awk ‘$0 ~ /user/’ /etc/passwd  (#如果某域含有user就将该行打印出来)
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
[root@mypc /]# awk ‘/user/’ /etc/passwd      (#同上)
[root@mypc /]# awk -F: ‘{if ($5 ~ /user/) print $0}’ /etc/passwd (#如第五域有user则输出该行)
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
[root@mypc /]# ifconfig | awk ‘/inet/{print $2}’  (#从ifconfig的输出中抽取含inet的行并打印第二域)
[root@mypc /]# ifconfig | awk ‘/inet/{print $2}’ | awk -F: ‘{print $2}’ (#在上面的基础上再抽取,这个命令可以让你直接得到本机的ip地址)

Sed的常用命令语法
Sed是一个非交互性文本流编辑器。它编辑文件或标准输入导出的文本拷贝。

1.行的匹配
[root@mypc /]# sed -n ‘2p’ /etc/passwd  打印出第2行
[root@mypc /]# sed -n ‘1,3p’ /etc/passwd 打印出第1到第3行
[root@mypc /]# sed -n ‘$p’ /etc/passwd   打印出最后一行
[root@mypc /]# sed -n ‘/user/’p /etc/passwd 打印出含有user的行
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
[root@mypc /]# sed -n ‘/\$/’p /etc/passwd  打印出含有$元字符的行,$意为最后一行

2.插入文本和附加文本(插入新行)
[root@mypc /]# sed -n ‘/FTP/p’ /etc/passwd  打印出有FTP的行
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@mypc /]# sed ‘/FTP/ a\ 456′ /etc/passwd 在含有FTP的行后面新插入一行,内容为456
[root@mypc /]# sed ‘/FTP/ i\ 123′ /etc/passwd在含有FTP的行前面新插入一行,内容为123
[root@mypc /]# sed ‘/FTP/ i\ “123″‘ /etc/passwd在含有FTP的行前面新插入一行,内容为”123″
[root@mypc /]# sed ‘5 a\ 123′ /etc/passwd         在第5行后插入一新行,内容为123
[root@mypc /]# sed ‘5 i\ “12345″‘ /etc/passwd   在第5行前插入一新行,内容为”12345″

3.删除文本
[root@mypc /]# sed ‘1d’ /etc/passwd  删除第1行
[root@mypc /]# sed ‘1,3d’ /etc/passwd  删除第1至3行
[root@mypc /]# sed ‘/user/d’ /etc/passwd  删除带有user的行

4. 替换文本,替换命令用替换模式替换指定模式,格式为:
[ a d d r e s s [,address]] s/ pattern-to-find /replacement-pattern/[g p w n]
[root@mypc /]# sed ’s/user/USER/’ /etc/passwd     将第1个user替换成USER,g表明全局替换
[root@mypc /]# sed ’s/user/USER/g’ /etc/passwd    将所有user替换成USER
[root@mypc /]# sed ’s/user/#user/’ /etc/passwd    将第1个user替换成#user,如用于屏蔽作用
[root@mypc /]# sed ’s/user//’ /etc/passwd         将第1个user替换成空
[root@mypc /]# sed ’s/user/&11111111111111/’ /etc/passwd  如果要附加或修改一个很长的字符串,可以使用( &)命令,&命令保存发现模式以便重新调用它,然后把它放在替换字符串里面,这里是把&放前面
[root@mypc /]# sed ’s/user/11111111111111&/’ /etc/passwd  这里是将&放后面

5. 快速一行命令
下面是一些一行命令集。([ ]表示空格,[ ]表示t a b键)
‘s / \ . $ / / g’ 删除以句点结尾行
‘-e /abcd/d’ 删除包含a b c d的行
‘s / [ ] [ ] [ ] * / [ ] / g’ 删除一个以上空格,用一个空格代替
‘s / ^ [ ] [ ] * / / g’ 删除行首空格
‘s / \ . [ ] [ ] * / [ ] / g’ 删除句点后跟两个或更多空格,代之以一个空格
‘/ ^ $ / d’ 删除空行
‘s / ^ . / / g’ 删除第一个字符
‘s /COL \ ( . . . \ ) / / g’ 删除紧跟C O L的后三个字母
‘s / ^ \ / / / g’ 从路径中删除第一个\
‘s / [ ] / [ ] / / g’ 删除所有空格并用t a b键替代
‘S / ^ [ ] / / g’ 删除行首所有t a b键
‘s / [ ] * / / g’ 删除所有t a b键
如果使用s e d对文件进行过滤,最好将问题分成几步,分步执行,且边执行边测试结果。
经验告诉我们,这是执行一个复杂任务的最有效方式。

List All Unix Users

Filed under: freebsd — admin @ 12:03 pm

List All Unix Users

To list all users on a Unix system, even the ones who are not logged in, look at the /etc/password file.


$ cat /etc/passwd
...
george:*:1009:1009:George Washington:/home/george:/usr/bin/bash
tom:*:1016:1016:Thomas Jefferson:/home/tom:/usr/bin/bash
al:*:1017:1017:Alexander Hamilton:/home/alex:/usr/bin/bash
...

You can use the `cut` command to only see one field from the password file.

For example, to just see the Unix user names, use the command “$ cat /etc/passwd | cut -d: -f1″.


$ cat /etc/passwd | cut -d: -f1
...
george
tom
al
...

Or, to only see the GECOS field (i.e. the account holders real name), try this:


$ cat /etc/passwd | cut -d: -f5
...
George Washinton
Thomas Jefferson
Alexander Hamilton
...

Note that you will also see Unix system accounts, such as “root

unix command

Filed under: freebsd — admin @ 11:53 am

LIST OF COMMON COMMANDS

Name

Manual Page

Description

alias csh(1) Create an alias for a command sequence, see csh(1)
apropos apropos(1) locate commands by keyword lookup
banner banner(1V) display a string in large letters
bc bc(1) arbitrary-precision arithmetic language
bg csh(1) Run a suspended job in the background, see csh(1)
cal cal(1) display a calendar
cancel lp(1) send/cancel requests to a printer
cat cat(1V) concatenate and display
cc cc(1V) C compiler
cd cd(1) change working directory
chgrp chgrp(1) change the group ownership of a file
chmod chmod(1V) change the permissions mode of a file
chsh passwd(1) change local or NIS password information
clear clear(1) clear the terminal screen
compress compress(1) compress or expand files
cp cp(1) copy files
crontab crontab(1) install, edit, remove or list a user’s crontab file
csh csh(1) shell with a C-like syntax and advanced interactive features
date date(1V) display or set the date
dbx dbx(1) source-level debugger
df df(1V) report free disk space on file systems
diff diff(1) display line-by-line differences between pairs of text files
dircmp dircmp(1V) compare directories
dirs csh(1) Display the directory stack, see csh(1)
dos2unix dos2unix(1) convert text file from DOS format to ISO format
du du(1V) display the number of disk blocks used per directory or file
echo echo(1V) echo arguments to the standard output
egrep grep(1V) search a file for a string or regular expression
eject eject(1) eject media device from drive
exec csh(1) Execute a command in place of the current shell, see csh(1)
exit csh(1) Exit the current shell, see csh(1)
fdformat fdformat(1) format diskettes
fg csh(1) Resume executing a suspended job in the foreground, see csh(1)
fgrep grep(1V) search a file for a string or regular expression
file file(1) determine the type of a file by examining its contents
find find(1) find files by name, or by other characteristics
finger finger(1) display information about users
from from(1) display the sender and date of newly-arrived mail messages
ftp ftp(1C) file transfer program
grep grep(1V) search a file for a string or regular expression
groups groups(1) display a user’s group memberships
head head(1) display first few lines of specified files
history csh(1) Display recently typed commands, see csh(1)
hostid hostid(1) print the numeric identifier of the current host
hostname hostname(1) set or print name of current host system
id id(1V) print the user name and ID, and group name and ID
jobs csh(1) List jobs suspended or backgrounded from this shell, see csh(1)
kill kill(1) send a signal to a process, or terminate a process
last last(1) indicate last logins by user or terminal
limit csh(1) Set or display resource limits, see csh(1)
line line(1) read one line
lint lint(1V) a C program verifier
ln ln(1V) make hard or symbolic links to files
login login(1) log in to the system
logname logname(1) get the name by which you logged in
logout csh(1) Terminate a login shell, see csh(1)
lp lp(1) send/cancel requests to a printer
lpq lpq(1) display the queue of printer jobs
lpr lpr(1) send a job to the printer
lprm lprm(1) remove jobs from the printer queue
lpstat lpstat(1) display the printer status information
ls ls(1V) list the contents of a directory
mach mach(1) display the processor type of the current host
man man(1) display reference manual pages
mesg mesg(1) permit or deny messages on the terminal
mkdir mkdir(1) make a directory
more more(1) browse or page through a text file
mv mv(1) move or rename files
newgrp newgrp(1) log in to a new group
nice nice(1) run a command at low priority
passwd passwd(1) change local or password information
popd csh(1) Pop the directory stack, see csh(1)
printenv printenv(1) display environment variables currently set
ps ps(1) display the status of current processes
pushd csh(1) Push a directory onto the directory stack, see csh(1)
pwd pwd(1) display the pathname of the current working directory
quota quota(1) display a user’s disk quota and usage
rehash csh(1) Rescan the execution path, see csh(1)
reset tset(1) establish or restore terminal characteristics
rlogin rlogin(1C) remote login
rm rm(1) remove (unlink) files or directories
rmdir rm(1) remove (unlink) files or directories
rsh rsh(1C) remote shell
rup rup(1C) show host status of local machines (RPC version)
ruptime ruptime(1C) show host status of local machines
rusers rusers(1C) who’s logged in on local machines (RPC version)
rwall rwall(1C) write to all users over a network
rwho rwho(1C) who’s logged in on local machines
script script(1) make typescript of a terminal session
setenv csh(1) Set or display environment variables, see csh(1)
set csh(1) Set or display shell variables, see csh(1)
sh sh(1) standard UNIX system shell and command-level language
sleep sleep(1) suspend execution for a specified interval
sort sort(1V) sort and collate lines
source csh(1) Read commands from a file, see csh(1)
spell spell(1) report spelling errors
spellin spell(1) report spelling errors
strings strings(1) find printable strings in an object file or binary
strip strip(1) remove symbols and relocation bits from an object file
stty stty(1V) set or alter the options for a terminal
su su(1V) super-user, temporarily switch to a new user ID
suspend csh(1) Suspend the shell, see csh(1)
sync sync(1) update the super block; force changed blocks to the disk
tail tail(1) display the last part of a file
talk talk(1) talk to another user
tar tar(1) create tape archives, and add or extract files
telnet telnet(1C) user interface to a remote system using the TELNET protocol
time time(1V) time a command
touch touch(1V) update the access and modification times of a file
trace trace(1) trace system calls and signals
tset tset(1) establish or restore terminal characteristics
tty tty(1) display the name of the terminal
umask csh(1) Set or display the file permissions mask, see csh(1)
unalias csh(1) Discard an alias, see csh(1)
uncompress compress(1) compress or expand files, display expanded contents
unix2dos unix2dos(1) convert text file from ISO format to DOS format
unlimit csh(1) Remove a limit on a resource, see csh(1)
unset csh(1) Discard a shell variable, see csh(1)
unsetenv csh(1) Discard an envrionment variable, see csh(1)
uptime uptime(1) show how long the system has been up
users users(1) display a compact list of users logged in
uudecode uuencode(1C) encode a binary file, or decode its ASCII representation
uuencode uuencode(1C) encode a binary file, or decode its ASCII representation
vacation vacation(1) reply to mail automatically
w w(1) who is logged in, and what are they doing
wall wall(1) write to all users logged in
wc wc(1) display a count of lines, words and characters
whatis whatis(1) display a one-line summary about a keyword
whereis whereis(1) locate the binary, source, and manual page for a command
which which(1) locate a command; display its pathname or alias
who who(1) who is logged in on the system
whoami whoami(1) display the effective current username
whois whois(1) Internet user name directory service
write write(1) write a message to another user
Newer Posts »

Powered by hoder.org