Python
This page contains a very simple example for using scrapli to connect to a CLI device (telnet/ssh) as well as a NETCONF server -- there are many more examples here, so check those out too.
CLI
from scrapli import AuthOptions, Cli # (1)
def main(name):
cli = Cli( # (2)
definition_file_or_name="cisco_iosxe",
host="myrouter",
auth_options=AuthOptions(
username="scrapli",
password="verysecurepassword",
),
)
with cli as c: # (3)
result = c.send_input(input_="show version") # (4)
print(result.result) # (5)
if __name__ == "__main__":
main()
- Import the
CliandAuthOptionsobjects from scrapli, theCliobject is the class that represents our connection to some device via telnet/SSH, andAuthOptionsis just a class that holds various authentication related things. - Here we create our instance
cliof theCliclass -- the only required argument ishost-- as of course we must tell scrapli what host to connect to, but here we also provide a few other common things.definition_file_or_namerefers to either a platform name or a local YAML file defining a platform.auth_optionsis of course options related to authentication. - The
Cliimplements the context manager protocol, so you can use it like this in awithblock, or simply callopenand thenclosewhen you are done. - Once we've got an opened connection we can use methods like
send_inputto interact with the device. - All operations return a
Resultobject, in this case we are printing theresultfield of that object which of course contains the output from our input.
NETCONF
from scrapli import AuthOptions, Netconf # (1)
def main(name):
netconf = Netconf( # (2)
host="myrouter",
auth_options=AuthOptions(
username="scrapli",
password="verysecurepassword",
),
)
with cli as c: # (3)
result = c.get_config() # (4)
print(result.result) # (5)
if __name__ == "__main__":
main()
- Similar to the cli example we'll import
Netconfhere with ourAuthOptions. - When creating a
Netconfconnection we only need to provide the host and probably auth information. - Again, using the context manager is a good idea, but not strictly required.
- Once we've got an opened connection we can use methods like
send_inputto interact with the device. - All methods also return a
Result, though this time it is a NETCONF flavor result, but the look and feel is pretty much the same!