技术流ken

运维拯救世界

DAY7:河北科技大学课堂记录–nginx和shell编程

nginx反向代理

location ~ php$ {
proxy_pass http://192.168.64.5;
}
location / {
proxy_pass http://192.168.64.7;
}

 

nginx负载均衡

upstream ken {
server 192.168.64.5 weight=1;
server 192.168.64.7 weight=2;
}

location / {
proxy_pass http://ken;
}

 

rewrite重定向

location ~ \.php$ {
rewrite (.*php)$ http://192.168.64.7/$1 last;
}

练习1:使用root用户帐号创建并执行test2.sh,实现创建一个shelltest用户,并在其家目录中新建文件try.html。

#!/bin/bash
useradd shelltest
touch /home/shelltest/try.html

 

练习2:统计当前系统总共有多少用户

[root@ken ~]# cat test2.sh
#!/bin/bash
echo “当前系统中用户数量:`cat /etc/passwd | wc -l`”

[root@ken ~]# bash test2.sh
当前系统中用户数量:27

 

练习3:统计当前已经安装的软件数量

#!/bin/bash
yum list installed | wc -l

 

grep命令详解

 

grep是行处理

 

-o 仅仅显示匹配到的内容

-n 显示匹配到的内容所在的行号

-v 取反

-i 不区分大小写

-c 统计次数

 

例子1:找到/etc/passwd中的所有的root

[root@ken ~]# grep “root” /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

 

仅仅显示root

[root@ken ~]# grep -o “root” /etc/passwd
root
root
root
root

 

统计root出现的数量

[root@ken ~]# grep -o “root” /etc/passwd | wc -l
4

 

查看crond是否在线

[root@ken ~]# ps aux| grep “crond” | grep -vc “grep”
1

 

例子2:查看root所在的行号

[root@ken ~]# grep -n “root” /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin

 

例子3:找到所有非/sbin/nologin 的行

[root@ken ~]# grep -v “nologin” /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
ken:x:1000:1000::/home/ken:/bin/bash
shelltest:x:1001:1001::/home/shelltest:/bin/bash

 

例子4:在/etc/profile中找到history默认显示的行号

[root@ken ~]# grep -i “^hist” /etc/profile
HISTSIZE=1000

 

cut命令详解

-c 指定字符数量

-d 指定分隔符

-f 指定分隔符后显示第几列

 

例子1:取出六位随机密码

[root@ken ~]# echo $RANDOM | md5sum | cut -c 1-6
475f1c

 

例子2:取出所有的用户名

[root@ken ~]# cat /etc/passwd | cut -d “:” -f 1
root
bin
daemon
adm

 

取出shell类型

[root@ken ~]# cat /etc/passwd | cut -d “:” -f 7
/bin/bash
/sbin/nologin
/sbin/nologin

 

sort

排序默认是升序

-r  降序

-n 以数字排序

例子1:根据大小进行排序

[root@ken ~]# sort -r test  #默认是字符排序
3
2
11

[root@ken ~]# sort -rn test
11
3
2

 

uniq 去重重复的行

-c 统计重复的行出现的次数

例子1:

[root@ken ~]# uniq -c test
10 2
1 11
10 3

 

 

统计访问网站IP数量并排名

[root@ken ~]# cat /var/log/httpd/access_log | cut -d ” ” -f1 | sort | uniq -c | grep -v “::” | sort -rn
98 192.168.64.4
45 192.168.64.1
8 192.168.64.7
3 192.168.64.5

 

取出访问网站前三名的IP地址

[root@ken ~]# cat /var/log/httpd/access_log | cut -d ” ” -f1 | sort | uniq -c | grep -v “::” | sort -rn | head -3
98 192.168.64.4
45 192.168.64.1
8 192.168.64.7

 

seq

seq 5   #结束位是5

seq 2 10  #起始位是2,结束位是10

seq 2 2 10 #起始位是2,步长是2,结束位是10

 

 

 

 

 

 

