Tuesday 10 September 2019

Python Script To Connect to remote server and run rails tasks

We need to import paramiko python2 package and it's dependent packages like bcrypt and PyNaCI

Here is the python2 script to connect ssh

import paramiko
ssh_client=paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='xxxx',username='xxxx',password='xxxx'', allow_agent=False,look_for_keys=False)
ssh_client.invoke_shell()
stdin,stdout,stderr=ssh_client.exec_command("export PATH=/home/xxx/.rbenv/shims:/home/xxx/.rbenv/bin:/home/agwebadmin/bin:/home/xxx/.local/bin:/home/xxx/.rbenv/shims:/home/xxx/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin; export RAILS_ENV=production; ruby -v; cd current/HotDesk; rake sunspot:solr:reindex")
print stdout.read()
print stderr.read()
ssh_client.close()

To get ruby path we need to run command $which ruby

No comments:

Post a Comment