Created
May 4, 2015 01:33
-
-
Save deeso/3304f7d99a58bf707444 to your computer and use it in GitHub Desktop.
set the hostname of a remote host
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, sys | |
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 set_hostname(client, password, cmds): | |
for cmd in cmds: | |
transport = client.get_transport() | |
session = transport.open_session() | |
session.set_combine_stderr(True) | |
session.get_pty() | |
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() | |
def perform_set_hostname(user, password, host, new_hostname): | |
echo_hostname = '''sudo sh -c "echo '%s' > /etc/hostname"'''%(new_hostname) | |
cmd_list = [ | |
echo_hostname, | |
] | |
client = ssh_to_target(host, username=user, password=password) | |
setup_nfs (client, password, cmd_list) | |
if __name__ == "__main__": | |
if len(sys.argv) < 5: | |
cmd = "%s <host_update> <user> <password> <new_hostname>"%sys.argv[0] | |
sys.exit(-1) | |
host = sys.argv[1] | |
user = sys.argv[2] | |
password = sys.argv[3] | |
new_hostname = sys.argv[4] | |
perform_set_hostname(user, password, host, new_hostname) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment