Junos Automation (Scripting)
Junos Automation (Scripting)

Trying to convert a slax script to pyhton

‎11-15-2019 12:10 PM

I found the login.slax in a github repository, trying to convert to python but having a hard time finding the user to logout.

 

Trying to conver the slax:

if ($response == "no") {
var $rpc = <command> "request system logout user " _ $user;
var $noresponse = jcs:invoke($rpc);

 

to python

with Device() as dev: #Call Device this way becuase it will automatically open and close the connection
jcs.output('..')
jcs.output('..')
jcs.output('Logging Out')
res = dev.rpc.cli('request system logout user ' + username, format='text', normalize=False)

 

I am hoping someone knows how to fund the logged in user. We are suing Cisco ISE and TACACS to login which gets mapped to a local user on the switch so cant log that user out.

 

I am hoping to logout the user that attempted to login using their username they entered: JDI

show system users
fpc0:
--------------------------------------------------------------------------
6:48AM up 1 day, 19:54, 1 users, load averages: 0.15, 0.23, 0.24
USER TTY FROM LOGIN@ IDLE WHAT
JDIZZLE pts/0 10.192.175.124 6:14AM - -cl

3 REPLIES 3
Highlighted
Junos Automation (Scripting)

Re: Trying to convert a slax script to pyhton

‎11-17-2019 03:38 AM

What user names show up when you run

show system users

 

With RADIUS this does still show as the actual user name even when mapped for permissions to a local user.  If that is true for the Cisco auth you should be able to use the script as designed with user name.

 

Steve Puluka BSEET - Juniper Ambassador
IP Architect - DQE Communications Pittsburgh, PA (Metro Ethernet & ISP)
http://puluka.com/home
Highlighted
Junos Automation (Scripting)

Re: Trying to convert a slax script to pyhton

‎11-19-2019 03:26 PM

The logged in users, my question is how to do I get the current logged in user. I have been looking at the juniper PyEZ documentation but cannot find anything

Highlighted
Junos Automation (Scripting)
Solution
Accepted by topic author jamannmtgov
‎12-19-2019 09:29 AM

Re: Trying to convert a slax script to pyhton

‎12-19-2019 09:28 AM

So here is what I ended up doing

 

import os # Used to retrieve the username
import pwd # Used to retrieve the username

 

# finding the username and terminal session to disconnect. Cant do just user in case someone is trying to
# brute force login with same user somone uses becuase the alternative would be to disconnect all sessions
# for the user
_tty = os.environ.get("CLI_TTY") # retrieving the current terminal session
_terminal = re.findall(r"pts/[0-9]*",_tty)
_username = pwd.getpwuid( os.getuid() )[ 0 ] #retrieving the current username

 

try:
    with Device() as dev: #Call Device this way becuase it will automatically open and close the connection
    jcs.output('..')
    jcs.output('..')
    jcs.output('Logging Out')
    res = dev.rpc.cli(
        "request system logout user " + _username + " terminal " + _terminal[0],
        format="text",
        normalize=False,

    )

except:
    print("Unexpected error:", sys.exc_info()[0])
    # Issue with opneing Device or the username so logging out the current terminal session
    res = dev.rpc.cli(
        "request system logout terminal " + _terminal[0],
        format="text",
        normalize=False,
    )

 

Feedback