shell脚本杂记(四)

1,wget是一个用于文件下载的命令行工具,选项繁多且用法灵活,wget可以下载网页或远程文件,命令: wget url
[code="java"]# wget http://www.cnblogs.com/
--2014-09-20 02:00:48-- http://www.cnblogs.com/
正在解析主机 www.cnblogs.com... 42.121.252.58
正在连接 www.cnblogs.com|42.121.252.58|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:44681 (44K) [text/html]
正在保存至: “index.html.1”

100%[======================================================================================================================================>] 44,681 --.-K/s in 0.07s

2014-09-20 02:00:48 (613 KB/s) - 已保存 “index.html.1” [44681/44681])

# [/code]
用-t指定重复次数
wget -t 5 url
或者要求wget不停的重试
wget -t 0 url
下载限速
wget --limit--rate 20k url
也可也用k和m来对指定速度限制,除此之外还可以指定下载的最大配额
wget -Q 100m url
断点续传
wget -c url
复制整个网站
wget --mirror --convert-links url
限制深度
wget -r -N -1 -k DEPTH url
访问需要认证的HTTP或页面
wget --user username --password pass url


2,curl作为一款强力工具,支持http,https,ftp等协议,还支持post,cookie,认证,断点下载,referer,用户代理字符串,扩展头部,限速,文件大小限制,进度条等特性
将下载的文件输出到终端:
curl url
避免显示进度条
curl url --silent
选项-o将下载数据写入文件,而非标准输出中,该文件采用的是url中解析出的文件名:
curl url --silent -O
断点续传
curl -C - url

3,
tar -zxvf xxx.tar.gz解压tar文件
tar -zcvf 打包一个目录,并启动gzip压缩
gzip -n xxx.xx指定一个压缩
1级压缩率最快,但速度最快
9级压缩率高,但速度慢
gunzip xxx.gz解压压缩
zip file.zip file 生成zip压缩
unzip 解压zip

4,查看ip信息ifconfig
查看路由信息route
[code="java"]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.46.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default localhost 0.0.0.0 UG 0 0 0 eth0
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.46.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.46.254 0.0.0.0 UG 0 0 0 eth0
#

[/code]

5,ping命令查看网络通信情况,我们使用-c指定ping的组数:
[code="java"]# ping zk1 -c 3
PING zk1 (192.168.46.28) 56(84) bytes of data.
64 bytes from fse1 (192.168.46.28): icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from fse1 (192.168.46.28): icmp_seq=2 ttl=64 time=0.034 ms
64 bytes from fse1 (192.168.46.28): icmp_seq=3 ttl=64 time=0.037 ms

--- zk1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.023/0.031/0.037/0.007 ms
# [/code]

一个小脚本判断网络是否通畅:

ping $1 -c 2

if [ $? -eq 0 ];then

echo "网络成功!"

else

echo "网络失败!"

fi


列出网上,所有活动的主机:

[code="java"]# cat b.sh


for ip in 192.168.46.{1..255}
do


ping $ip -c 2 &> /dev/null ;

if [ $? -eq 0 ];then
echo $ip "处于在线状态"
fi

done
# [/code]

上面的例子只是单线程的ping,速度非常之慢,下面的这个脚本,可以以并行的方式运行,最后使用wait命令阻塞,等待子进程执行完,再结束。


for ip in 192.168.46.{1..255}
do
(

ping $ip -c 2 &> /dev/null ;

if [ $? -eq 0 ];then
echo $ip "处于在线状态"
fi

)&

done
wait


