"Cloud computing" basic application shell script (5)

4.expect expected interaction
 issue
in this case requires expect to write a script, automate interactive SSH login:
ready ahead of the target host, IP address is 192.168.4.5
user name mike, the password is 1234567
auto-login after executing the script, and Create a test target host file /tmp/mike.txt
 program
expect TCL is based on written automated interactive program that can automatically convey instructions or text prepared in advance for the interactive process (such as FTP, SSH login process, etc.), without the need for manual intervention. The trigger is based on the expected characteristics will prompt text.
Common expect directive:
define the environment variable: set the variable name variable values
to create an interactive process: spawn an interactive command line
triggered the expected interaction: expect "is expected to appear in the text Keywords:" {send "text to be sent \ r"}
in spawn in the process of establishing allow interactive instructions: interAct
 steps
to achieve this case need to follow the steps below.
Step a: Preparation expect test environment and SSH
1) expect mounting tool
[root @ svr5 ~] # yum -y install expect // mounting expect
... ...
the Installed:
expect.x86_64 0: 5.44.1.15-5.el6_4
the Dependency the Installed:
TCL .x86_64 1: 8.5.7-6.el6

[root @ svr5 ~] # which expect // confirmation expect the path
/ usr / bin / expect
2) prepare SSH test environment
to the machine 192.168.4.5, for example, by enabling sshd service, and users can add mike:
[root @ svr5 ~] # service sshd restart
stop sshd: [OK]
starting sshd: [OK]

[root @ SVR5 ~] # useradd mike
[root @ SVR5 ~] # echo 1234567 | --stdin mike passwd
to change the user's password mike.
passwd: all authentication tokens have been successfully updated.
Step Two: Write expect_ssh script, to achieve free interactive logon
1) mission requirements analysis and ideas
in SSH login process, if it is the first time you connect to the target host, you will first be asked to accept the key before prompted for a password:
[root @ SVR5 ~] # SSH [email protected] // connection to the target host
at The Authenticity of host '192.168.4.5 (192.168.4.5)' CAN BE not the ESTABLISHED.
RSA Key Fingerprint IS 58: A0: d6: 00: c7: f1: 34: 5D:. 6C:. 6D: 70: ce: E0: 20: f8: F3.
Are you the Sure you want to Connecting the Continue (yes / NO) // yes to accept the keys?
Warning: Permanently added '192.168 .4.5 '(the RSA) The List of Known to the hosts.
[email protected] ' S password: password authentication //
Last login: Thu May 7 22:05:44 2015 from 192.168.4.5
[mike @ svr5 ~] $ exit // returned to the client
Zimbabwe Logout
Connection to 192.168.4.5 Closed.
In the above process, the first interaction occurs in the event of "... ... (yes / no) ?" prompt, the need to provide " yes "; while the second interaction occurs in the event of" ... ... password: "when prompted, provide a login password for the user; after interactive logon success, you need to" [Mike SVR5 @ ~] Katex the parse error: the Expected 'EOF', GOT '#' position AT 77: ...: [root @ SVR5 ~] # SSH [email protected]... Exit // returned to the client
Zimbabwe Logout
connection to 192.168.4.5 Closed.
in summary, you should know in advance the SSH connection to the target host normal interaction. need a script to achieve these interactive process, you can use expect tools to create ssh login process by spawn command, and set the expect command to identify the prompt text is expected to occur, provided the appropriate text, and even further send the corresponding sub-process interaction commands which the user names, IP addresses can be set in advance by the variables are set, this is more convenient.
2) to write a script file based on the realization of ideas
scripts refer to the following content
[root @ SVR5 ~] # vim expect_ssh.sh
#! / usr / bin / the Expect
the SET Host 192.168.4.5 // # define variables
the SET the User Mike
the SET password "1234567"
spawn ssh in s e r @ user@ Host # // create interactive process
the Expect "password:" {the send "Katex the parse error: function of Can not use '\ r' in the MODE AT position the Math 9: password \ r"} // automatically # sending secret ...User @ "{send" pwd> /tmp/$user.txt; Exit \ R & lt "}
# // send interactive commands
interact # // allows interactive environment

