Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the commonly used shell scripts in the production environment

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "what are the commonly used shell scripts in the production environment", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what shell scripts are commonly used in the production environment.

1. Download the file #! / bin/bashif from the FTP server [$#-ne 1] Then echo "Usage: $0 filename" fidir=$ (dirname $1) file=$ (basename $1) ftp-n-v #-n automatically log in to open 192.168.1.10 # ftp server user admin passwordbinary # set ftp transfer mode to binary to avoid different MD5 values or .tar.gz compressed package format error cd $dirget "$file" EOF2, enter 5 digits less than 100 in a row, statistics sum, minimum and maximum #! / bin/bashCOUNT=1SUM=0MIN=0MAX=100while [$COUNT-le 5] Do read-p "Please enter 1-10 integers:" INT if [[! $INT = ~ ^ [0-9] + $]]; then echo "input must be an integer!" Exit 1 elif [[$INT-gt 100]]; then echo "input must be less than 100!" Exit 1 fi SUM=$ (($SUM+$INT)) [$MIN-lt $INT] & & MIN=$INT [$MAX-gt $INT] & & MAX=$INT let COUNT++doneecho "SUM: $SUM" echo "MIN: $MIN" echo "MAX: $MAX" 3. Assign the result to the variable application scenario: you want to assign the execution result or location parameter to the variable for later use. Method 1:for I in $(echo "456"); do eval a$i=$idoneecho $a4 $a5 $a6 method 2: split the position parameter 192.168.1.1 {1jue 2} into each variable num=0for I in $(eval echo $*) Do # eval decomposes {1echo 2} into 12 let num+=1 eval node$ {num} = "$I" doneecho $node1 $node2 $node3# bash a.sh 192.168.1.1 {1min2} 192.168.1.11 192.168.1.12 method 3ROR = (456) INDEX1=$ (echo ${arr [0]}) INDEX2=$ (echo ${arr [1]}) INDEX3=$ (echo ${arr [2]}) 4, Example of batch modification of file name: # touch article_ {1.. 3} .html # lsarticle_1.html article_2.html article_3.html purpose: change article to bbs method 1:for file in $(ls * html) Do mv $file bbs_$ {file#*_} # mv $file $(echo $file | sed-r's file. * (_. *) / bbs\ 1Unip') # mv $file $(echo $file | echo bbs_$ (cut-d _-f2) done method 2:for file in $(find. -maxdepth 1-name "* html"); do mv $file bbs_$ {file#*_} done method 3html # rename article bbs *. Html5, count the total size of files ending in .html in the current directory. -name "* .html"-exec du-k {}\; | awk'{sum+=$1} END {print sum} 'method 2:for size in $(ls-l *. Html | awk' {print $5}'); do sum=$ (($sum+$size)) doneecho $sum6, scanning host port status #! / bin/bashHOST=$1PORT= "22 25 80 8080" for PORT in $PORT; do if echo & > / dev/null > / dev/tcp/$HOST/$PORT Then echo "$PORT open" else echo "$PORT close" fidone7, Expect implementation SSH interactive execution command Expect is an automatic interactive application tool, such as telnet,ftp,passwd and so on. You need to install the expect package first. Method 1:EOF standard output as expect standard input #! / bin/bashUSER=rootPASS=123.comIP=192.168.1.120expect set timeout 30spawn ssh $USER@$IP expect {"(yes/no)" {send "yes\ r" Exp_continue} "password:" {send "$PASS\ r"} expect "$USER@*" {send "$1\ r"} expect "$USER@*" {send "exit\ r"} expect eofEOF method 2USERTROOPASS 123.comIPstocks 192.168.1.120 "spawn ssh $USER@$IP expect {\" (yes/no)\ "{send\" yes\ r\ "; exp_continue}\" password:\ "{send\" $PASS\ r\ " Exp_continue}\ "$USER@*\" {send\ "df-h\ r exit\ r\" Method 3: separate the expect script from the login script: # cat login.expandable expect script: # puts "Usage: expect login.exp ip user passwd" exit 1} set timeout 30spawn ssh $user@$ipexpect {"(yes/no)" {send "yes\ r" Exp_continue} "password:" {send "$passwd\ r"} expect "$user@*" {send "$cmd\ r"} expect "$user@*" {send "exit\ r"} expect eof execute command script: write a loop to operate on multiple servers in batches #! / bin/bashHOST_INFO=user_info.txtfor ip in $(awk'{print $1}'$HOST_INFO) do user=$ (awk-v I = "$ip" 'Illustrated servers 1 {print $2}') $HOST_INFO) pass=$ (awk-v I = "$ip" 'Illustrated hosts 1 {print $3}' $HOST_INFO) expect login.exp $ip $user $pass $1doneLinux host SSH connection information: # cat user_info.txt192.168.1.120 root 1234568, Batch modify server user password Linux host SSH connection information: old password # cat old_pass.txt 192.168.18.217 root 123456 22192.168.18.218 root 123456 22 content format: IP User Password PortSSH remote password modification script: new password randomly generated #! / bin/bashOLD_INFO=old_pass.txtNEW_INFO=new_pass.txtfor IP in $(awk'/ ^ [^ #] / {print $1}'$OLD_INFO) Do USER=$ (awk-v I=$IP 'Illustrated blocks 1 {print $2}' $OLD_INFO) PASS=$ (awk-v I=$IP 'Illustrated blocks 1 {print $3}' $OLD_INFO) PORT=$ (awk-v I=$IP 'Illustrated packages 1 {print $4}' $OLD_INFO) NEW_PASS=$ (mkpasswd-l8) # Random password echo "$IP $USER $NEW_PASS $PORT" > $NEW_INFO expect-c "spawn ssh-p$PORT $USER@$IP set timeout 2 expect {\" (yes/no)\ "{send\" yes\ r\ " Exp_continue}\ "password:\" {send\ "$PASS\ r\"; exp_continue}\ "$USER@*\" {send\ "echo\'$NEW_PASS\'| passwd-- stdin $USER\ r exit\ r\"; exp_continue} "done generates a new password file: # cat new_pass.txt 192.168.217 root n8wX3mU% 22192.168.18.218 root c87 ZnnL 229, print multiplication formula method 1 for # awk 'BEGIN {for (2:for + method 2:for) echo-n "$j*$i=$result" done echodone10, getopts tool perfect script command line parameter getopts is a tool for parsing script option parameters. Command format: when using getopts optstring name [arg] for the first time, you should pay attention to these points: the script position parameter will match the individual letters in optstring one by one, and if it matches, the value will be assigned to name, otherwise the value name will be assigned to the question mark; the single letter in optstring is an option, and if the letter is followed by a colon, it means that the option is followed by a parameter, and the parameter value will be assigned to the OPTARG variable. The first one in optstring is a colon, which indicates a masking system error (test.sh: illegal option-- h) Allow options to be put together, such as-ab below to write a simple example of printing a file with a specified line to guide your thinking: #! / bin/bashwhile getopts: FRV n: option; do case $option in f) FILE=$OPTARG [!-f $FILE] & & echo "$FILE File not exist!" & & exit;; n) sed-n "${OPTARG} p" $FILE ;?) Echo "Usage: $0-f-n" echo "- f,-- file specified file" echo "- n,-- line-number print specified line" exit 1;; esacdone so far, I believe you have a deeper understanding of "what common shell scripts are used in the production environment". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report