- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
如何编写一个 Linux Bash 脚本来告诉我 LAN 中哪些计算机处于开启状态?
如果我可以给它一个 IP 地址范围作为输入会有所帮助。
最佳答案
我建议使用 nmap 的 ping-scan 标志,
$ nmap -sn 192.168.1.60-70
Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2009-04-09 20:13 BST
Host machine1.home (192.168.1.64) appears to be up.
Host machine2.home (192.168.1.65) appears to be up.
Nmap finished: 11 IP addresses (2 hosts up) scanned in 0.235 seconds
也就是说,如果你想自己写(这很公平),我会这样做:
for ip in 192.168.1.{1..10}; do ping -c 1 -t 1 $ip > /dev/null && echo "${ip} is up"; done
..以及对上述命令每一位的解释:
您可以使用 {1..10}
语法来生成数字列表,例如..
$ echo {1..10}
1 2 3 4 5 6 7 8 9 10
(它对于 mkdir {dir1,dir2}/{sub1,sub2}
之类的东西也很有用 - 这使得 dir1
和 dir2
,每个包含 sub1
和 sub2
)
因此,要生成 IP 列表,我们会执行类似的操作
$ echo 192.168.1.{1..10}
192.168.1.1 192.168.1.2 [...] 192.168.1.10
要在 bash 中循环某些内容,请使用 for
:
$ for thingy in 1 2 3; do echo $thingy; done
1
2
3
接下来,ping.. ping 命令会因操作系统、发行版/版本的不同而有所不同(我目前使用的是 OS X)
默认情况下(同样,在 OS X 版本的 ping
上)它会一直 ping 直到被中断,这对这个不起作用,所以 ping -c 1
只会尝试发送一个数据包,这足以确定机器是否已启动。
另一个问题是超时值,在这个版本的 ping 上似乎是 11 秒。它使用 -t
标志进行了更改。一秒钟应该足以查看本地网络上的机器是否处于事件状态。
所以,我们将使用的 ping 命令是..
$ ping -c 1 -t 1 192.168.1.1
PING 192.168.1.1 (192.168.1.1): 56 data bytes
--- 192.168.1.1 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss
接下来,我们需要知道机器是否回复了..
如果第一个成功,我们可以使用 &&
操作符来运行命令,例如:
$ echo && echo "It works"
It works
$ nonexistantcommand && echo "This should not echo"
-bash: nonexistantcommand: command not found
很好,我们可以这样做..
ping -c 1 -t 1 192.168.1.1 && echo "192.168.1.1 起来了!"
另一种方法是使用 ping 的退出代码。如果 ping 命令有效,它将以 exit-code 0(成功)退出,如果失败,则以非零代码退出。在 bash 中,您会使用变量 $?
所以,要检查命令是否有效,我们会这样做..
ping -c 1 -t 1 192.168.1.1;
if [ $? -eq 0 ]; then
echo "192.168.1.1 is up";
else
echo "ip is down";
fi
最后,我们不需要看到 ping 输出,所以我们可以使用 >
将 stdout
重定向到 /dev/null
> 重定向,例如:
$ ping -c 1 -t 1 192.168.1.1 > /dev/null && echo "IP is up"
IP is up
并重定向 stderr
(丢弃 ping: sendto: Host is down
消息),您使用 2>
- 例如:
$ errorcausingcommand
-bash: errorcausingcommand: command not found
$ errorcausingcommand 2> /dev/null
$
所以,结合所有这些..
for ip in 192.168.1.{1..10}; do # for loop and the {} operator
ping -c 1 -t 1 192.168.1.1 > /dev/null 2> /dev/null # ping and discard output
if [ $? -eq 0 ]; then # check the exit code
echo "${ip} is up" # display the output
# you could send this to a log file by using the >>pinglog.txt redirect
else
echo "${ip} is down"
fi
done
或者,使用 &&
方法,在单行中:
for ip in 192.168.1.{1..10}; do ping -c 1 -t 1 $ip > /dev/null && echo "${ip} is up"; done
它很慢.. 每个 ping 命令大约需要 1 秒(因为我们将 -t 超时标志设置为 1 秒)。它一次只能运行一个 ping 命令。解决此问题的明显方法是使用线程,因此您可以运行并发命令,但这超出了您应该使用 bash 的范围..
"Python threads - a first example"解释了如何使用 Python 线程模块来编写多线程 ping'er.. 虽然在那时,我还是会再次建议使用 nmap -sn
..
关于linux - 如何编写一个 Linux bash 脚本来告诉我 LAN 中哪些计算机处于打开状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/733418/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
Linux 管道可以缓冲多少数据?这是可配置的吗? 如果管道的两端在同一个进程中,但线程不同,这会有什么不同吗? 请注意:这个“同一个进程,两个线程”的问题是理论上的边栏,真正的问题是关于缓冲的。 最
我找到了here [最后一页] 一种有趣的通过 Linux 启动 Linux 的方法。不幸的是,它只是被提及,我在网上找不到任何有用的链接。那么有人听说过一种避免引导加载程序而使用 Linux 的方法
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我试图了解 ld-linux.so 如何在 Linux 上解析对版本化符号的引用。我有以下文件: 测试.c: void f(); int main() { f(); } a.c 和 b.c:
与 RetroPie 的工作原理类似,我可以使用 Linux 应用程序作为我的桌面环境吗?我实际上并不需要像实际桌面和安装应用程序这样的东西。我只需要一种干净简单的方法来在 RaspberryPi 上
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
有什么方法可以覆盖现有的源代码,我应该用 PyQt、PyGTK、Java 等从头开始构建吗? 最佳答案 如果您指的是软件本身而不是它所连接的存储库,那么自定义应用程序的方法就是 fork 项目。据我所
我的情况是:我在一个磁盘上安装了两个 linux。我将第一个安装在/dev/sda1 中,然后在/dev/sda2 中安装第二个然后我运行第一个系统,我写了一个脚本来在第一个系统运行时更新它。
我在 i2c-0 总线上使用地址为 0x3f 的系统监视器设备。该设备在设备树中配置有 pmbus 驱动程序。 问题是,加载 linux 内核时,这个“Sysmon”设备没有供电。因此,当我在总线 0
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve thi
我正试图在 linux 模块中分配一大块内存,而 kalloc 做不到。 我知道唯一的方法是使用 alloc_bootmem(unsigned long size) 但我只能从 linux 内核而不是
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我有 .sh 文件来运行应用程序。在该文件中,我想动态设置服务器名称,而不是每次都配置。 我尝试了以下方法,它在 CentOS 中运行良好。 nohup /voip/java/jdk1.8.0_71/
我是在 Linux 上开发嵌入式 C++ 程序的新手。我有我的 Debian 操作系统,我在其中开发和编译了我的 C++ 项目(一个简单的控制台进程)。 我想将我的应用程序放到另一个 Debian 操
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
我使用4.19.78版本的稳定内核,我想找到带有企鹅二进制数据的C数组。系统启动时显示。我需要在哪里搜索该内容? 我在 include/linux/linux_logo.h 文件中只找到了一些 Log
我知道可以使用 gdb 的服务器模式远程调试代码,我知道可以调试针对另一种架构交叉编译的代码,但是是否可以更进一步,从远程调试 Linux 应用程序OS X 使用 gdbserver? 最佳答案 当然
是否有任何可能的方法来运行在另一个 Linux 上编译的二进制文件?我知道当然最简单的是在另一台机器上重建它,但假设我们唯一能得到的是一个二进制文件,那么这可能与否? (我知道这可能并不容易,但我只是
我是一名优秀的程序员,十分优秀!