Following Python script will setup password less SSH between hosts in Solaris 11.
Hope the Python pkgs are already installed in Soalris 11.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
How to run the script:
# ./setupPasswordlessSSH.py --help
usage: setupPasswordlessSSH.py [-h] [-u USER] [-p PASSWORD] -c CLIENT
[CLIENT ...]
optional arguments:
-h, --help show this help message and exit
-u USER, --user USER Username[Optional] (Default : root)
-p PASSWORD, --password PASSWORD
Password[Optional] (Default : welcome1)
-c CLIENT [CLIENT ...], --client CLIENT [CLIENT ...]
Host or List of Hosts separated by space.
Ex: ./setupPasswordlessSSH.py -u [<USER>] -p [<your password>] -c Host1 Host2 .. HostN
Note: If you don't specify, default user it takes as 'root'. Also you can update the script and replace 'PASSWORD' in Help section with your actual password, then no need to use -p option for password.
setupPasswordlessSSH.py [Copy between the lines]
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/usr/bin/python
import os
from getpass import getpass
import argparse
##Check and Install Python Lib Pkgs required for this script to run
def inst_pkgs():
retcde=os.system('pkg list -q library/python/paramiko')
if retcde!=0:
print 'Packages "paramiko,paramiko-27" are not found'
print 'Its required for this script to work. Installing it now....\n'
os.system('pkg install paramiko')
inst_pkgs()
import paramiko
##Function
def deploy_key(key, server, username, password):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
client.connect(server, username=username, password=password)
retcode=os.system('ssh -l %s -oNumberOfPasswordPrompts=0 %s "echo hello" >/dev/null 2>&1' % (username, server))
if retcode==0:
print "Password-less SSH is already enabled for:", server
return
else:
client.connect(server, username=username, password=password)
client.exec_command('mkdir -p ~/.ssh/')
client.exec_command('echo "%s" >> ~/.ssh/authorized_keys' % key)
client.exec_command('chmod 644 ~/.ssh/authorized_keys')
client.exec_command('chmod 700 ~/.ssh/')
print "Password-less SSH has been setup with:", server
##Help options
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--user', default='root', help="Username[Optional] (Default : root)")
parser.add_argument('-p', '--password', default='PASSWORD', help="Password[Optional] (Default : PASSWORD)")
parser.add_argument('-c', '--client', nargs='+', type=str, default=[], required=True, help="Host or List of Hosts separated by space. Ex: ./<script> -c Host1 Host2 ")
args = parser.parse_args()
## Variables
username = args.user
hosts = args.client
password = args.password
keyfile=os.path.join(os.environ['HOME'] + "/.ssh/id_rsa")
print "User:", username
print "Hostnames:", hosts
print "Password:", password
if not os.path.isfile(keyfile):
print "RSA is missing, Generating Keypair", keyfile
os.system('ssh-keygen -t rsa -f %s -q -N "" ' %(keyfile))
os.system('touch ~/.ssh/known_hosts')
key = open(os.path.expanduser('~/.ssh/id_rsa.pub')).read()
for host in hosts:
deploy_key(key, host, username, password)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Hope the Python pkgs are already installed in Soalris 11.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
How to run the script:
# ./setupPasswordlessSSH.py --help
usage: setupPasswordlessSSH.py [-h] [-u USER] [-p PASSWORD] -c CLIENT
[CLIENT ...]
optional arguments:
-h, --help show this help message and exit
-u USER, --user USER Username[Optional] (Default : root)
-p PASSWORD, --password PASSWORD
Password[Optional] (Default : welcome1)
-c CLIENT [CLIENT ...], --client CLIENT [CLIENT ...]
Host or List of Hosts separated by space.
Ex: ./setupPasswordlessSSH.py -u [<USER>] -p [<your password>] -c Host1 Host2 .. HostN
Note: If you don't specify, default user it takes as 'root'. Also you can update the script and replace 'PASSWORD' in Help section with your actual password, then no need to use -p option for password.
setupPasswordlessSSH.py [Copy between the lines]
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/usr/bin/python
import os
from getpass import getpass
import argparse
##Check and Install Python Lib Pkgs required for this script to run
def inst_pkgs():
retcde=os.system('pkg list -q library/python/paramiko')
if retcde!=0:
print 'Packages "paramiko,paramiko-27" are not found'
print 'Its required for this script to work. Installing it now....\n'
os.system('pkg install paramiko')
inst_pkgs()
import paramiko
##Function
def deploy_key(key, server, username, password):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
client.connect(server, username=username, password=password)
retcode=os.system('ssh -l %s -oNumberOfPasswordPrompts=0 %s "echo hello" >/dev/null 2>&1' % (username, server))
if retcode==0:
print "Password-less SSH is already enabled for:", server
return
else:
client.connect(server, username=username, password=password)
client.exec_command('mkdir -p ~/.ssh/')
client.exec_command('echo "%s" >> ~/.ssh/authorized_keys' % key)
client.exec_command('chmod 644 ~/.ssh/authorized_keys')
client.exec_command('chmod 700 ~/.ssh/')
print "Password-less SSH has been setup with:", server
##Help options
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--user', default='root', help="Username[Optional] (Default : root)")
parser.add_argument('-p', '--password', default='PASSWORD', help="Password[Optional] (Default : PASSWORD)")
parser.add_argument('-c', '--client', nargs='+', type=str, default=[], required=True, help="Host or List of Hosts separated by space. Ex: ./<script> -c Host1 Host2 ")
args = parser.parse_args()
## Variables
username = args.user
hosts = args.client
password = args.password
keyfile=os.path.join(os.environ['HOME'] + "/.ssh/id_rsa")
print "User:", username
print "Hostnames:", hosts
print "Password:", password
if not os.path.isfile(keyfile):
print "RSA is missing, Generating Keypair", keyfile
os.system('ssh-keygen -t rsa -f %s -q -N "" ' %(keyfile))
os.system('touch ~/.ssh/known_hosts')
key = open(os.path.expanduser('~/.ssh/id_rsa.pub')).read()
for host in hosts:
deploy_key(key, host, username, password)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++