Paramiko ssh/sftp多个命令

2024-09-28 03:15:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python和paramiko(用我的基本初学者逻辑)实现一些自动化。你知道吗

下面的代码,我很高兴地说,工程。直到我加入命令rm-f测试跟踪.pcap通过sftp下载文件后删除该文件。你知道吗

定义登录凭据

host = input("Host: ")
user = input("User: ")
port = 22
password = getpass("Password: ")

打开ssh连接

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port=port, username=user, password=password)

执行命令run tcpdump

stdin, stdout, stderr = ssh.exec_command('timeout 10 tcpdump port 5060 -nnv -s 0 -w testtrace.pcap')
channel = stdout.channel
channel.recv_exit_status()
ssh.close()

打开sftp连接

transport = paramiko.Transport((host, port))
transport.connect(username=user, password=password)
sftp = paramiko.SFTPClient.from_transport(transport)

文件下载

filepath = '/root/testtrace.pcap'
localpath = 'C:\\Users\\******\\Desktop\\python\\testtrace.pcap'
sftp.get(filepath, localpath)

执行命令删除文件

stdin, stdout, stderr = ssh.exec_command('rm -f testtrace.pcap')
channel = stdout.channel
channel.recv_exit_status()
ssh.close()

Tags: 文件rmhostparamikoinputportstdoutchannel
1条回答
网友
1楼 · 发布于 2024-09-28 03:15:46

是的,我已经为这个问题挣扎了一两天,我在注册Stackoverflow!后的10分钟内就解决了!。你知道吗

执行命令run tcpdump

stdin, stdout, stderr = ssh.exec_command('timeout 10 tcpdump port 5060 -nnv -s 0 -w testtrace.pcap')
channel = stdout.channel
channel.recv_exit_status()

我关闭ssh连接太早了。通过移除ssh.close文件()从本节来看,它似乎已经解决了问题,现在可以正常工作了。你知道吗

相关问题 更多 >

    热门问题