Jump to content
oorahduc

Script Filter takes {query}, builds CLI string, runs in terminal

Recommended Posts

The title basically says what I'm trying to do. 

 

I have a python script using deanishe's alfred-workflow library that I'm using to build an ssh CLI string.

 

Workflow is set up like this:

 

workflow.png?_subject_uid=377869981&w=AA

 

The Script Filter runs "python jump.py {query}", or attempts to.

 

jump.py looks like the following, and is meant to take args such as "9999,192.168.100.100,50.57.203.223", and buildout 'query', which should be passed to the Terminal Command action of "ssh {query}

def main(wf):
    if len(wf.args):
        query = wf.args[0]
    else:
        query = None
    print query
    log.debug(query)
    arglist = query.split(',')
    randport1 = random.randint(5000,7000)
    randport2 = random.randint(5000,7000)
    port = arglist[0]
    srv1 = arglist[1]
    srv2 = arglist[2]
    query = "-A -t -L 9800:localhost:{0} -L {1}:localhost:{2} user@{3} ssh -L {4}:localhost:9800 -L {5}:localhost:80 user@{6}".format(randport1,port,randport2,srv1,randport1,randport2,srv2)
    wf.add_item(
        title =  "Jump from %s to %s" % (srv1,srv2),
        arg = "-A -t -L 9800:localhost:{0} -L {1}:localhost:{2} user@{3} ssh -L {4}:localhost:9800 -L {5}:localhost:80 user@{6}".format(randport1,port,randport2,srv1,randport1,randport2,srv2),
        valid = True
        )
    wf.send_feedback()

# Mainloop
if __name__ == u"__main__":
    wf = Workflow()
    log = wf.logger
    sys.exit(wf.run(main))

 

 

This is the debug output I get... I'm not terribly familiar with XML, so I don't know what's going on here. The confusing thing is I have another script that is identical in the way it takes the args passed through Alfred, splits them into a list, and then assigns it to key value pairs in a dict. It works fine..

<?xml version="1.0" encoding="utf-8"?>
<items><item valid="no"><title>Error in workflow 'com.alfred.custconn'</title><subtitle>list index out of range</subtitle><icon>/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns</icon></item></items>
[STDERR: alfred.workflow.input.scriptfilter] 15:18:37 jump.py:28 DEBUG     9999,11.48.54.65,11.11.11.1
[INFO: alfred.workflow.input.scriptfilter] 9999,192.168.100.100,50.57.203.223
[ERROR: alfred.workflow.input.scriptfilter] XML Parse Error 'The operation couldn’t be completed. (NSXMLParserErrorDomain error 4.)'. Row (null), Col (null): 'Document is empty' in XML:
9999,192.168.100.100,50.57.203.223
<?xml version="1.0" encoding="utf-8"?>
<items><item valid="yes"><title>Jump from 192.168.100.100 to 50.57.203.223</title><subtitle /><arg>-A -t -L 9800:localhost:6678 -L  9999:localhost:5025 user@192.168.100.100 ssh -L 6678:localhost:9800 -L 5025:localhost:80 user@50.57.203.223</arg></item></items>
Edited by oorahduc

Share this post


Link to post

I think its complaining about the an index out of range. You need to be sure your "arglist" list really has three entries when you split it.

 

I've tidied up your main function (note, haven't run this):

def main(wf):
	try:
		if not len(wf.args):
			raise Exception

		arglist = wf.args[0].split(',')
		
		randport1 = random.randint(5000,7000)
		
		randport2 = random.randint(5000,7000)
		
		port = arglist[0]
		
		srv1 = arglist[1]
		
		srv2 = arglist[2]

		query = "-A -t -L 9800:localhost:{0} -L {1}:localhost:{2} user@{3} ssh -L {4}:localhost:9800 -L {5}:localhost:80 user@{6}".format(randport1,port,randport2,srv1,randport1,randport2,srv2)

		wf.add_item(
			title =  "Jump from %s to %s" % (srv1,srv2),
			arg = query,
			valid = True,
			icon = ICON_WEB)
	except:
		wf.add_item(title =  "Error",
					subtitle = "Malformed Input!",
					icon = ICON_ERROR)
	
	wf.send_feedback()

Share this post


Link to post

 

I think its complaining about the an index out of range. You need to be sure your "arglist" list really has three entries when you split it.

I incorporated your modifications to main() - tidying up indeed. Unfortunately that didn't help the XML error. I am validating that arglist is getting split, though. In my code I assigned separate vars to the indexes of arglist to be certain it was splitting here:

        arglist = query.split(',')
        port = arglist[0]
        srv1 = arglist[1]
        srv2 = arglist[2]

and my wf.add_item is calling those vars here:

        wf.add_item(
            title =  "Jump from %s to %s" % (srv1,srv2),
            arg = "-A -t -L 9800:localhost:{0} -L {1}:localhost:{2} bvi@{3} ssh -L {4}:localhost:9800 -L {5}:localhost:80 bvi@{6}".format(randport1,port,randport2,srv1,randport1,randport2,srv2),
            valid = True
            )

and I am seeing it used those vars in the arg it creates in the debug window here:

[ERROR: alfred.workflow.input.scriptfilter] XML Parse Error 'The operation couldn’t be completed. (NSXMLParserErrorDomain error 4.)'. Row (null), Col (null): 'Document is empty' in XML:
9999,myserverip1,myserverip2
<?xml version="1.0" encoding="utf-8"?>
<items><item valid="yes"><title>Jump from 11 to 11</title><subtitle /><arg>-A -t -L 9800:localhost:5923 -L  9999:localhost:5318 bvi@myserverip1 ssh -L 5923:localhost:9800 -L 5318:localhost:80 bvi@myserverip2</arg></item></items>

Still at a loss here...

Share this post


Link to post

Resolved....Something was fishy somewhere. I copied over the other script that I mentioned was identical in the query arg handling, remodified it to be identical to the broken code above, and everything is gravy.

 

Strange happenings.

Share this post


Link to post

The reason you were getting invalid XML is because of the print statement. By default print sends its output to STDOUT, which is where Alfred is reading the XML from, so the output from print gets mixed in with the XML, making it invalid.

You need to explicitly print to STDERR (print('blah...', file=sys.stderr)), or if you're using Alfred-Workflow, you might as well just do wf.logger.debug('blah').

Edited by deanishe

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...