Created
August 29, 2015 03:59
-
-
Save deeso/1937a07a9f3fea55d21c to your computer and use it in GitHub Desktop.
quick script to login and rename the hosts with ssh, username, and password
This file contains 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
import paramiko, re, time | |
script_target = '/srv/nfs/cortana/logs/cmd/modify_host.py' | |
script_data= '''data = open('/etc/hosts').read() | |
name = open('/etc/hostname').read().strip() | |
new_data = data.replace('python-workx64-replaceme', name) | |
open('/etc/hosts', 'w').write(new_data)''' | |
open(script_target, 'w').write(script_data) | |
def ssh_to_target (hostname, username, password): | |
client = paramiko.SSHClient() | |
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
client.connect(hostname, username=username, password=password) | |
return client | |
def exec_cmds (client, password, cmds): | |
for cmd in cmds: | |
transport = client.get_transport() | |
session = transport.open_session() | |
session.set_combine_stderr(True) | |
session.get_pty() | |
print "Executing cmd: %s"%cmd | |
session.exec_command(cmd) | |
stdin = session.makefile('wb', -1) | |
stdout = session.makefile('rb', -1) | |
#you have to check if you really need to send password here | |
#stdin.write(password +'\n') | |
#stdin.flush() | |
for line in stdout.read().splitlines(): | |
print 'host: %s: %s' % (host, line) | |
user = "java" | |
password = "java" | |
for i in xrange(0, 20): | |
host = "10.16.120.1{:02d}".format(i) | |
cmd = '''sudo sh -c "echo 'python-workx64-{:02d}' > /etc/hostname"'''.format(i) | |
cmd2 = 'sudo python {}'.format(script_target) | |
client = ssh_to_target(host, username=user, password=password) | |
exec_cmds(client, password, [cmd, cmd2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment