The standard output redirected to a file is different from the actual

In my program below, the output is redirected to the test1.txt file, but when I open the file, I have three problems:

  • I see that commands like ls, pwd are under the prompt (sw0: FID128: root>).
  • it is expected that "sw0: FID128: root>" appears in the prompt, but it shows "sw0: FID128: root"
  • If the actual output has 2 tabs, the file shows only 1 tab.

Basically I want it to compare with another file, so it will fail if the number of tabs is different.

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() 

Sample output is as follows

 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 
+5
source share
1 answer

I see that commands like ls, pwd are under the prompt

Your output has inconsistent newlines ( ^M is CR ). This means that some parts of your ensemble output CR+LF , others only LF . It is hard to say what exactly from one exit. Separate them and see for yourself.

it is expected that "sw0: FID128: root>" appears in the prompt, but it shows "SW0: FID128: root"

Possible reasons:

  • You cannot accurately reproduce the result. For instance. something is printed on stderr .
    • use 2>&1 or the equivalent or else you will see both outputs somehow
  • When redirecting, the output is actually different from what will be visible on the console. Some programs do this using the isatty() function to tell them apart. The rationale, as a rule, is to better fit the reading output by a person or machine. Many such programs have switches to explicitly indicate the output taste.
    • you can use something like | cat | cat (to redirect output) or cat <command_file> | (to redirect input) to make sure it is.

With telnet it can be either. > is the command line. It is usually printed on stderr . It may still be printed or not printed at all when redirecting.

if the actual output has 2 tabs, the file shows only 1 tab.

Probably the same as 2). Perhaps something that goes through the output also converts the tab.

0
source

Source: https://habr.com/ru/post/1242168/


All Articles