Create a bot to operate on a server using SSH#
This example shows how to set-up a SSH bot to ease operations with a server.
Store your credentials#
codebots
uses .json files to manage credentials. You can create one
using the CLI (check the related example) or create a json file yourself in a
secret location using the template below:
{
"hostname": "_____________",
"username": "_____________",
"password": "_____________",
"pvtkey": "_____________"
}
Note
about keypairing
You can choose to connect either using username and password (less secure) or using a SSH key. If you opt for the first approach, than leave blank the pvtkey field (“”).
Note
ssh keys generation
If you don’t have a set of SSH key pairs or you don’t know how to create one, ask the bot! ;)
>>> sshbot genkeys --ssh_folder "my_folder"
After running the code there will be a set of private and public keys in the give folder that you can use to set-up your connection.
Note
password key
the password key in the json file is either the password used to login to the server, or the password to decrypt the private SSH key (leave blank if not encrypted).
In action#
this is an example implementation:
from codebots.bots import sshBot
# execute command on the server
bot = sshBot('username@host')
bot.execute_cmds(commands=['ls']) # note: the imput is a list!
# get folder content
remotefolderpath = '/home/server/Documents/test/'
localfolderpath = '/home/client/Documents/'
bot.get_folder_from_server(remotefolderpath, localfolderpath)