Created
June 29, 2015 05:50
-
-
Save tawateer/001f25dc8b3c34412f68 to your computer and use it in GitHub Desktop.
一个简单脚本,把第一列主机名改成第二列。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
# -*- coding: utf-8 -*- | |
import subprocess | |
text=""" | |
test0 test10 | |
test1 test11 | |
""" | |
def shell(cmd): | |
process = subprocess.Popen(args=cmd, stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, shell=True) | |
std_out, std_err = process.communicate() | |
return_code = process.poll() | |
return return_code, std_out, std_err | |
def _shell(cmd): | |
rc , so, se = shell(cmd) | |
if rc == 0: | |
return so.strip() | |
else: | |
return se | |
def func(orig, dest): | |
cmd = "ssh -oStrictHostKeyChecking=no %s 'sudo -i hostname %s' " % (orig, dest) | |
return _shell(cmd) | |
def main(): | |
_dict = dict() | |
for i in text.strip().splitlines(): | |
_dict[i.split()[0]] = i.split()[1] | |
for i in _dict: | |
print func(i, _dict[i]) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment