我使用Python的Paramiko库来SSH远程机器并从命令行获取一些输出.我看到很多垃圾打印和实际输出.如何摆脱这个?
chan1.send("ls\n")
output = chan1.recv(1024).decode("utf-8")
print(output)
[u'Last login: Wed Oct 21 18:08:53 2015 from 172.16.200.77\r', u'\x1b[2J\x1b[1;1H[local]cli@BENU>enable', u'[local]cli@BENU#Configure',
Run Code Online (Sandbox Code Playgroud)
我想从输出中消除[2J\x1b [1; 1H和 u].他们是垃圾.
我试图sesu
在Paramiko的帮助下从Python在Unix服务器中运行命令exec_command
。但是,当我运行此命令exec_command('sesu test')
时,
sh:sesu:未找到
当我运行简单ls
命令时,它会提供所需的输出。仅使用sesu
命令无法正常工作。
这是我的代码的样子:
import paramiko
host = host
username = username
password = password
port = port
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,username,password)
stdin,stdout,stderr=ssh.exec_command('sesu test')
stdin.write('Password')
stdin.flush()
outlines=stdout.readlines()
resp=''.join(outlines)
print(resp)
Run Code Online (Sandbox Code Playgroud) 我正在尝试运行使用本地 Linux 逻辑路径(例如 )的命令cat $test_dir/test.dat
,但逻辑路径$test_dir
(这是用户环境变量)无法通过ChannelExec
. 但是当我使用 Interactive 时ChannelShell
,我可以看到用户变量和命令在交互式会话上运行良好。我只能从“exec”会话中查看系统级环境变量。使用 JSch 库是否可以实现这一点,如果是,那么我该如何实现它,如果不是,我应该使用什么库来实现这一目标?
添加我的类代码如下:
private static final Logger logger = LogManager.getLogger(SecureShell.class);
private String uName;
private String pWord;
private String hName;
private int port;
private Session session = null;
private Channel channel = null;
/**Create an instance to start and stop the remote shell and execute commands
* remotely via java.
*
* @param uName
* host username
* @param pWord
* host password
* @param …
Run Code Online (Sandbox Code Playgroud)