10 thoughts on “DAY7:河北科技大学课堂记录–nginx和shell编程

  1. Nice post. I be taught one thing more difficult on completely different blogs everyday. It’s going to all the time be stimulating to learn content material from different writers and practice slightly one thing from their store. I抎 choose to make use of some with the content on my blog whether or not you don抰 mind. Natually I抣l provide you with a link on your net blog. Thanks for sharing.

  2. I’m also writing to make you be aware of what a wonderful encounter my daughter went through reading yuor web blog. She learned such a lot of issues, most notably what it’s like to possess a wonderful teaching style to let men and women without problems know just exactly specific problematic topics. You actually did more than visitors’ desires. Thank you for distributing those productive, safe, edifying not to mention easy guidance on the topic to Jane.

  3. Thank you so much for providing individuals with an extraordinarily breathtaking possiblity to read articles and blog posts from this web site. It’s usually so brilliant and as well , packed with fun for me and my office acquaintances to visit your blog no less than 3 times per week to read through the fresh guides you have. And of course, I’m so certainly impressed for the surprising methods you serve. Certain 2 tips in this article are definitely the most impressive I have had.

  4. I precisely had to say thanks once more. I am not sure the things I would have followed without the actual secrets documented by you concerning such a area of interest. It had become a scary matter in my opinion, but noticing the expert form you treated the issue made me to jump over fulfillment. I am happy for your help and as well , trust you comprehend what a great job you have been providing training people today all through a blog. Most probably you’ve never come across all of us.

  5. I must show my thanks to you just for bailing me out of this matter. Just after scouting throughout the the web and coming across concepts which were not powerful, I believed my entire life was over. Existing without the presence of strategies to the issues you have fixed by way of your entire post is a serious case, as well as the ones that would have in a wrong way affected my entire career if I had not discovered the blog. Your primary training and kindness in maneuvering all the stuff was excellent. I don’t know what I would’ve done if I had not come across such a step like this. It’s possible to now look ahead to my future. Thanks for your time very much for the high quality and result oriented help. I won’t be reluctant to suggest your site to anybody who should get direction on this topic.

  6. A lot of thanks for all of your labor on this web page. Debby delights in carrying out investigations and it’s simple to grasp why. My partner and i hear all regarding the dynamic means you deliver important secrets via the web blog and as well as invigorate response from the others on the idea while our daughter is truly becoming educated a whole lot. Enjoy the remaining portion of the new year. Your performing a glorious job.

  7. I definitely wanted to compose a comment so as to appreciate you for all of the pleasant tips you are sharing here. My considerable internet lookup has at the end been rewarded with good quality concept to share with my two friends. I would believe that most of us visitors actually are really fortunate to dwell in a very good community with so many wonderful professionals with very helpful secrets. I feel very much fortunate to have encountered the website page and look forward to plenty of more cool minutes reading here. Thanks a lot again for a lot of things.

  8. I want to voice my admiration for your kindness for folks who require assistance with this one issue. Your special dedication to passing the message up and down turned out to be wonderfully productive and has surely encouraged ladies just like me to achieve their desired goals. Your valuable guide indicates so much a person like me and especially to my peers. Best wishes; from all of us.

  9. I must express appreciation to you just for rescuing me from this situation. Because of looking throughout the world wide web and finding things which are not helpful, I was thinking my life was done. Living without the presence of strategies to the problems you’ve solved through the guideline is a crucial case, and the ones that would have negatively damaged my career if I hadn’t come across the blog. The understanding and kindness in handling all the pieces was priceless. I am not sure what I would have done if I hadn’t come across such a step like this. It’s possible to now look forward to my future. Thanks very much for the reliable and results-oriented guide. I won’t hesitate to recommend your web sites to any person who desires guidance about this situation.

  10. I want to express my love for your kind-heartedness supporting persons that actually need guidance on this one issue. Your personal commitment to passing the message up and down turned out to be rather insightful and has regularly allowed employees just like me to arrive at their goals. Your warm and helpful help entails much to me and much more to my office colleagues. Best wishes; from each one of us.

发表评论

电子邮件地址不会被公开。