《linux Shell 脚本攻略》进阶学习(第三部分)

简介:

第七章、无网不利
  网络上每一个节点都需要分配多个参数才能与其他主机顺利实现互联。这些参数包括子网掩码、网关、路由、DNS
   这则攻略将介绍命令ifconfig、route、nslookup以及host.
实战演练
理论:网络接口规则:eth0、eth1这种命名惯例。还有一些其他的接口,如usb0\wlan0等,分别对应USB网络接口,无线LAN等网络。
ifconfig 命令用于显示网络接口,子网掩码等详细信息。

~$ echo $PATH | tr ':' "\n" | grep sbin
/usr/local/sbin
/usr/sbin
/sbin

$ ifconfig | cut -c-10 | tr ' ' ':' 
eth0::::::
::::::::::
::::::::::
::::::::::
::::::::::
::::::::::
::::::::::
::::::::::

lo::::::::
::::::::::
::::::::::
::::::::::
::::::::::
::::::::::
::::::::::
::::::::::

打印网络接口列表
$ ifconfig | cut -c-10 | tr -d ' ' | tr -s '\n'
eth0
lo

ip地址的分配与显示

只显示某个特定的接口信息
$ ifconfig wlan0
从ifconfig 输出中提取除某些地址来做另一步的处理
$ ifconfig  | egrep -o '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'
192.168.1.5
192.168.1.255
255.255.255.0
127.0.0.1
255.0.0.0



$ ifconfig |egrep -o 'inet [^ ]*' | egrep -o '[0-9.]+'
192.168.1.5
127.0.0.1
$ ifconfig eth0 |egrep -o 'inet [^ ]*' | egrep -o '[0-9.]+'
192.168.1.5


ifconfig eth0 |egrep -o 'inet [^ ]* '
inet 地址:192.168.1.5 

设置设置网络接口IP地址
$ ifconfig eth0 192.168.0.80
设置子掩码
ifconfig wlan0 192.168.0.80 netmask 255.255.252.0

硬件地址欺骗
ifconfig eth0 hw ether 00:1c:bf:87:25:d5
在上面的命令中,00:1c:bf:87:25:d5就是新MAC分配的新MAC地址

名字服务器与DNS
internet最根本的寻址方案是IP地址

$ cat /etc/resolv.conf 
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 127.0.1.1
#可以这样手动添加名字服务器
$ echo nameserver IP_ADDRESS >> /etc/resolv.conf

DNS查找
$ host baidu.com #host 也可以列出资源记录,比如MX(mail)
baidu.com has address 220.181.111.86
baidu.com has address 123.125.114.144
baidu.com has address 220.181.111.85
baidu.com mail is handled by 20 mx1.baidu.com.
baidu.com mail is handled by 20 jpmx.baidu.com.
baidu.com mail is handled by 20 mx50.baidu.com.
baidu.com mail is handled by 10 mx.mailcdn.baidu.com.

$ nslookup google.com
Server:        127.0.1.1
Address:    127.0.1.1#53

Non-authoritative answer:
Name:    google.com
Address: 173.194.72.139
Name:    google.com
Address: 173.194.72.101
Name:    google.com
Address: 173.194.72.100
Name:    google.com
Address: 173.194.72.138
Name:    google.com
Address: 173.194.72.102
Name:    google.com
Address: 173.194.72.113

应用:向/etc/host 添加条目

设置默认网关,显示路由表信息
route
内核 IP 路由表
目标            网关            子网掩码        标志  跃点   引用  使用 接口
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
link-local      *               255.255.0.0     U     1000   0        0 eth0
192.168.1.0     *               255.255.255.0   U     1      0        0 eth0

$ route -n # -n制定以数字形式显示地址
内核 IP 路由表
目标            网关            子网掩码        标志  跃点   引用  使用 接口
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0

设置默认网关
$ route add default gw 192.168.0.1 eth0


ping 查看网络上的所有主机
#!/bin/bash

network="192.168.1"
for sitenu in $(seq 1 100)
do
(ping -c 1 -w 1 ${network}.${sitenu} &> /dev/null && echo ${network}.${sitenu} up || echo ${network}.${sitenu} down)&
done

wait

原理 ()& 将任务放入后台操作;每次ping都非常慢; -w 延迟1秒

wait 放在脚本最后,它就会一直等到所有的子进程全部结束


传输文件
使用lftp 命令访问tfp服务器以便传输文件。它使用的端口是21,只有远程主机上装有FTP服务区才能FTP。很多公共网站都是用FTP共享文件
$ lftp username@ftphost

ssh基础上的文件传输
SCP (secure copy,安全复制)
scp -r /home/tmp user@remotehost:/home/backups #将目录下的/home/tmp 递归复制到远程主机中


网络流量与端口分析

