source命令不生效

时间:2014-03-16 04:40:44

标签: python linux paramiko

我有一组机器,我编写了一个Python脚本来更改主机名;代码如下。

让我感到困惑的是, source命令仅在某些计算机上生效,而不是全部。我重复几次后,所有主机名生效[BTW:我可以更改/etc/hostname中的主机名,但主机名服务不会生效。]

import paramiko
import time
import threading
import os

cmd0 = "sudo source /root/.bashrc"
cmd1 = "sudo service hostname stop"
cmd2 = "sudo service hostname start"

def executeit(ip):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    ssh.connect(ip, 22, "root")
    ssh.exec_command(cmd0)
    stdin, stdout, stderr = ssh.exec_command(cmd1)
    out = stdout.readlines()
    for o in out:
            print(o)
    time.sleep(1)
    ssh.exec_command(cmd2) 
    ssh.close()

    print("=======")

def main():
        fr = open("info.txt", "r")
        contents = fr.read().splitlines()
        fr.close()
        for ip in contents:
                t = threading.Thread(target=executeit, args=(ip,))
                t.start()

main()

0 个答案:

没有答案