[root @ SVR5 ~] # chmod + the X-expect_ssh.sh
3) verification, test script
before executing the script, and no /tmp/mike.txt files on the target host:
[root @ SVR5 ~] # LS /tmp/mike.txt
ls: can not access /tmp/mike.txt: no such file or directory
to perform expect_ssh.sh automatic login script:
[root @ SVR5 ~] # expect_ssh.sh
spawn SSH [email protected]
[email protected] 'S password:
Login Last: 12:08:47 Mon. 11, 2015 On May from 192.168.4.5
pwd> /tmp/mike.txt; Exit
[Mike SVR5 @ ~] $ pwd> /tmp/mike.txt; Exit
Zimbabwe Logout
Connection to 192.168.4.5 Closed .
check again the target host, you will see the already established /tmp/mike.txt document describing expect automatic login and remote execution of commands successfully:
[root @ SVR5 ~] # LS /tmp/mike.txt the -l
-rw-rw -r-. 1 mike mike 11 12:17 /tmp/mike.txt 5 Yue 11
5. using regular expressions
 problem
This case requires familiarity with regular expressions to write, perform the following tasks:
using the tool egrep regular exercise basic usage of expressions
to extract valid configuration file httpd.conf line of
writing regular expressions that match the MAC address, E-Mail E-mail address IP address, host name
 program
 steps
to achieve this case need to follow the steps below.
Step one: regular expression matching exercises
1) Typical applications: grep, egrep line of text retrieval
when used without -E option of the grep command supports basic regular match mode. Such as "word" keyword search, "^ word" word matches the beginning of the line, "word Katex the parse error: the Expected 'the EOF', GOT '#' position AT 47: ...: [SVR5 the root @ ~] # grep ' R & lt ^ '/ etc ... ' / etc / the hosts
127.0.0.1 localhost.localdomain localhost
If desired grep simultaneously combining a plurality of retrieval condition formula, such as the output of the "root" or row to "daemon" at the beginning, this time to substantially regular less convenient ( "or" must be escaped as "|"):
[root @ SVR5 ~] # grep ' root | daemon' / etc / passwd // no search results
[root @ SVR5 ~] #
[root @ SVR5 ~] # grep ' root | daemon' / etc / passwd // get the correct results
root❌0: 0: the root: / the root: / bin / the bash
daemon❌2: 2: daemon: / sbin: / sbin / nologin
and Ruoruo Use grep -E or egrep command supports extended regular pattern matching, automatically identifying |, regular expressions like {special characters, more convenient to use, such as:
[the root SVR5 @ ~] # grep -E ' the root | daemon' / etc / the passwd
root❌0: 0: the root: / the root: / bin / the bash
daemon❌2: 2: daemon: / sbin: / sbin / nologin
or
[SVR5 the root @ ~] # egrep ' the root | daemon' / etc / the passwd
root❌0: 0: the root: / the root: / bin / the bash
daemon❌2: 2: daemon: / sbin: / sbin / nologin
use grep -E egrep command using the exact equivalent, which is recommended, especially when it comes to a complex regular expressions.
2) -q option grep, egrep command
option -q quiet expressed (silent) means, in conjunction with this option is not only retrieval output, typically used to find the target recognition exists in the script, by returning a status KaTeX parse error: Expected 'EOF', got '#' at position 110: ...: [root @ svr5 ~] # grep '^ 192.168 ...- Matches the starting end of line
output the default run-level configuration records (lines beginning with id):
[SVR5 the root @ ~] # egrep '^ id' / etc / inittab
id:. 3: the initdefault:
Output Host Configuration Record Name (beginning HOSTNAME line):
[SVR5 the root @ ~] # egrep '^ HOSTNAME' / etc / sysconfig / Network
HOSTNAME = svr5.tarena.com
count the number of users in the local user is logged Shell "/ sbin / nologin" of :
[SVR5 the root @ ~] # egrep -M10 '/ sbin / nologin Katex the parse error: the Expected' the EOF ', GOT' # 'position AT 468: n-... [the root SVR5 @ ~] egrep -C #' / SBI ... '/ etc / passwd
rows 32 // -c option output matching combined
-c option to match the number of output lines, which again through a pipe wc -l effect is the same, but easier wording. For example, using the statistical "/ bin / bash" as a normal number of users logged Shell performed:
[SVR5 the root @ ~] egrep -C # '/ bin / the bash Katex the parse error: the Expected' the EOF ', GOT' # ' at position 34: ... by [root @ svr5 ~] # egrep ' / bin / ba ... '/ etc / passwd | wc -l
26 is
4) basic character element - match any single character
to /etc/rc.local file, for example, to confirm text:
[SVR5 the root @ ~] /etc/rc.local CAT #
# / bin / SH!

