Skip to content

Instantly share code, notes, and snippets.

@tawateer
Created June 29, 2015 05:50
Show Gist options
  • Save tawateer/001f25dc8b3c34412f68 to your computer and use it in GitHub Desktop.
Save tawateer/001f25dc8b3c34412f68 to your computer and use it in GitHub Desktop.
一个简单脚本,把第一列主机名改成第二列。
#!/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