Jump to content

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
Link to comment

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()

Link to comment

 

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...

Link to comment
  • 2 weeks later...

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
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...