This script will be executed after all the other init scripts.

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

touch / var / lock / subsys / local
row including at least a character in the output file /etc/rc.local (except \ n newline), i.e. non-blank line:
[SVR5 the root @ ~] # egrep / etc '.' /rc.local
#! / bin / SH

This script will be executed after all the other init scripts.

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

touch / var / lock / subsys / local
output blank line (with the -v option inverted condition) within the /etc/rc.local file:
[SVR5 the root @ ~] # egrep -v / etc / RC '.'. local

[root @ svr5 ~] #
same operation effect as described above takes the following blank line:
[root @ svr5 ~] # egrep '^ $' /etc/rc.local

[root @ SVR5 ~] #
? 5) basic meta-characters +, -, * - the number of goals appears
also to /etc/rc.local file as an example:
[root @ SVR5 ~] # CAT /etc/rc.local
# ! / bin / sh

This script will be executed after all the other init scripts.

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

touch / var / lock / subsys / local
output includes f, ff, ff, ...... line, i.e., "f" at least once:
[SVR5 the root @ ~] # egrep '+ F' /etc/rc.local

This script will be executed after all the other init scripts.

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

The output includes init, initial line, that is the end of "ial" appears at most once (probably not):
[root @ SVR5 ~] # egrep 'the init (IAL)?' /Etc/rc.local

This script will be executed after all the other init scripts.

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

The output includes stu, stuf, stuff, stufff, ...... rows, i.e., the end of the "f" can appear any number of times, or may not. Repeated target only one character, you can not use parentheses:
[root @ SVR5 ~] # egrep 'stuf *' /etc/rc.local

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

. "All the output lines, the individual " can match any row (including blank lines):
[the root SVR5 @ ~] # egrep '.
/Etc/rc.local'
# / bin / SH!

This script will be executed after all the other init scripts.

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

touch / var / lock / subsys / local
output / etc / the passwd file "r" at the beginning and at "nologin" end user record, i.e., the intermediate can be any character:
. [the root @ SVR5 ~] # egrep '^ R & lt * $ nologin '/ etc / the passwd
rpc❌32: 32: the Portmapper the RPC User: /: / sbin / nologin
rpcuser❌29: 29: the RPC the User-Service: / var / lib / NFS: / sbin / nologin
. 6) membered characters {} - limit the scope of the number that appears
to create a practice test file:
[root @ SVR5 ~] # vim brace.txt
ab DEF ghi ABDR
dedef abab ghighi
abcab CD-ROM
TARENA the GROUP IT
CDCD ababab
the Hello World ABABABAB
output line includes the ababab , i.e., "ab" appears three times in a row:
[SVR5 the root @ ~] # egrep '(ab &) {3}' brace.txt
CDCD ABABAB
the Hello World ABABABAB
output comprises abab, ababab, abababab line, i.e., "ab" consecutive 2 to 4 times:
[SVR5 the root @ ~] # egrep '(ab &) {2,4}' brace.txt
ABAB ghighi dedef
CDCD ABABAB
the Hello World ABABABAB
output comprises ababab, abababab, ...... line, i.e., "ab" least consecutive three times:
[SVR5 the root @ ~] # egrep '(ab &) {3,}' brace.txt
CDCD ABABAB
the Hello World ABABABAB
7) metacharacters [] - matching a single character in the range
also with the previous test file bracet.txt for example:
[root @ SVR5 ~] # CAT brace.txt
ab DEF ghi ABDR
dedef abab ghighi
abcab CD -ROMs
TARENA the iT the GROUP
CDCD ABABAB
the Hello World ABABABAB
output comprises abc, abd rows, i.e., the first two characters "ab", as long as a third character c, d in conditions to meet:
[SVR5 the root @ ~] egrep # 'ab & [CD]' brace.txt
ab & DEF GHI ABDR
abcab the CD-a ROM
output lines including uppercase, using [AZ] match contiguous range:
[SVR5 the root @ ~] # egrep '[AZ]' brace.txt
CD-ROM abcab
TARENA the GROUP IT
Hello abababab World
output includes rows "Non-space nor lowercase" of other characters, the present embodiment capital letters and - meets requirements:
[SVR5 the root @ ~] # egrep '[^ A-zA-the Z]' brace.txt
CD-ROM abcab
8) matches a word boundary
to file /etc/rc.local, for example:
[root @ SVR5 ~] # CAT /etc/rc.local
# / bin / SH!

This script will be executed after all the other init scripts.

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

