子进程python多个命令
要打开ssh会话,运行命令并在进程运行时实时获取输出(此基础将涉及在远程服务器上运行其他命令)子进程python多个命令,python,ssh,subprocess,Python,Ssh,Subprocess,要打开ssh会话,运行命令并在进程运行时实时获取输出(此基础将涉及在远程服务器上运行其他命令) 从子流程导入Popen,管道 用Popen(['ssh', ],shell=True, 标准偏差=管道,标准偏差=管道,标准偏差=管道, universal_newlines=True)作为ssh: output1=ssh.stdin.write('ls-l') output2=ssh.stdin.write('mkdir test') status=ssh.poll() 打印(输出1) 打印(输出2
从子流程导入Popen,管道
用Popen(['ssh',
],shell=True,
标准偏差=管道,标准偏差=管道,标准偏差=管道,
universal_newlines=True)作为ssh:
output1=ssh.stdin.write('ls-l')
output2=ssh.stdin.write('mkdir test')
status=ssh.poll()
打印(输出1)
打印(输出2)
到目前为止,这就是我所拥有的,使用ssh.communicate[]给出了正确的输出,但在第一个命令后关闭了子任务,有什么想法吗?对我有用吗
from fabric2 import Connection
with Connection('<host>') as c:
print(CGREEN +'connected succsfully!' + CEND)
#gather user info
user = io.StringIO
user = c.run("whoami", hide=True)
print(f'user found:{user.stdout} ')
#fetching files
c.run(<command>, pty=True)
从fabric2导入连接
连接(“”)为c:
打印(CGREEN+“已成功连接!”+CEND)
#收集用户信息
user=io.StringIO
user=c.run(“whoami”,hide=True)
打印(f'user found:{user.stdout}')
#获取文件
c、 运行(,pty=True)
你确定stdin应该是一个管道吗?在这里使用shell=True
是错误的,但正好在Windows上工作。不过,您确实希望尽可能避免使用shell=True
。看见在本例中,请尝试Popen('ssh',server],…)
,但正如您所发现的,您确实需要一个更高级别的库,如Paramiko,或者期望与远程服务器上的交互进程进行对话。
from fabric2 import Connection
with Connection('<host>') as c:
print(CGREEN +'connected succsfully!' + CEND)
#gather user info
user = io.StringIO
user = c.run("whoami", hide=True)
print(f'user found:{user.stdout} ')
#fetching files
c.run(<command>, pty=True)