《Linux Shell脚本攻略》读书笔记第七章 无网不利
原创
©著作权归作者所有:来自51CTO博客作者stonebox的原创作品,请联系作者获取转载授权,否则将追究法律责任
1、联网
[root@stone file]# ifconfig | cut -c1-10 | tr -d ' ' | tr -s '\n'
eth0
eth0:0
lo
peth0
vif0.0
virbr0
xenbr0
#打印可用的网络接口列表
[root@stone file]# ifconfig eth0 | egrep -o "inet addr:[^ ]*" | egrep -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
172.16.3.54
#获取某个接口的Ip地址
[root@stone ~]# cat /etc/resolv.conf
; generated by /sbin/dhclient-script
search localdomain
nameserver 172.16.16.114
nameserver 61.128.128.68
#DNS
[root@stone ~]# host www.baidu.com
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 115.239.210.27
www.a.shifen.com has address 115.239.210.26
#www.baidu.com对应的所有IP地址
2、ping
[root@stone ~]# ping -c 2 www.baidu.com
PING www.a.shifen.com (115.239.210.27) 56(84) bytes of data.
64 bytes from 115.239.210.27: icmp_seq=1 ttl=53 time=53.2 ms
64 bytes from 115.239.210.27: icmp_seq=2 ttl=53 time=54.1 ms
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 53.240/53.719/54.199/0.532 ms
#-c指定发送的包的数量
[root@stone ~]# cat bin/ping.sh
#!/bin/bash
for ip in 172.16.3.{1..255};
do
ping -c 1 $ip &> /dev/null;
if [ $? -eq 0 ];
then
echo $ip is alive
fi
done
#该程序等待时间过长
3、ssh
[root@stone ~]# ssh root@172.16.3.52
The authenticity of host '172.16.3.52 (172.16.3.52)' can't be established.
RSA key fingerprint is e7:ba:36:5a:de:70:60:34:cf:30:29:41:74:6b:f6:26.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.3.52' (RSA) to the list of known hosts.
root@172.16.3.52's password:
Last login: Wed May 22 15:17:39 2013 from 172.16.3.151
[root@localhost ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog
[root@localhost ~]# exit
logout
Connection to 172.16.3.52 closed.
[root@stone ~]# ssh root@172.16.3.52 cat text1
root@172.16.3.52's password:
aaa
#访问远程主机后执行命令
[root@stone ~]# ssh root@172.16.3.52 "cat text1;echo aaa >> text1;cat text1"
root@172.16.3.52's password:
aaa
aaa
aaa
[root@stone ~]# ssh root@172.16.3.52 "echo user:$(whoami);echo os:$(uname)"
root@172.16.3.52's password:
user:root
os:Linux
[root@localhost ~]# commands1="echo user:$(whoami);echo os:$(uname)"
[root@localhost ~]# ssh root@172.16.3.52 $commands1
root@172.16.3.52's password:
user:root
os:Linux
#多条命令需要加上引号或者通过变量引用
[root@stone ~]# ssh root@172.16.3.52 "cat text1" > text1
root@172.16.3.52's password:
[root@stone ~]# cat text1
aaa
aaa
#将远程主机上执行的命令重定向到本机文件
[root@localhost ~]# cat text1
aaa
aaa
[root@localhost ~]# ssh root@172.16.3.52 "cat >> text1" < text1
root@172.16.3.52's password:
[root@localhost ~]# ssh root@172.16.3.52 cat text1
root@172.16.3.52's password:
aaa
aaa
aaa
aaa
#将本地主机上的文件重定向到远程主机文件
[root@localhost ~]# ssh -C root@172.16.3.52 cat text1
root@172.16.3.52's password:
aaa
aaa
#-C选项启用压缩功能
#无密码自动登录
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
d1:d3:68:e2:ff:bb:42:3b:0f:57:6c:3a:97:b5:d9:72 root@localhost.localdomain
#生成本机的公钥和私钥
[root@stone ~]# ls .ssh/
authorized_keys id_rsa id_rsa.pub known_hosts
[root@stone ~]# scp .ssh/id_rsa.pub root@172.16.3.52:.ssh/authorized_keys
root@172.16.3.52's password:
id_rsa.pub 100% 392 0.4KB/s 00:00
#将本机的公钥复制到远程主机的.ssh下,文件名为authorized_keys即可
[root@stone ~]# ssh root@172.16.3.52 chmod -R go-rwx .ssh
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
《Linux Shell脚本攻略》 笔记 第七章:网络操作
《Linux Shell脚本攻略》 笔记第七章:网络操作1、打印网络接口列表[root@localhost touch_more]# ifconfig | cut -c
网络 Shell 脚本 命令 .net bash linux -
Linux Shell -- 无网不利
这篇文章中我介绍几个非常实用的和网络相关的命令一.ifconfig这个命令在Windows下被“翻译为ipconfig”,它用于显示网络接口的信息:相关技
linux shell 网络 硬件 Windows 百度 -
Linux 第七章(总结)
网络信息查看命令ifconfig (-a)  
linux 职场 休闲