touch / var / lock / subsys / local
output includes the words "init" line, the file "initialization" undesirably:
[SVR5 the root @ ~] # egrep '\ Binit \ B' /etc/rc.local

This script will be executed after all the other init scripts.

want to do the full Sys V style init stuff.

Or:
[SVR5 the root @ ~] # egrep '<the init>' /etc/rc.local

This script will be executed after all the other init scripts.

want to do the full Sys V style init stuff.

Output includes "ll" at the end of a word line, using> right boundary matching words:
[SVR5 the root @ ~] # egrep 'LL>' /etc/rc.local

This script will be executed after all the other init scripts.

want to do the full Sys V style init stuff.

Or:
[SVR5 the root @ ~] # egrep 'LL \ B' /etc/rc.local

This script will be executed after all the other init scripts.

want to do the full Sys V style init stuff.

9) a combination of a plurality of conditions are
viewed through dmesg start log and the IDE interface, device information associated with the CDROM:
[SVR5 the root @ ~] # egrep '<IDE> | <CDROM>' / var / log / dmesg
Uniform the Multi-Platform IDE Driver Revision-E: 7.00alpha2
PIIX4: the Controller IDE PCI slot AT 0000: 00: 07.1
Probing IDE interface ide0 ...
Probing IDE interface ide1 ...
HDC: VMware Virtual IDE CDROM Drive, ATAPI CD / DVD-ROM Drive
Probing IDE interface ide0 ...
View Bluetooth devices by dmesg boot log, network card device-related information:
[root @ SVR5 ~] # egrep -i 'eth | Network | the Bluetooth' / var / log / dmesg
Initalizing Network Monitor Service drop
the Bluetooth: Core ver 2.10
the Bluetooth: the HCI initialized Connection Manager and Device
the Bluetooth: the HCI socket Layer initialized
the Bluetooth: the HCI the USB Driver ver 2.9
PRO the Intel® / 1000 the Network Driver - Version 7.3.21-NAPI-k4-3
the e1000: eth0: e1000_probe: the Intel® PRO / 1000 the Network Connection
Step 2: Using regex retrieval task
1) the extracted file httpd.conf effective configuration line
to RHEL6 comes with httpd package, for example, provides a number of annotations (starting with # or empty a few cells and then #) in the default httpd.conf configuration file, as well as some blank lines separated:
[root @ svr5 ~] # head /etc/httpd/conf/httpd.conf // confirm the contents of the file

# This is the main Apache server configuration file. It contains the
# configuration directives that give the server its instructions.
# See URL:http://httpd.apache.org/docs/2.2/ for detailed information.
# In particular, see
# URL:http://httpd.apache.org/docs/2.2/mod/directives.html
# for a discussion of each configuration directive.

Simply the Read the Do the NOT at The # Instructions in the without here Wallpaper Understanding
[root @ svr5 ~] # egrep -c . "*" /Etc/httpd/conf/httpd.conf

991 // number of rows
[root @ svr5 ~] # egrep -c "#" /etc/httpd/conf/httpd.conf
rows containing comments // 674
[SVR5 the root @ ~] # egrep -C "^ Katex the parse error: the Expected 'the EOF', GOT '#' position AT 134: ...: [SVR5 the root @ ~] -C # egrep -v '# ... ' /etc/httpd/conf/httpd.conf
222
binding> redirection extract effective httpd.conf configuration will saved to a file httpd .conf.min, related as follows:
[the root SVR5 @ ~] # egrep -v '# | $ ^' /etc/httpd/conf/httpd.conf> httpd.conf.min
[SVR5 the root @ ~] # head the httpd .conf.min // validated first 10 rows arranged
ServerTokens the OS
the ServerRoot "/ etc / the httpd"
the PidFile RUN / the httpd.pid
the Timeout 120
the KeepAlive Off
MaxKeepAliveRequests 100
KeepAliveTimeout 15

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:838 errors:0 dropped:0 overruns:0 frame:0
TX packets:838 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:93855 (91.6 KiB) TX bytes:93855 (91.6 KiB)
[root@svr5 ~]# ifconfig | egrep ‘<[0-9]{1,3}(.[0-9]{1,3}){3}>’
inet addr:192.168.4.4 Bcast:192.168.4.255 Mask:255.255.255.0
inet addr:127.0.0.1 Mask:255.0.0.0

Guess you like

Origin blog.csdn.net/xiaozhedeitzhilu/article/details/90962446