Reputation: 55
I have a Python Paramiko script that sends commands to remote hosts on out intranet. There are times when I would like to send the shutdown command to several hosts at once. The issue is that the shutdown command simply sits and waits unless you background it. I have tried using the ampersand (bare as above, or escaped: \&). Here is a small test program. My os is RHEL Linux 5.9 (Python 2.4.3). Note that the sudoers disables requiretty for some users.
#!/usr/bin/python
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("<hostname>",username="<my username>", password="<mypassword>")
stdin, stdout, stderr = ssh.exec_command("sudo /sbin/shutdown -h 17:00 \&")
stdin.write('\n')stdin.flush()
data = stdout.read().splitlines()
for line in data:
print line
Upvotes: 2
Views: 1512
Reputation: 55
I have solved the issue using the shutdown command as it is intended. First do not escape the ampersand (\&). Since the shutdown command does not return anything to stdout, I just eliminate those lines dealing with the output. The reason for wanting to use shutdown with a time is for user notification.
#!/usr/bin/python
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("<hostname>",username="<my username>", password="<mypassword>")
stdin, stdout, stderr = ssh.exec_command("sudo /sbin/shutdown -h 17:00 &")]
ssh.close()
Upvotes: 1