W moim poniższym programie, wyjście jest przekierowywane do pliku test1.txt, ale gdy otwieram plik, mam trzy problemy tutaj:Standardowe wyjście przekierowane do pliku pokazuje różni się od faktycznej
- widzę poleceń podobnie jak ls, pwd są poniżej znaku zachęty (sw0: FID128: root>).
- monit powinien wyświetlać "sw0: FID128: root>", ale pokazuje "sw0: FID128: root"
- jeśli rzeczywiste wyjście ma 2 zakładki, plik pokazuje tylko jedną kartę.
Zasadniczo chcę, aby porównać z innym plikiem, więc nie powiedzie się, jeśli liczba kart są różne.
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()
Przykładowe wyjście wygląda tak
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
dla mojego pierwszego punktu, na przykład ls -al jest przekazywane z linii wysyłania i łączna wartość 7736 drwx ------ 3 root root 4096 Feb 5 05:19 ./ drwxr-xr-x 28 root 4096 5 lutego 05:30 ../ -rw-r - r-- 1 root sys 507 Feb 1 06:47 .bash_logout -rw-r - r-- 1 root sys 27 lutego 1 06:47 .inputrc -rw-r - r-- 1 root sys 1220 Feb 1 06:47 .profile -rw-r - r-- 1 root sys 2551 Feb 1 06:47 .rhosts drwxr-xr-x 2 root sys 4096 kwi 22 2015 .ssh/ -rw-r - r-- 1 root sys 617 Feb 1 06:47 .toprc – Suma
@Suma Widzę to już. Samotny "LF" jest po znaku zachęty. Zbadaj szczegółowo, co dzieje się w tym momencie - skąd pochodzi ten "LF". –