当然还有更简洁的命令fping,如果你的centos系统没有自带的话,需要执行如下命令安装:
rpm -ivh http://pkgs.repoforge.org/fping/fping-3.1-1.el6.rf.i686.rpm
[code="java"]# fping -a -s -g 192.168.46.1 192.168.46.25
192.168.46.12
192.168.46.13
192.168.46.15
192.168.46.16
192.168.46.18
192.168.46.20
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.1
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.1
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.1
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.2
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.2
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.2
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.3
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.3
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.3
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.4
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.4
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.4
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.5
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.5
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.5
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.6
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.6
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.6
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.7
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.7
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.7
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.8
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.8
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.8
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.9
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.9
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.9
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.10
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.10
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.10
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.11
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.11
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.11
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.14
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.14
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.14
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.19
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.19
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.19
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.21
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.21
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.21
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.22
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.22
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.22
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.23
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.23
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.23
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.24
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.24
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.24
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.25
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.25
ICMP Host Unreachable from 192.168.46.28 for ICMP Echo sent to 192.168.46.25

25 targets
6 alive
19 unreachable
0 unknown addresses

19 timeouts (waiting for response)
82 ICMP Echos sent
6 ICMP Echo Replies received
54 other ICMP received

0.32 ms (min round trip time)
1.24 ms (avg round trip time)
1.85 ms (max round trip time)
4.716 sec (elapsed real time)

# [/code]

6, ssh host可以连接到远程主机上,如果想在远程主机执行命令可以用:
ssh host "cmd1; cmd2; cmd3"执行,注意一定是放在ssh后面使用,否则不生效

指定一个端口连接ssh host -p 333
ssh的压缩功能:
ssh -C host commands
在远程主机上执行执行图形化命令
ssh host "export DISPLAY=:0; cmd ; cmd ..."
在本地启动图形化命令,操作远程主机
ssh -X host "cmd1;cmd2;cmd3"


scp 可以方便的传输文件
scp -r /home/test host:/home/test

7,创建ssh认证
执行ssh-keygen-t rsa -t -P ''
然后执行ssh-copy-id -i .ssh/id_ras.pub host进行认证

8,ssh进行端口转发
下面的命令将本地主机端口为8000上的流量转上到指定地址的80端口上
ssh -L 8000:www.kernel.org:80 host
如果host不是localhost,那么就是将远程主机端口上的8000流量转发到www.kernel.org的80端口上。

9,非交互式端口转发
ssh -fL 8000:www.abc.com:80 user@host -N
-f指定ssh在执行命令前转入后台模式,-L指定远程的登陆名,-N告诉ssh无需执行命令,只进行端口专发。


10,反向端口转发是SSH最强大的特性之一,如果你有一台无法通过互联网进行访问的主机,但是又希望其他的用户可以访问到这台主机上的服务,那么就是反向端口转发的大显身手的时候了,如果你使用ssh访问一台可以通过互联网访问的远程主机,那么就可以在这台主机上设置反向端口转发,将流量转发到运行服务的本地主机上:

ssh -R 8000:www.abc.com:80 user@host
上述命令会将远程主机端口8000上的流量,转发到本地主机的端口80上,利用这种方法,你在远程主机http://192.168.23.23:8000上访问,那么实际连接的是运行在本地主机端口8000上的web服务器。


11,在本地挂载点上挂载远程驱动器
sshfs -o allow_other host:/home/path /mnt/mountpoint
执行本条命令然后输入密码,现在位于远程主机上/home/path下的数据就可以通过本地挂载点/mnt/mountpoint来访问了。


完成任务后,卸载命令如下umount /mnt/mountpoint

12,网络流量与端口分析
lsof -i查看开放端口以及运行的服务。
netstat -tnp 列出开放端口与服务

13,
创建套接字 nc -l 1234
nc host 1234 连接到该套接字

在网络上进行快速文件复制
在接收端: nc -l 1234 > destion_file
在发送端: nc host 1234 < source_file

14,使用iptables阻塞流量
阻塞发送到特定ip的流量
iptables -A OUTPUT -d 8.8.8.8 -j DROP
阻塞发送到特定端口的流量
iptables -A OUTPUT -p tcp -dropt 21 -j DROP

清楚ip链所作出的所有改动
iptalbes --flush
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值