I connect to remote server with ssh with my python codes. I connect and get results. But results are coming with rn My codes are below :
JavaScript
x
16
16
1
#To connect remote server and run networkmon.py and get results
2
from pexpect import pxssh
3
import getpass
4
try:
5
s = pxssh.pxssh()
6
hostname = "10.89.71.39"
7
username = "manoadmin"
8
password = "********" #generic pass added
9
s.login(hostname, username, password)
10
s.sendline('cat manobackup_scripts/networkmon.py')
11
s.prompt()
12
print(s.before)
13
s.logout()
14
except pxssh.ExceptionPxssh as e:
15
print("pxssh login hata.")
16
The output i get is like;
import osrnimport socketrnrnimport timernrnstart_time = time.time()rnrn
But i want the out put to be printed out as below
import os import socket
import time
start_time = time.time()
Advertisement
Answer
By default data in s.before
is of bytes
, not str
. You can decode when printing:
JavaScript
1
2
1
print(s.before.decode() )
2
You can ask it to automatically convert the data to str
by specifying an encoding
:
JavaScript
1
4
1
s = pxssh.pxssh(encoding='utf8')
2
3
print(s.before)
4