Skip to content

Instantly share code, notes, and snippets.

@ysqi
Last active November 1, 2018 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ysqi/3e0f5d455a2d4aa1e8eee1accf9f4561 to your computer and use it in GitHub Desktop.
Save ysqi/3e0f5d455a2d4aa1e8eee1accf9f4561 to your computer and use it in GitHub Desktop.
batch set Disable/Enable Root SSH login with fabric tool (sshd_config PermitRootLogin)
{
  "172.123.4.6":{"user":"abc"},
  "172.123.5.6":{"user":"abc"},
  "172.123.1.6":{"user":"abc"},
}
from invoke import Responder
from fabric import Connection
import json

# load hosts
hosts=json.load(open("nodeinfo.json",'r'), encoding='utf-8')

for key,node in hosts.items(): 
    ip = key.strip()  
    user = 
    host ="{0}@{1}".format(node['user'],key) #host forma: user@host:port
    c = Connection(host,connect_kwargs={'password':'userPassword'}) 

    # auto fill password
    sudopass = Responder(
    pattern=r'Password:', 
    response='replace with your password\n',
    ) 
    # enable
    result=c.run('su -l root -s /bin/sh -c \'sed -i "s/PermitRootLogin no/PermitRootLogin yes/g" /etc/ssh/sshd_config | service ssh restart \'',pty=True, hide='stderr', watchers=[sudopass])
    # disable
    # result=c.run('su -l root -s /bin/sh -c \'sed -i "s/PermitRootLogin yes/PermitRootLogin no/g" /etc/ssh/sshd_config | service ssh restart \'',pty=True, hide='stderr', watchers=[sudopass])
    print(host, result.ok)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment