Created
May 4, 2015 01:28
-
-
Save deeso/a89fbdbc0fb42d6203f4 to your computer and use it in GitHub Desktop.
Script that can be used to set-up nfs on a remote host using paramiko
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 setup_nfs (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_nfs_setup(user, password, host, nfs_host, remote_mount, local_mount): | |
install_nfs = "sudo -S apt-get install -y nfs-common" | |
mklocal_mount = "sudo -S mkdir -p %s"%local_mount | |
chmod_mount = "sudo -S chmod -R a+rwx %s"%local_mount | |
echo_mount = '''sudo sh -c "echo '%s:%s %s nfs rsize=8192,wsize=8192,timeo=14,intr' >> /etc/fstab"'''%(nfs_host, remote_mount, local_mount) | |
mount_all = "sudo -S mount -a" | |
cmd_list = [ | |
install_nfs, | |
mklocal_mount, | |
chmod_mount, | |
echo_mount, | |
mount_all, | |
] | |
client = ssh_to_target(host, username=user, password=password) | |
setup_nfs (client, password, cmd_list) | |
if __name__ == "__main__": | |
if len(sys.argv) < 7: | |
cmd = "%s <host_update> <user> <password> <nfshost> <local_mount> <remote_mount>"%sys.argv[0] | |
sys.exit(-1) | |
host = sys.argv[1] | |
user = sys.argv[2] | |
password = sys.argv[3] | |
nfshost = sys.argv[4] | |
local_mount = sys.argv[5] | |
remote_mount = sys.argv[6] | |
perform_nfs_setup(user, password, host, nfshost, remote_mount, local_mount) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment