我有这个代码,我正在使用子进程模块执行ssh并运行一些命令并获得响应。
command = "ssh -n " +str(server_name) + " sudo /etc/init.d/postfix status && sudo /etc/init.d/ssh status && sudo /etc/init.d/lldpd status"
result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
response, err = result.communicate()
print response
它返回
sudo: /etc/init.d/lldpd: command not found
但如果我格式化这样的命令
sudo /etc/init.d/lldpd status && sudo /etc/init.d/postfix status && sudo /etc/init.d/ssh status
它完美无缺。如果我将lldpd放入命令中的第一个位置以外,它将无法工作。 我可以按任何顺序在服务器上运行该命令,并且它正在工作。
编辑: - 这个命令
sudo /etc/init.d/postfix status && sudo /etc/init.d/ssh status && sudo /etc/init.d/lldpd status
我有这个输出
sudo: /etc/init.d/lldpd: command not found
postfix is running.
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled)
Active: active (running) since Sun 2017-05-28 14:13:49 IST; 19h ago
Process: 20365 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
Main PID: 651 (sshd)
CGroup: /system.slice/ssh.service
└─651 /usr/sbin/sshd -D
May 29 09:36:33 rohit systemd[1]: Reloading OpenBSD Secure Shell server.
May 29 09:36:33 rohit sshd[651]: Received SIGHUP; restarting.
May 29 09:36:33 rohit systemd[1]: Reloaded OpenBSD Secure Shell server.
May 29 09:36:33 rohit sshd[651]: Server listening on 0.0.0.0 port 22.
May 29 09:36:33 rohit sshd[651]: Server listening on :: port 22.
May 29 09:36:33 rohit systemd[1]: Reloading OpenBSD Secure Shell server.
May 29 09:36:33 rohit sshd[651]: Received SIGHUP; restarting.
May 29 09:36:33 rohit systemd[1]: Reloaded OpenBSD Secure Shell server.
May 29 09:36:33 rohit sshd[651]: Server listening on 0.0.0.0 port 22.
May 29 09:36:33 rohit sshd[651]: Server listening on :: port 22.
用于此命令
sudo /etc/init.d/lldpd status && sudo /etc/init.d/postfix status && sudo /etc/init.d/ssh status
我有这个输出
lldpd is running.
● postfix.service - LSB: Postfix Mail Transport Agent
Loaded: loaded (/etc/init.d/postfix)
Drop-In: /run/systemd/generator/postfix.service.d
└─50-postfix-$mail-transport-agent.conf
Active: active (running) since Sun 2017-05-28 14:14:10 IST; 19h ago
Process: 20381 ExecReload=/etc/init.d/postfix reload (code=exited, status=0/SUCCESS)
Process: 1350 ExecStart=/etc/init.d/postfix start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/postfix.service
├─ 1474 /usr/lib/postfix/master
├─20395 pickup -l -t unix -u -c
└─20396 qmgr -l -t unix -u
May 29 08:33:43 rohit systemd[1]: Reloaded LSB: Postfix Mail Transport Agent.
May 29 08:33:43 rohit postfix[18121]: Reloading Postfix configuration...done.
May 29 09:36:33 rohit systemd[1]: Reloading LSB: Postfix Mail Transport Agent.
May 29 09:36:33 rohit postfix/master[1474]: reload -- version 2.11.3, configuration /etc/postfix
May 29 09:36:33 rohit postfix[20292]: Reloading Postfix configuration...done.
May 29 09:36:33 rohit systemd[1]: Reloaded LSB: Postfix Mail Transport Agent.
May 29 09:36:33 rohit systemd[1]: Reloading LSB: Postfix Mail Transport Agent.
May 29 09:36:33 rohit postfix/master[1474]: reload -- version 2.11.3, configuration /etc/postfix
May 29 09:36:33 rohit postfix[20381]: Reloading Postfix configuration...done.
May 29 09:36:33 rohit systemd[1]: Reloaded LSB: Postfix Mail Transport Agent.
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled)
Active: active (running) since Sun 2017-05-28 14:13:49 IST; 19h ago
Process: 20365 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
Main PID: 651 (sshd)
CGroup: /system.slice/ssh.service
└─651 /usr/sbin/sshd -D
May 29 09:36:33 rohit systemd[1]: Reloading OpenBSD Secure Shell server.
May 29 09:36:33 rohit sshd[651]: Received SIGHUP; restarting.
May 29 09:36:33 rohit systemd[1]: Reloaded OpenBSD Secure Shell server.
May 29 09:36:33 rohit sshd[651]: Server listening on 0.0.0.0 port 22.
May 29 09:36:33 rohit sshd[651]: Server listening on :: port 22.
May 29 09:36:33 rohit systemd[1]: Reloading OpenBSD Secure Shell server.
May 29 09:36:33 rohit sshd[651]: Received SIGHUP; restarting.
May 29 09:36:33 rohit systemd[1]: Reloaded OpenBSD Secure Shell server.
May 29 09:36:33 rohit sshd[651]: Server listening on 0.0.0.0 port 22.
May 29 09:36:33 rohit sshd[651]: Server listening on :: port 22.