$ lsof -i

列出本地主机当前开放的端口
$ lsof -i | grep ":[0-9]\+->" -o | grep "[0-9]\+" -o | sort | uniq
59699

:[0-9]\+-> 用来从lsof 输出中提取主机端口部分(:34395);第二个提取端口号(数字)

netstat -tnp 列出开放端口与服务


第八章、当个好管家
统计硬盘使用情况
df和du;df是disk free;du是disk usage

实战
du file;du dir
$ du touchlearnfiles.sh 
4    touchlearnfiles.sh

du -a dir 递归显示所有文件的硬盘使用情况

查找大文件
$ find . -type f -exec du -k {} \; | sort -nrk 1 | head
44    ./linux命令.txt
8    ./第一章.txt
8    ./第七章.txt
8    ./第二章.txt
4    ./第四章.txt
4    ./第三章
4    ./第八章.txt
4    ./touchlearnfiles.sh
4    ./third/remove_duplicates.sh
4    ./third/filestat.sh

磁盘可用空间信息
$ df
$ df -h

计算命令执行时间
$ time command

与当前登陆用户、启动日志及启动故障的相关信息
介绍一下命令: who、w、user、uptime(通电时间)、last、lastb(获取登陆失败的用户,必须以管理员运行)、lastlog
last zhangjianlin


列出最常用的10条命令

1
2
3
4
5
6
7
#!/bin/bash
printf  "command\tcount\n"  ;
cat  ~/.bash_history |  awk  '{ list[$1]++; }\
END{
for (i  in  list){
printf ( "%s\t%d\n" ,i,list[i]);}
}' |  sort  -nrk 2 |  head

工作原理
计数值添加1(list[$1]++)。$1是输入文本行的第一个单词

列出1小时内占用cpu最多的10个进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
SECS=3600
UNIT_TIME=60
STEPS=$(( $SECS / $UNIT_TIME ))
echo  watching cpu usage...;
for ((i=0;i<STEPS;i++))
do
ps  -eo  comm ,pcpu |  tail  -n +2 >>  /tmp/cpu_usage .$$
sleep  $UNIT_TIME
done
echo
echo  cpu eaters:
cat  /tmp/cpu_usage .$$ | \
awk  '
{ process[$1]+=$2; }
END{
     for (i  in  process){
     printf ( "%-20s %s" ,i,process[i]);
}
}' |  sort  -nrk 2 |  head
rm  /tmp/cpu_usage .$$

用watch 监视命令输出(默认每2秒更新输出一次)
watch COMMAND
watch ls

-n 指定所要更新输出的时间间隔
watch -n 5 'ls -l'
-d使输出差异以不同的颜色突出表示出来
watch -d command

用logrotate  管理日志文件
logrotate配置目录位于/etclogrotate.d
自己可以在这编写一个特定的配置

通过监视用户登陆找出入侵者
原理:
1)扫描日志文件从中收集所要的信息;处理ssh登陆失败的情况。
2)用户认证的对话记录在/var/log/auth.log中,脚本扫描这个文件来检测出失败的登陆信息,并执行不同的检测来获取需要的数据;
3)我们可以用host命令找出IP地址所对应的主机

检测的数据有如下
试图登陆的账户
试图登陆的次数
攻击者的IP地址
IP地址所对应的主机
进行登陆过的时间段

