Create a Post
cancel
Showing results for 
Search instead for 
Did you mean: 
Alec_Ritter
Employee Alumnus
Employee Alumnus
Jump to solution

API returning "Wrong session ID"

"Wrong session id [3YtytO9PKqAC0NpHMKxY2TyZtm8Hb_gC9KL0o58XtNI]. Session may be expired. Please check session id and resend the request."

This error started showing up on a new box after making an additional user for API purposes. Here's the relevant code:

 

import os
import sys
import json
import getpass
from glob import glob
from cpapi import APIClient, APIClientArgs

#rules, ALL network objects

def main():
    apiServer = input("IP:")
    username = input("Username: ")

    if sys.stdin.isatty():
        password = getpass.getpass("Password: ")
    else:
        print("Module getpass failed, you can still enter your password but it will be shown.")
        password = input("Password: ")

    apiVer = "1.1"
    posApi = input("API Version? Please format as #.# If you don't know API version leave blank: ")
    #if anything was entered set the API version to that, otherwise leave it default
    if len(posApi) > 1:
        apiVer = posApi

    print(str(apiVer))
    
    #set the client variables for login
    client_args = APIClientArgs(server=apiServer, api_version=apiVer)
    with APIClient(client_args) as client:

    #checks fingerprint, if not there gives user a chance to accept. if user declines exits.
    if client.check_fingerprint() is False:
        print("Could not get the server's fingerprint.")
        exit(1)

    #logs into the server using given credentials
    loginRes = client.login(username, password)

    #if login fails print message, exit.
    if loginRes.success is False:
        print("Login failed: {}".format(loginRes.error_message))
        exit(1)

    cwd = os.getcwd()

    client_args = APIClientArgs(server=apiServer, api_version=apiVer)
    with APIClient(client_args) as client:

 

As far as I understand it the client_args should be maintaining the SID, meaning on login that should get updated. It doesn't look like that's happening though given the message. The weirdest part is that it was working fine* earlier, and I haven't made any changes. I've attached a screenshot of the output. I should also note I've checked the session list in smartconsole and it's bare, and the logs show the API stays logged in for about 2 seconds.

Edit: I've tried running it from another terminal window (PowerShell) to no avail, so I don't think CMD is just hanging onto errant variables. I've also tried running a similar set of commands through PostMan on the same server with the same credentials and it was successful.

Edit2: I just tried it on a separate box - no dice.

Edit3: I tried another script I wrote, using my same framework, and it executed just fine. Not sure where the hangup is...

0 Kudos
1 Solution

Accepted Solutions
Alec_Ritter
Employee Alumnus
Employee Alumnus

I figured it out... My arguments were not inside the "with APIClient(client_args) as client:" block, so those values were getting thrown out. Check your indentation, folks!

View solution in original post

0 Kudos
3 Replies
PhoneBoy
Admin
Admin
Not clear which framework you're using or that you're using the most recent version of it.
0 Kudos
Alec_Ritter
Employee Alumnus
Employee Alumnus

Hi @PhoneBoy.

Currently I'm using Python 3.7 with CPAPI 1.0.3, 1.0.3 is the newest version of our API afaik. When I mentioned my framework I just meant the outline I made for my CP API scripts. I'll post my current script below for posterity (I've ripped out dynamic variables, trying to par down unnecessary stuff to find the issue). 

 

 

import os
import sys
import json
import getpass
from glob import glob
from cpapi import APIClient, APIClientArgs

def main():
    apiServer = input("IP:")
    username = input("Username: ")
    
    if sys.stdin.isatty():
        password = getpass.getpass("Password: ")
    else:
        print("Getpass failed, you can still enter your password but it will be shown.")
        password = input("Password: ")

    client_args = APIClientArgs(server=apiServer, api_version="1.3")

    with APIClient(client_args) as client:
        if client.check_fingerprint() is False:
            print("Could not get the server's fingerprint.")
            exit(1)
        print("\n\tVerifying the type of server {}...".format(apiServer))
        if loginRes.success is False:
            print("Login failed: {}".format(loginRes.error_message))
            exit(1)
        session_res = client.api_call("show-session", {}, loginRes.data["sid"])
        sid=args.session_id
        print(sid)
    cwd = os.getcwd()
    os.chdir(os.path.join(cwd, "exportedData"))
    for file in glob('*.json'):
        print("Current file name is: " + str(file))
        with open (file, "r") as jsonFile:
            json_data=open(file)
            jdata = json.load(json_data)
            commandName = str(file).split("Export")[0]
            addHostResponse = client.api_call('add-host', {'name':'host1','ip-address':'192.168.0.0'})
            print(addHostResponse)
            publish_res = client.api_call("publish", {})

main()

 

 

 

0 Kudos
Alec_Ritter
Employee Alumnus
Employee Alumnus

I figured it out... My arguments were not inside the "with APIClient(client_args) as client:" block, so those values were getting thrown out. Check your indentation, folks!

0 Kudos

Leaderboard

Epsum factorial non deposit quid pro quo hic escorol.

Upcoming Events

    CheckMates Events