【问题标题】:Standard output redirected to file shows different from actual重定向到文件的标准输出显示与实际不同
【发布时间】:2016-05-12 09:25:37
【问题描述】:

在我下面的程序中,输出被重定向到文件 test1.txt,但是当我打开文件时,我在这里遇到了三个问题:

  1. 我看到 ls、pwd 等命令位于提示符下方 (sw0:FID128:root>)。
  2. 提示应显示“sw0:FID128:root>”,但它显示“sw0:FID128:root”
  3. 如果实际输出有 2 个选项卡,则文件仅显示 1 个选项卡。

我基本上希望它与另一个文件进行比较,因此如果选项卡数量不同,它将失败。

 telconn=pexpect.spawn('telnet 10.24.12.109')
 telconn.logfile = sys.stdout
 telconn.expect(":")
 telconn.send("user" + "\r")
 telconn.expect(":")
 telconn.send("pass" + "\r\r\r\r\n\n\n")
 telconn.expect("key to proceed.")
 telconn.send ("\003")
 telconn.expect("root>")
 prev_std= sys.stdout
 sys.stdout=open("test1.txt","w")

 print "Telnet connection is done"

 telconn.sendline('\n');
 telconn.expect (['>',pexpect.EOF])
 ls = telconn.before

 telconn.sendline('ls -al');
 telconn.expect (['>',pexpect.EOF])
 ls = telconn.before

 telconn.sendline('pwd');
 telconn.expect (['>',pexpect.EOF])
 pwd = telconn.before

 telconn.sendline('noscli');
 telconn.expect (['#',pexpect.EOF])
 nos = telconn.before

 telconn.sendline('terminal length 0');
 telconn.expect (['#',pexpect.EOF])
 term = telconn.before

 telconn.sendline('\n\n');

 telconn .sendline('exit');
 telconn.close()

 print ls
 print pwd
 print nos
 print term

 #print "Ended session"


 sys.stdout.close()
 sys.stdout =prev_std

 fo = open("test1.txt", "r+")
 str = fo.read();
 print "Read String is : ", str
 # Close opend file
 fo.close()

示例输出如下所示

Telnet connection is done
 ^M
sw0:FID128:root
 ls -al^M
total 32^M
pwddrwx------  3 root root 4096 Feb  2 11:07 ./^M
^M
drwxr-xr-x 28 root root 4096 Feb  3 05:58 ../^M
-rw-r--r--  1 root sys   507 Feb  1 06:47 .bash_logout^M
-rw-r--r--  1 root sys    27 Feb  1 06:47 .inputrc^M
-rw-r--r--  1 root sys  1220 Feb  1 06:47 .profile^M
-rw-r--r--  1 root sys  2551 Feb  1 06:47 .rhosts^M
drwxr-xr-x  2 root sys  4096 Feb  1 09:51 .ssh/^M
-rw-r--r--  1 root sys   617 Feb  1 06:47 .toprc^M
-rw-r--r--  1 root root    0 Feb  3 06:01 mcast_trc^M
-rw-r--r--  1 root root    0 Feb  3 06:01 sysdiag_trc^M
sw0:FID128:root
 pwd^M
/root^M

【问题讨论】:

    标签: python shell io


    【解决方案1】:

    我看到 ls、pwd 等命令在提示符下方

    您的输出包含不一致的换行符(^MCR)。这意味着您的合奏的某些部分输出CR+LF,其他部分 - 只是LF。很难从单个输出中分辨出哪个是哪个。把它们分开,自己看看。

    提示应显示“sw0:FID128:root>”,但它显示 "sw0:FID128:root"

    可能的原因:

    1. 您没有忠实地捕获输出。例如。一些东西也会打印到stderr
      • 使用2>&1 或等效项,或者以其他方式查看两个输出
    2. 重定向时,输出实际上与控制台上看到的不同。有些程序会这样做,使用isatty() 函数来区分它们。基本原理通常是为了更好地适合人类或机器阅读的输出。许多这样的程序都有明确指定输出风格的开关。
      • 您可以使用| cat(用于输出重定向)或cat <command_file> |(用于输入重定向)来检查是否是这种情况

    使用telnet,两者都可以。 > 是命令提示符。它通常打印到stderr。有重定向的时候还是可以打印,也可以不打印。

    如果实际输出有 2 个选项卡,则文件仅显示 1 个选项卡。

    可能与 2) 相同。也许输出经过的东西也会进行制表符转换。

    【讨论】:

    • 对于我的第一点,例如 ls -al 从 sendline 传递并输出总计 7736 drwx------ 3 root root 4096 Feb 5 05:19 ./ drwxr-xr-x 28根根 4096 2 月 5 日 05:30 ../ -rw-r--r-- 1 根系统 507 2 月 1 日 06:47 .bash_logout -rw-r--r-- 1 根系统 2 月 1 日 06:47。 inputrc -rw-r--r-- 1 根系统 1220 2 月 1 日 06:47 .profile -rw-r--r-- 1 根系统 2551 2 月 1 日 06:47 .rhosts drwxr-xr-x 2 根系统 4096 2015 年 4 月 22 日 .ssh/ -rw-r--r-- 1 root sys 617 Feb 1 06:47 .toprc
    • @Suma 我已经看到了。唯一的LF 在提示之后。因此,请详细检查此时发生的情况 - LF 来自哪里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多