Tagged: unix

Linux: Connect to open and closed networks from the command line with a single command.

You can easily connect to an open network though

iwconfig wlan0 essid "THE NETWORK NAME"

Closed networks work with wpa_supplicant. You normally send a configuration file with that but you can simulate the file with a named pipe:

wpa_supplicant -iwlan0 -c <(echo -e 'network={\n ssid="THE NETWORK NAME" \n psk="THE PASSWORD" \n}') -B

wlan0 is your network interface name. -B means go into the background. The \ns in the configuration file is needed sadly and so we pass -e to echo to interpret them.

iwconfig should show your new connection but prehaps only after a second.

After this dhclient wlan0 will get you a dhcp address and you’ll be ready to go. You may want to kill any prior instances of dhclient.

unix wifi

Upload an image to an imgur album from the command line with curl

First make sure you have a client id through registering at https://api.imgur.com/oauth2/addclient

Then make a command to create an album:

curl --request POST   --url https://api.imgur.com/3/album   
--header 'authorization: Client-ID YOUR_CLIENT_ID'

This will return an anonymous public album. Note the ‘deletehash’ id in the below:


The empty album is available at https://imgur.com/a/SOME_ID

Now let’s upload an image to that album. Because it’s public and anonymous we need to use the deletehash as the album ID:

curl --request POST   --url https://api.imgur.com/3/image   --header 'authorization: Client-ID YOUR_CLIENT_ID'   
--header 'content-type: multipart/form-data;' -F "album=THE_DELETE_HASH" -F "image=@/PATH/TO/THE/IMAGE.png"

The json returned should say everything is okay. Upload a couple of images to that album and you should be able to see them at https://imgur.com/a/SOME_ID.

You can get a json response of all the images in that album through `curl –request GET –url https://api.imgur.com/3/album/SOME_ID –header ‘authorization: Client-ID YOUR_CLIENT_ID’. It will include the array of images:

"images":[{"id":"ANOTHER_ID",... link":"https:\/\/i.imgur.com\/ANOTHER_ID.png"},...]

The link property is the direct link to the image without all the imgur.com html around it.

unix imgur curl

Upload to an image to imgur from the command line with curl

First, go to https://api.imgur.com/oauth2/addclient and register as a client. Create a new client and you’ll eventually get a Client ID and a Client secret.

Now take the client ID, and use this curl command:

curl --request POST --url https://api.imgur.com/3/image --header 'authorization: Client-ID YOUR_CLIENTID_HERE'   
--header 'content-type: multipart/form-data;' -F "image=@/LOCATION/OF/THE/IMAGE.png"

You should get some JSON back like this:

unix imgur curl

Parsing XML from the command line

First apt-get install xmlstarlet. And let’s say we have this XML file:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">

We can use xmlstarlet sel to select elements. But first we need to tell it about the name space, so xmlstarlet sel -N x="http://www.opengis.net/kml/2.2".

Now let’s use -t -v to start using some XPath. In our case /x:kml/x:Document/x:name. So in full xmlstarlet sel -N x="http://www.opengis.net/kml/2.2" -t -v "/x:kml/x:Document/x:name" the_xml_file.xml. This will print:

xml unix

Unix: Replace newlines with sed

Imagine we have this file

Line one

Line two

And we want to remove the double new line.

This ugly looking command will replace all the newlines with nothing.

sed ':a;N;$!ba;s/\n\n//' YOURFILE

The :a says create a label - we’ll need this in a moment.

The N says append the next line onto the current pattern - we need this since we’re matching two lines. So say we’re trying to match the double newline this will give us ‘\n\n’ in our pattern space.

The $ matches the last line, i.e. in ‘\n\n’ we’ll be right at the end. And the ! inverts that. So here’s we’re matching the first ‘\n’ since this is not the last line.

Then the ba means go back to our label that we just created. So if we’re not on the last line, go back and do the match on the next part. This seems to be so the newline doesn’t halt the match.

unix sed

Page 1 of 13