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

简介:

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

~echoPATH | 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地址的分配与显示

只显示某个特定的接口信息
ifconfigwlan0ifconfig 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.typefexecduk|sortnrk1|head44./linux.txt8./.txt8./.txt8./.txt4./.txt4./4./.txt4./touchlearnfiles.sh4./third/removeduplicates.sh4./third/filestat.sh df
dfh 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 sr0lshw #获取系统的详细信息


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


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

目录
打赏
0
0
0
0
69
分享
相关文章
MySQL 备份 Shell 脚本:支持远程同步与阿里云 OSS 备份
一款自动化 MySQL 备份 Shell 脚本,支持本地存储、远程服务器同步(SSH+rsync)、阿里云 OSS 备份,并自动清理过期备份。适用于数据库管理员和开发者,帮助确保数据安全。
|
11天前
|
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
67 25
|
8天前
|
shell_42:Linux参数移动
总的来说,参数移动是Linux shell脚本中的一个重要概念,掌握它可以帮助我们更好地处理和管理脚本中的参数。希望这个解释能帮助你理解和使用参数移动。
38 18
|
19天前
|
阿里云linux服务器使用脚本通过安全组屏蔽异常海外访问ip
公网网站可能会遭受黑客攻击导致访问异常,使用此脚本可以屏蔽掉异常IP 恢复访问。也可自行设置定时任务定期检测屏蔽。
124 28
零基础保姆级教程!手把手教你免费玩转Linux CentOS安装+学习环境搭建(附避坑指南)
本文详细介绍了在VMware虚拟机中安装CentOS 6.8的全过程。首先,需确保已安装VMware并开启V-CPU虚拟化功能,可通过BIOS设置或使用LeoMoon CPU-V工具检测。接着,下载CentOS镜像文件,并在VMware中新建虚拟机,配置CPU、内存、硬盘等参数。最后,加载ISO镜像启动虚拟机,按照提示完成CentOS的安装,包括语言、键盘、存储方式、地区、密码设置及硬盘分区等步骤。安装完成后,以root用户登录即可进入系统桌面,开始学习Linux命令和操作。
187 12
零基础保姆级教程!手把手教你免费玩转Linux CentOS安装+学习环境搭建(附避坑指南)
【Linux快速入门(三)】Linux与ROS学习之编译基础(Cmake编译)
【Linux快速入门(三)】Linux与ROS学习之编译基础(Cmake编译)
347 2
|
3月前
|
【linux】Shell脚本中basename和dirname的详细用法教程
本文详细介绍了Linux Shell脚本中 `basename`和 `dirname`命令的用法,包括去除路径信息、去除后缀、批量处理文件名和路径等。同时,通过文件备份和日志文件分离的实践应用,展示了这两个命令在实际脚本中的应用场景。希望本文能帮助您更好地理解和应用 `basename`和 `dirname`命令,提高Shell脚本编写的效率和灵活性。
160 32
定期备份数据库:基于 Shell 脚本的自动化方案
本篇文章分享一个简单的 Shell 脚本,用于定期备份 MySQL 数据库,并自动将备份传输到远程服务器,帮助防止数据丢失。
linux应急响应检查脚本
通过这个脚本,可以快速收集系统的关键信息,有助于在发生问题时进行及时的应急响应和分析。
146 34