【问题标题】:linux and perl script inputlinux和perl脚本输入
【发布时间】:2010-10-07 03:27:00
【问题描述】:

好的,你们中的一些人可能已经注意到我已经断断续续地解决这个问题大约 3 周了。我一生都无法弄清楚发生了什么。下面是 perl 脚本,它保存来自 USB 读卡器的输入,它就像键盘一样。该机器是一个嵌入式系统,使用 voyage linux 运行在紧凑型闪存驱动器上。

use strict;
use Time::Local;

open(MATCH,'swipe_match.txt');
my @matches = <MATCH>;
close(MATCH);

my $error = qr/[+%;]E\?/;

while(1) {
        my $text = <STDIN>;
        my $text1 = <STDIN>;
        my $text2 = <STDIN>;
        if (($text && $text1 && $text2) or ($text && $text1) or $text) {
                my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime(); $year += 1900;
                $mon+=1;
                my $timestamp = "$mon/$mday/$year $hour:$min:$sec";
                chomp $text;
        chomp $text1;
        chomp $text2;

#               my $matched = 0;
#               foreach my $test (@matches) {
#                       chomp $test;
#                       $matched = 1 if ($text =~ /$test/i);
#               }
#               if ($matched) {
#                       system("aplay /SWIPE/good.wav >/dev/null 2>/dev/null");
#               } else {
#                       system("aplay /SWIPE/bad.wav >/dev/null 2>/dev/null");
#               }

        # write out the swipe even if its bad...
        open(LOG,'>>/DATA/SWIPES.TXT');
        print LOG $text."\t".$text1."\t".$text2."\t".$timestamp."\n";
        close(LOG);

        if ($text =~ $error or $text1 =~ $error or $text2 =~ $error) {
                        system("aplay /SWIPE/bad.wav >/dev/null 2>/dev/null");
        }
        else {
                        system("aplay /SWIPE/good.wav >/dev/null 2>/dev/null");
        }
        }
}
exit;

这个剧本不是我写的,写它的人早就不在了。目前我有2台机器。其中一个正在工作,另一个是我试图开始工作的那个。我试图弄清楚这个脚本是如何获得输入的(在正在工作的机器上)。我可以打开日志文件 /DATA/SWIPES.TXT 并查看实际的滑动。目前机器上没有会影响脚本的正在运行的进程,以下是进程:

  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     0:29 init [2]
    2 ?        S<     0:00 [kthreadd]
    3 ?        S<     0:04 [ksoftirqd/0]
    4 ?        S<     3:21 [events/0]
    5 ?        S<     0:00 [khelper]
   44 ?        S<     0:00 [kblockd/0]
   46 ?        S<     0:00 [kacpid]
   47 ?        S<     0:00 [kacpi_notify]
   94 ?        S<     0:00 [kseriod]
  134 ?        S      0:00 [pdflush]
  135 ?        S      0:06 [pdflush]
  136 ?        S<     0:00 [kswapd0]
  137 ?        S<     0:00 [aio/0]
  138 ?        S<     0:00 [nfsiod]
  795 ?        S<     0:00 [kpsmoused]
  800 ?        S<     0:00 [rpciod/0]
 1627 ?        S<     0:00 [ksuspend_usbd]
 1631 ?        S<     0:00 [khubd]
 1646 ?        S<     0:00 [ata/0]
 1648 ?        S<     0:00 [ata_aux]
 1794 ?        S<s    0:00 udevd --daemon
 2913 ?        Ss     0:00 pump -i eth0
 2979 ?        Ss     0:00 /usr/sbin/rpc.idmapd
 3060 ?        S      0:01 /usr/sbin/syslogd --no-forward
 3083 ?        Ss     0:00 /usr/sbin/sshd
 3099 ?        S      0:00 /usr/sbin/inetutils-inetd
 3122 ?        Ss     0:00 /usr/sbin/pptpd
 3138 ?        Ss     0:00 /usr/sbin/cron
 3149 ?        SLs    0:33 /usr/sbin/watchdog
 3167 tty2     Ss+    0:00 /sbin/mingetty tty2
 3169 tty3     Ss+    0:00 /sbin/rungetty tty3
 3170 tty4     Ss+    0:00 /sbin/rungetty tty4
 3173 tty5     Ss+    0:00 /sbin/getty 38400 tty5
 3175 tty6     Ss+    0:00 /sbin/getty 38400 tty6
15677 ?        Ss     0:00 sshd: root@pts/0
15679 pts/0    Ss     0:00 -bash
15710 ?        Z      0:00 [watchdog] <defunct>
15711 pts/0    R+     0:00 ps x

所以,从那里,我不知道该去哪里。谁能给我任何关于这个脚本如何实际接收来自 USB 阅读器的输入的建议或提示。此外,它还有一些如何在未登录的情况下接收输入。这台机器是嵌入式机器,我打开它,它接受滑动并使用 perl 脚本保存它们。

【问题讨论】:

    标签: linux perl input


    【解决方案1】:

    看看 udev,它可以:“在创建或删除设备节点时(通常在连接或拔出设备时)启动脚本”

    http://www.reactivated.net/writing_udev_rules.html

    【讨论】:

      【解决方案2】:

      关键位在这里:

      while(1) {
        my $text = <STDIN>;
      

      USB 读卡器设置为将其输入定向到 STDIN,因为它的作用类似于键盘。当它读完一张卡片时,它会发送一个回车。然后 Perl 读取“输入”并插入$text,然后等待下一次滑动。一旦完成三次滑动(三个&lt;STDIN&gt; 行),它就会处理信息并将其写入文件。然后,由于您处于while(1) 循环中,它只是循环回到循环的顶部并等待更多输入。

      您可以通过运行该程序在另一台计算机上进行模拟,然后在它等待输入时输入一些文本并按 Enter 键完成。重复此操作 3 次以模拟三次滑动,程序应对其进行处理。

      【讨论】:

      • 是的,但是程序没有在机器上运行?它也不会在前台运行。
      【解决方案3】:

      脚本正在从标准输入读取,因此您需要找到在哪里/谁在调用此脚本,并查看标准输入上的输入内容。

      您检查过系统的 cron 作业吗?您可能会通过查看 /DATA/SWIPES.TXT 文件的时间戳和所有权找到提示。

      【讨论】:

        猜你喜欢
        • 2017-11-21
        • 1970-01-01
        • 2013-02-05
        • 2016-10-02
        • 2015-08-20
        • 2012-01-02
        • 1970-01-01
        • 2011-04-29
        • 1970-01-01
        相关资源
        最近更新 更多