技术技巧

作者:Staff (员工)
声纳Ping

当我在无法看到屏幕的机器上(例如,在桌子下面,摆弄以太网电缆以找到坏的一根)排除网络故障时,我使用这个简单的脚本。当我听到ping的声音时,我知道问题解决了。 或者,你可以通过在他们的机器上运行它并在一天中的随机时间发送单个ping(或者可能用 moo 替换 ping)来让你的同事发疯。

我把它做成了一个脚本,因为我记不住那么长的一行,而且我不想输入很多次。 这是sonar.sh

#!/bin/bash
#
# Written by Mike Studer a long time ago
# Make sure you obtain a nice submarine ping sound.
# ie., ping with an echo (sonar.au used here)

/usr/sbin/icmpinfo -vv | \
  /usr/bin/nawk '$4 == "ICMP_Echo"
                   {print $0;
                    system("/usr/bin/aplay -q ~/sounds/sonar.au")}'

您需要安装icmpinfo和aplay才能使用它。

用法:在您要发出声音的机器上运行此命令(测试)

sudo sonar.sh

在尝试到达测试机器的机器上运行此命令以进行不间断的 ping 轰炸

ping {testmachine}

对于单个 ping,运行此命令

ping -c 1 {testmachine}

—Mike Studer

从更少的东西中获得更多

除了查看文本之外,less 命令还可以用于查看非文本文件。 这是通过使用 less 的能力来调用输入文件的预处理器来完成的。 然后,这些预处理器可以改变文件内容的显示方式。 例如,假设你有一个脚本 lesspipe.sh

#! /bin/sh
case "$1" in
    *.tar.gz) tar -tzvf $1 2>/dev/null
    ;;
esac

确保脚本是可执行的,并将 LESSOPEN 环境变量设置为

LESSOPEN='|/path/to/lesspipe.sh %s'

现在你可以使用 less 来查看 .tar.gz 文件的内容

$ less autocorrect.tar.gz
-rwxrwxrwx raogr/raogr  84149 2009-02-02 03:20 autocorrect.dat
-rwxrwxrwx raogr/raogr    443 2009-02-02 03:21 generator.rb
-rwxrwxrwx raogr/raogr 181712 2009-02-02 03:21 autocorrect.vim

有更复杂的 lesspipe.sh 版本可用。 您可能已经安装了一个版本,或者您可能已经安装了 lessopen.sh 脚本。 如果没有,请在 Internet 上搜索 lesspipe.sh。 使用更复杂的版本,您可以执行以下操作

$ less knoppix_5.1.1.iso
CD-ROM is in ISO 9660 format
System id: LINUX
Volume id: KNOPPIX
Volume set id:
Publisher id: KNOPPER.NET
...
/KNOPPIX
/autorun.bat
/autorun.inf
/autorun.pif
/boot
/cdrom.ico
/index.html
/KNOPPIX/KNOPPIX
/KNOPPIX/KNOPPIX-FAQ-EN.txt

—Gururaj Rao

保持笔记本电脑温度在控制之下

我一直使用笔记本电脑工作,正如你们都知道的那样,笔记本电脑有时会变热。 当您真正将它用作“膝上型”电脑,或者当您离足够近以听到风扇的声音时,您就会知道它何时在升温。 但是,当条件使得您没有意识到它正在升温时,您的笔记本电脑可能会变得非常热。 而且,你们都听过关于笔记本电脑着火的故事。

以下脚本监视温度,并在温度过高时减慢您的系统速度。 该脚本应该每分钟左右以 root 身份从 cron 运行。 您需要安装 cpufrequtils 才能使其工作

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:/usr/local/bin:

# Get the temp of the core 0
core_O=`acpi -t | awk {'print $4'} | head -n 1`

# Get the temp of the core 1
core_1=`acpi -t | awk {'print $4'} | tail -n 1`

# Round the result of core_O
convert_O=$(echo "scale=0; $core_O/1.0" | bc)

# Round the result of core_1
convert_1=$(echo "scale=0; $core_1/1.0" | bc)

# Set maximum permissible temperature.
max=90

# Set temperature at which the CPU frequency can
# be increased again (if needed).
min=68

if (( $convert_O >= $max )) ; then
    # Too hot, slow down to 800MHz.
    cpufreq-set -f 800
    echo "CPU temp higher than desired!!!" | \
        mail -s "CPU temp too high, set frequency to half" root
elif (($convert_O <= $min)) ; then
    # Cooled down, allow frequency to increase again if needed.
    cpufreq-set -g ondemand
fi

正如您所见,在脚本中,我实际上只使用了核心 0 的温度,因为我知道这个核心往往在核心 1 之前过热。

—Alberto

加载 Disqus 评论