工作原理:如果有非法用户
failed password for bob1 from 203.83.248.32 port 50035 ssh2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/bash
AUTHLOG= /var/log/auth .log
if  [[ -n $1 ]];
then
AUTHLOG=$1
echo  Using log  file :$AUTHLOG
fi
LOG= /tmp/valid .$$.log
grep  - v  "invalid"  $AUTHLOG > $LOG
users =$( grep  "Faild passwd "  $LOG | awk  '{print $(NF-5) }'  sort  uniq )
printf  "%-5s|%-10s|%-10s|%-13s|%-33s|%s\n"  "Sr#"  "User"  "Attempts"  "ip address"  "Host_Mapping"  "Time rangs"
ucount=0;
ip_list= "$(egrep -o " [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ " $LOG | sort | uniq)"
for  ip  in  $ip_list;
do
grep  $ip $LOG >  /tmp/temp .$$.log
for  user  in  $ users ;
do
grep  $user  /tmp/temp .$$.log >  /tmp/ $$.log
cut  -c-16  /tmp/ $$.log > $$. time
tstart=$( head  -1 $$. time );
start=$( date  -d  "$tstart"  "+%s" );
tend=$( tail  -1 $$. time );
end=$( date  -d  "$tend"  "+%s" )
limit=$(( $end - $start ))
if  [ $limit -gt 120 ];
then
let  ucount++;
IP=$( egrep  -o  "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"  /tmp/ $$.log |  head  -1 );
TIME_RANGE= "$tstart-->$tend"
ATTEMPTS=$( cat  /tmp/ $$.log |  wc  -l);
HOST=$(host $IP |  awk  '{print $NF }' )
printf  "%-5s | %-10s | %-10s | %-10s | %-33s | %-s\n"  "$ucount"  "$user"  "$ATTEMPTS"  "$IP"  "$HOST"  "$TIME_RANGE" ;
fi
done
done
rm  /tmp/vaild .$$.log  /tmp/ $$.log $$. time  /tmp/temp .$$.log 2>  /dev/null


用法:bash filenam.sh 或./filename.sh otherauth.log

找出系统中用户的活的时段
last 命令列出一个系统中有关用户登陆回话的细节。这些回话存储在/var/log/wtmp文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#/bin/bash
log= /var/log/wtmp
if  [[ -n $1 ]];
then
     log=$1
fi
printf  "%-4s %-10s %-10s %-6s %-8s\n"  "Rank"  "User"  "Start"  "Logins"  "Usage hours"
last -f $log |  head  -n -2 >  /tmp/ulog .$$
cat  /tmp/ulog .$$ |  cut  -d  ' '  -f1 |  sort  uniq  /tmp/users .$$
(
while  read  user;
do
     grep  ^$user  /tmp/ulog .$$ >  /tmp/user .$$
     seconds=0;
while  read  t
     do
     s=$( date  -d $t +%s 2>  /dev/null )
     let  seconds=seconds+s
     done < <( cat  /tmp/user .$$ |  awk  '{print $NF}'  tr  -d  ')(' )
firstlog=$( tail  -n 1  /tmp/user .$$ |  awk  '{print $5,$6}' )
nlogins=$( cat  /tmp/user .$$ |  wc  -l)
hours=$( echo  "$seconds / 60.0"  bc )
printf  "%-10s %-10s %-6s %-8s\n"  $user  "$firstlog"  $nlogins $hours
done  /tmp/users .$$
) |  sort  -nrk 4 |  awk  '{ printf("%-4s %s\n",NR, $0) }'
rm  /tmp/users .$$  /tmp/user .$$  /tmp/ulog .$$



9.管理重任
进程管理的重要命令top、ps、和pgrep。

which、whereis、file、whatis与平均负载
which 在$PATH 中查找
whereis 和which类似,但whereis不仅返回命令路径,并打印对应的命令手册的位置以及命令源码路径

平局负载 uptime

收集系统信息
hostname
uname
cat /proc/cpuinfo
cat /proc/meminfo
cat /proc/part 分区信息

$ cat /proc/partitions  或fdisk -l
major minor  #blocks  name

  8        0  488386584 sda
  8        1  482191360 sda1
  8        2          1 sda2
  8        5    6192128 sda5
 11        0    1048575 sr0


$lshw #获取系统的详细信息


这三个部分对一般linux用户来说已经够用了。


本文转自lilin9105 51CTO博客,原文链接:http://blog.51cto.com/7071976/1251952,如需转载请自行联系原作者

相关文章
|
11天前
|
Web App开发 Java Linux
Linux之Shell基本命令篇
Linux之Shell基本命令篇
Linux之Shell基本命令篇
|
1天前
|
运维 监控 Shell
利用Shell脚本编写局域网监控软件:实时监测主机连接情况
本文介绍了如何使用Shell脚本创建一个局域网监控工具,以实时检查主机连接状态。脚本包括扫描IP地址范围检测主机可达性及使用`netstat`监控ESTABLISHED连接。此外,还展示了如何每60秒将连接数数据自动提交到指定网站API,以便实时跟踪网络活动。这个自动化监控系统有助于提升网络安全性和故障排查效率。
7 0
|
2天前
|
Shell
Shell脚本之流程控制语句
Shell脚本之流程控制语句
|
3天前
|
JSON 运维 监控
训练shell常用脚本练习(三)
【4月更文挑战第14天】shell代码训练(三)
14 1
|
7天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
137 0
|
7天前
|
弹性计算 Shell Go
ecs服务器shell常用脚本练习(九)
【4月更文挑战第10天】shell代码训练(八)
124 0
|
9天前
|
存储 Shell Linux
【攻防世界】unseping (反序列化与Linux bash shell)
【攻防世界】unseping (反序列化与Linux bash shell)
|
10天前
|
Shell
shell学习(六) 【case多条件分支语句】
shell学习(六) 【case多条件分支语句】
11 1
|
10天前
|
Shell 应用服务中间件 nginx
shell学习(七) 【shell 函数】
shell学习(七) 【shell 函数】
8 1
|
10天前
|
Shell Perl
shell学习(九) 【shell sed用法】
shell学习(九) 【shell sed用法】
8 0