Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从Paramiko SSH打印输出时如何跳过行_Python_Ssh_Paramiko - Fatal编程技术网

Python 从Paramiko SSH打印输出时如何跳过行

Python 从Paramiko SSH打印输出时如何跳过行,python,ssh,paramiko,Python,Ssh,Paramiko,因此,我构建了一个程序,使用tail-f打印我的ubuntu服务器的登录日志。 该程序使用Paramiko通过ssh进行连接,并运行命令跟踪日志。 该程序可以工作,但它从服务器打印出motd,这是不必要的 我尝试过使用itertools进行拼接。 已尝试使用next()。 还是不行 这是我的密码: 导入yaml、paramiko、getpass、回溯、时间、itertools 从paramiko_期望导入SSHClientInteraction 以open(“config.yaml”、“r”)作

因此,我构建了一个程序,使用tail-f打印我的ubuntu服务器的登录日志。 该程序使用Paramiko通过ssh进行连接,并运行命令跟踪日志。 该程序可以工作,但它从服务器打印出motd,这是不必要的

我尝试过使用itertools进行拼接。 已尝试使用next()。 还是不行

这是我的密码:

导入yaml、paramiko、getpass、回溯、时间、itertools
从paramiko_期望导入SSHClientInteraction
以open(“config.yaml”、“r”)作为yamlfile:
cfg=yaml.load(yamlfile,Loader=yaml.FullLoader)
def main():
command=“sudotail-f/var/log/auth.log”
尝试:
ssh=paramiko.SSHClient()
ssh.set_缺少_主机_密钥_策略(paramiko.AutoAddPolicy())
server_pw=getpass.getpass(“在%s上输入帐户%s的密码:%”(cfg['ssh\u config']['username'],cfg['ssh\u config']['host']))
sudo_pw=getpass.getpass(“在%s上输入%s的sudo密码:%”(cfg['ssh_config']['username'],cfg['ssh_config']['host']))
连接(主机名=cfg['ssh\u config']['host'],用户名=cfg['ssh\u config']['username'],端口=cfg['ssh\u config']['port'],密码=server\u pw)
interact=SSHClientInteraction(ssh,超时=10,显示=False)
交互发送(命令)
交互发送(sudo_pw+“\n”)
以open(interact.tail(line_prefix=cfg['ssh\u config']['servername']+':')作为尾部:
对于itertools.islice中的行(尾部,17,无):
打印(行)
除键盘中断外:
打印('检测到Ctrl+C中断,正在停止尾部')
除例外情况外:
traceback.print_exc()
最后:
尝试:
ssh.close()
除:
通过
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()

您将获得MOTD,因为您正在打开一个交互式shell会话。我认为你不需要,恰恰相反

改用:

相关问题:



强制性警告:不要使用
AutoAddPolicy
–这样做会失去保护。有关正确的解决方案,请参阅。

谢谢,现在可以使用了!
stdin, stdout, stderr = ssh.exec_command(command, get_pty=True)

stdin.write(sudo_pw + "\n")
stdin.flush()

for line in iter(stdout.readline, ""):
    print(line, end="")