You are not logged in.

#2151 2013-07-18 22:23:48

rlo
Member
From: Stockholm, Sweden
Registered: 2013-07-18
Posts: 3

Re: Post your handy self made command line utilities

I use this little script to keep my aur packages up to date.

#!/bin/bash

script='
import json,sys

obj=json.load(sys.stdin)

if len(obj["results"]) > 0:
  print(obj["results"][0]["Version"])
else:
  print("unknown")
'

pacman -Qm | while read line; do
  pkg=(${line})
  version=$(curl -s "https://aur.archlinux.org/rpc.php?type=multiinfo&arg=${pkg[0]}" | python -c "$script")
  color="\e[0;31m"
  if [ "$version" == "${pkg[1]}" ]; then
    color="\e[0;32m"
  fi
  echo -e "$color${pkg[0]} ${pkg[1]} => $version\e[00m"
done

Offline

#2152 2013-07-19 10:53:08

Earnestly
Member
Registered: 2011-08-18
Posts: 804

Re: Post your handy self made command line utilities

rlo wrote:

...

Have a look into using jshon for parsing json on the shell.

Last edited by Earnestly (2013-07-19 10:53:30)

Offline

#2153 2013-07-19 14:13:09

jan64
Member
Registered: 2013-07-19
Posts: 1

Re: Post your handy self made command line utilities

My script for checking kernel, mbr and initramfs checkums (You can add this to bashrc file and setup sudo to allow dd without providing password):
EDIT: Changed hash checks a bit, Thanks lolilolicon!

krnlchksum="cdc355df127a7e5e90edcd06154646e8bbb7641a"
irdchksum="b32d79cbc198a63ebf12fde2c4ef1cc8e61d4501"
irdfbchksum="1d98c91daaa1677422a9c59c5afe8f5da2810641"
mbrchksum="b4fe212152915cd4b411f51f392892321a7af5cd"

tmpkrnl=`sha1sum /boot/vmlinuz-linux | cut -d\  -f1`
tmpird=`sha1sum /boot/initramfs-linux.img | cut -d\  -f1`
tmpirdfb=`sha1sum /boot/initramfs-linux-fallback.img | cut -d\  -f1`
tmpmbr=`sudo dd if=/dev/sda bs=446 count=1 2>/dev/null | sha1sum - | cut -d\  -f1`

echo ""
echo "================================================================="
echo "                                                                 "
echo -e "            Logged in as:      \033[1m\E[0;34m\033[1m"${USER}
echo -e "\033[0m                                                                 "
echo "================================================================="
echo "                                                                 "
if [ "$mbrchksum" != "$tmpmbr" ]
then
echo -e "\033[0m            MBR checksum:       \033[1m\E[0;31m\033[1mERR"
else
echo -e "\033[0m            MBR checksum:       \033[1m\E[0;32m\033[1mOK"
fi

if [ "$krnlchksum" != "$tmpkrnl" ]
then
echo -e "\033[0m            Kernel checksum:    \033[1m\E[0;31m\033[1mERR"
else
echo -e "\033[0m            Kernel checksum:    \033[1m\E[0;32m\033[1mOK"
fi

if [ "$irdchksum" != "$tmpird" ]
then
echo -e "\033[0m            Initrd checksum:    \033[1m\E[0;31m\033[1mERR (Main)"
else
if [ "$irdfbchksum" != "$tmpirdfb" ]
then
echo -e "\033[0m            Initrd checksum:    \033[1m\E[0;31m\033[1mERR (Fallback)"
else
echo -e "\033[0m            Initrd checksum:    \033[1m\E[0;32m\033[1mOK"
fi
fi
echo -e "\033[0m                                                                 "
echo "================================================================="
echo ""

Last edited by jan64 (2013-07-19 15:15:03)

Offline

#2154 2013-07-19 14:24:55

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,676

Re: Post your handy self made command line utilities

jan64 wrote:
tmpmbr=`sudo dd if=/dev/sda of=/tmp/mbr.bin bs=446 count=1 >& /dev/null && sha1sum /tmp/mbr.bin && sudo rm /tmp/mbr.bin`

Embrace the pipe:

sha1_mbr=$(dd if=/dev/sda bs=446 count=1 2>/dev/null | sha1sum - | cut -d\  -f1)

Last edited by lolilolicon (2013-07-19 14:26:15)


This silver ladybug at line 28...

Offline

#2155 2013-07-20 13:11:18

defer
Member
From: Finland
Registered: 2013-06-25
Posts: 46
Website

Re: Post your handy self made command line utilities

CLI Wlan Manager. Uses iw, iproute2, wpa_supplicant and dhcpcd. Feedback and improvements are welcome.

You can find the current version from: https://github.com/defer-/my-projects/b … ter/cwm.sh

#!/bin/env bash

ifname="wlan0"

init () {
	sudo killall dhcpcd 2> /dev/null
	sudo ip link set $ifname up
}

if [ "$1" = "-o" ] && [ -n "$2" ]; then
	init
	sudo iw $ifname set type managed
	sudo iw $ifname connect -w $2
	sudo dhcpcd $ifname
elif [ "$1" = "-a" ] && [ -n "$2" ] && [ -n "$3" ]; then
	init
	sudo iw $ifname set type ibss
	sudo iw $ifname ibss join $2 $3
	sudo dhcpcd $ifname
elif [ "$1" = "-w" ] && [ -n "$2" ] && [ -n "$3" ]; then
	init
	sudo iw $ifname set type managed
	sudo iw $ifname connect -w $2 keys $3
	sudo dhcpcd $ifname
elif [ "$1" = "-W" ] && [ -n "$2" ] && [ -n "$3" ]; then
	init
	sudo iw $ifname set type managed
	sudo wpa_supplicant -B -i $ifname -c <(wpa_passphrase $2 $3)
	sudo dhcpcd $ifname
elif [ "$1" = "-s" ]; then
	sudo ip link set $ifname up	
	sudo iw $ifname scan | awk '
	/^BSS / {
		MAC = $2
		wifi[MAC]["enc"] = "Open"
	}
	$1 == "SSID:" {
		wifi[MAC]["SSID"] = $2
	}
	$1 == "freq:" {
		wifi[MAC]["freq"] = $2
	}
	$1 == "signal:" {
		wifi[MAC]["sig"] = $2 " " $3
	}
	$1 == "capability:" {
		wifi[MAC]["type"] = $2
	}
	$1 == "WPA:" {
		wifi[MAC]["enc"] = "WPA"
	}
	$1 == "WEP:" {
		wifi[MAC]["enc"] = "WEP"
	}
	END {
		for (w in wifi) {
			if ( i++ > 0 ) { printf "\n" }
			printf "SSID: %s\nType: %s\nFrequency: %s\nSignal: %s\nEncryption: %s\n"\
			,wifi[w]["SSID"],wifi[w]["type"],wifi[w]["freq"],wifi[w]["sig"],wifi[w]["enc"]
		}
	}'
elif [ "$1" = "-S" ]; then
	sudo ip link set $ifname up
	sudo iw $ifname scan
elif [ "$1" = "-i" ]; then
	sudo ip link set $ifname up
	sudo iw $ifname info
	sudo iw $ifname link
	sudo iw $ifname get power_save
else
	echo "CLI Wlan Manager"
	echo
	echo "-h               Show help"
	echo "-i               Show interface info"
	echo "-s               Scan access points"
	echo "-S               Scan access points verbosely"
	echo "-o ESSID         Connect to open (ESS) access point"
	echo "-a ESSID FREQ    Connect to ad-hoc (IBSS) access point"
	echo "-w ESSID KEY     Connect to WEP encrypted access point"
	echo "-W ESSID KEY     Connect to WPA encrypted access point"
fi

Last edited by defer (2013-08-02 12:25:08)

Offline

#2156 2013-07-20 13:18:53

HalosGhost
Member
From: Twin Cities, MN
Registered: 2012-06-22
Posts: 1,486
Website

Re: Post your handy self made command line utilities

defer wrote:

Simple CLI Wlan Manager. Uses iw/iproute2/dhcpcd. Not well tested yet. Feedback and improvements are welcome.

That's a pretty hefty if/elif/else block you got there. My recommendation would be for you to check out case statements. smile

All the best,

-HG


"All errors are ᴘᴇʙᴋᴀᴄ errors—It's just a matter of narrowing down which keyboard and chair." -Trilby
\ldots

Offline

#2157 2013-07-20 13:31:16

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,676

Re: Post your handy self made command line utilities

HalosGhost wrote:
defer wrote:

Simple CLI Wlan Manager. Uses iw/iproute2/dhcpcd. Not well tested yet. Feedback and improvements are welcome.

That's a pretty hefty if/elif/else block you got there. My recommendation would be for you to check out case statements. smile

All the best,

-HG

For me it's all that sudo that's bothering me.

Edit:
also spotted this

elif [ "$1" = "-wep" ] && [ "$2" ] && [ "3" ]; then

Last edited by lolilolicon (2013-07-20 13:32:13)


This silver ladybug at line 28...

Offline

#2158 2013-07-20 13:36:52

defer
Member
From: Finland
Registered: 2013-06-25
Posts: 46
Website

Re: Post your handy self made command line utilities

I forgot to say you should add "username ALL=NOPASSWD: /usr/sbin/iw,/bin/ip,/usr/sbin/wpa_supplicant,/sbin/dhcpcd" to /etc/sudoers to be able to sudo them without password.

And i dont think there is nothing wrong with case sensitive parameters if i understood what you said.

Offline

#2159 2013-07-20 13:46:59

Vain
Member
Registered: 2008-10-19
Posts: 174
Website

Re: Post your handy self made command line utilities

defer wrote:

Simple CLI Wlan Manager. Uses iw/iproute2/dhcpcd. Not well tested yet. Feedback and improvements are welcome.

Plus, the help message mentions "-w" and "-p" while the code checks for "-wep" and "-wpa". I don't think using "case" instead of "if" would simplify the code, though.

Nice idea. I'll keep it. Thanks for sharing. smile

Offline

#2160 2013-07-20 14:32:56

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,676

Re: Post your handy self made command line utilities

defer wrote:

I forgot to say you should add "username ALL=NOPASSWD: /usr/sbin/iw,/bin/ip,/usr/sbin/wpa_supplicant,/sbin/dhcpcd" to /etc/sudoers to be able to sudo them without password.

What I meant to say was that you don't need to put all the sudos in the script, instead, remove them from the script and run the script with sudo when necessary.

And i dont think there is nothing wrong with case sensitive parameters if i understood what you said.

http://mywiki.wooledge.org/BashGuide/Te … _select.29


This silver ladybug at line 28...

Offline

#2161 2013-07-20 14:57:48

HalosGhost
Member
From: Twin Cities, MN
Registered: 2012-06-22
Posts: 1,486
Website

Re: Post your handy self made command line utilities

Vain wrote:

I don't think using "case" instead of "if" would simplify the code, though.

Just as a demonstration. Here's how the code would look cleaned up a bit. This was just a once-over clean-up, so I'm sure there are some mistakes, but the point should be obvious.

P.S., having a flag for the command line which allows the user to specify an interface would be a very good idea (not everyone uses "wlan0").

All the best,

-HG

Last edited by HalosGhost (2013-07-20 15:01:11)


"All errors are ᴘᴇʙᴋᴀᴄ errors—It's just a matter of narrowing down which keyboard and chair." -Trilby
\ldots

Offline

#2162 2013-07-20 18:15:55

Thaodan
Member
From: Dortmund, Nordrein-Westfalen
Registered: 2012-04-28
Posts: 438

Re: Post your handy self made command line utilities

HalosGhost wrote:
Vain wrote:

I don't think using "case" instead of "if" would simplify the code, though.

Just as a demonstration. Here's how the code would look cleaned up a bit. This was just a once-over clean-up, so I'm sure there are some mistakes, but the point should be obvious.

P.S., having a flag for the command line which allows the user to specify an interface would be a very good idea (not everyone uses "wlan0").

All the best,

-HG

Just some suggestions: add a --help option and do the stuff before case only if valid arguments were found.
In my opinion long args should have two dashes, to make more clearly that its a long option.


Linux odin 3.13.1-pf #1 SMP PREEMPT Wed Mar 5 21:47:28 CET 2014 x86_64 GNU/Linux

Offline

#2163 2013-07-20 18:17:57

HalosGhost
Member
From: Twin Cities, MN
Registered: 2012-06-22
Posts: 1,486
Website

Re: Post your handy self made command line utilities

Thaodan wrote:

Just some suggestions: add a --help option and do the stuff before case only if valid arguments were found.
In my opinion long args should have two dashes, to make more clearly that its a long option.

This isn't really mine, that was just a clean-up of defer's script.

All the best,

-HG


"All errors are ᴘᴇʙᴋᴀᴄ errors—It's just a matter of narrowing down which keyboard and chair." -Trilby
\ldots

Offline

#2164 2013-07-23 08:43:50

kokoko3k
Member
Registered: 2008-11-14
Posts: 1,482

Re: Post your handy self made command line utilities

Since nvidia-smi works again for "low end" cards:
https://github.com/CFSworks/nvml_fix

I wrote a script that changes performance levels for my 9800GT with a threshold i defined.
Before i've had choppy kde effects and choppy firefox scrolling, due to the default policy being too much "conservative" and my card clocked to a very low speed when in powersave mode.
I then decided to "overclock" my powersave frequencies to an higher value, but that means more watts.

With this script is SO much better, i can keep default frequencies and switch faster.

#!/bin/bash
export LD_LIBRARY_PATH=/tmp/nvml_fix/built/319.23/

threshold=10            #switch to performance at this load
powersave=0             #powermizer=0 means adaptive mode
performance=1           #powermizer=1 means performance
curlevel=$performance   #pretend we started in performance mode
sleepmore=5            #seconds to wait between cycles when in performance mode
sleepless=0.5           #seconds to wait between cycles when in adaptive mode

while true ; do
    gpuuse=$(nvidia-smi -q --display=UTILIZATION |grep Gpu|sed  's/ //g'|cut -d ":" -f 2|cut -d "%" -f 1)
    echo gpu use=$gpuuse
    if [ $gpuuse -lt $threshold ]; then
        if [ $curlevel = $performance ]; then
            nvidia-settings -a [gpu:0]/GPUPowerMizerMode=$powersave &>/dev/null
            curlevel=$powersave
            echo switched to GPUPowerMizerMode=$curlevel ADAPTIVE 
            sleeptime=$sleepless
        fi
            else
        if [ $curlevel != $performance ]; then
            nvidia-settings -a [gpu:0]/GPUPowerMizerMode=$performance &>/dev/null
            curlevel=$performance
            echo switched to GPUPowerMizerMode=$curlevel PERFORMANCE 
            sleeptime=$sleepmore
        fi
    fi
    sleep $sleeptime
    echo current level=$curlevel
    echo -
done

Probably the two "cut" in the sed line could be simplified by just using sed, but i'm not that smart.
I will test this deeper, and if it really works as it is doing right now, i'll make a .service file.

Last edited by kokoko3k (2013-07-23 08:45:08)

Offline

#2165 2013-07-23 09:16:12

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,761
Website

Re: Post your handy self made command line utilities

kokoko3k wrote:

Probably the two "cut" in the sed line could be simplified by just using sed...

Or awk:

awk '/Gpu/ {print $3}' <(nvidia-smi -q --display=UTILIZATION)

Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#2166 2013-07-25 15:36:47

ThatPerson
Member
Registered: 2013-05-25
Posts: 9

Re: Post your handy self made command line utilities

A little program I use to automate installation. I have a laptop and netbook and I often reinstall Arch on them (I like changing desktop environments, and if I have a desktop with KDE, then replace it with MATE, often I end up with little programs which end up clogging stuff up, such as Amarok, which is not in anything like kdemultimedia-amarok, so I forget to remove). It can be found at https://github.com/ThatPerson/ANI.
In order to use it I configure the JSON file on a working computer, and run the Python script. This gives me a shell script. Then, I install the base bit (ie, Partition, mount to /mnt, and pacstrap base and base-devel, and then chroot in. Then, I can use the shell script to create users, generate locales, etc. I am planning on making it support more stuff, such as setting up GRUB, and doing keymaps, however generally I set the keymap in the desktop environment.
In the Git repo there is an example of one I used earlier, which installs the MATE desktop environment with NetworkManager.

Offline

#2167 2013-07-29 20:26:31

nfisher.sr
Member
Registered: 2013-06-15
Posts: 38
Website

Re: Post your handy self made command line utilities

Launches xephyr so I can debug my little WM tweaks (like pipemenus) and catch the output in the original terminal. Simple. Also useful for catching any other type of session errors in real time.

#!/bin/sh
cd
[ "$WM" = "" ] && WM=openbox
[ "$RES" = "" ] && RES=1024x768
Xephyr -ac -screen $RES -br -reset -terminate :100 2>/dev/null &
sleep 2
export DISPLAY=:100
$WM &

Offline

#2168 2013-08-02 12:32:13

defer
Member
From: Finland
Registered: 2013-06-25
Posts: 46
Website

Re: Post your handy self made command line utilities

colors.sh - Print table of terminal colors with escape sequences

#!/bin/env bash

t='abcd'

declare -a fg
declare -a bg
declare -a fg2

for i in {0..7}; do
	fg[$i]="\e[3${i}m"
	fg2[$i]="\e[1;3${i}m"
	bg[$i]="\e[4${i}m"
done

echo -en "       "
echo ${bg[@]}

for f in ${fg[@]} ${fg2[@]}; do
	echo -en "$f $t "
	for b in ${bg[@]}; do
		echo -en "$f $b $t \e[00m"
	done
	echo " $f"
done

extract.sh - Extract almost any archive. Thanks to Emmanuel Rouat for the idea.

#!/bin/env bash

if [ -z "$1" ]; then
	echo "Usage: extract [archive]"

if [ -f "$1" ]; then
	case "$1" in
		*.tar.bz2)  tar xjf "$1"      ;;
		*.tbz2)     tar xjf "$1"      ;;
		*.tar.gz)   tar xzf "$1"      ;;
		*.tgz)      tar xzf "$1"      ;;
		*.tar.xz)   tar xJf "$1"      ;;
		*.tar.Z)    tar xzf "$1"      ;;
		*.bz2)      bunzip2 "$1"      ;;
		*.gz)       gunzip "$1"       ;;
		*.xz)       unxz "$1"         ;;
		*.Z)        uncompress "$1"   ;;
		*.tar)      tar xf "$1"       ;;
		*.deb)      ar x "$1"         ;;
		*.7z)       7z x "$1"         ;;
		*.rar)      unrar x "$1"      ;;
		*.zip)      unzip "$1"        ;;
		*.jar)      unzip "$1"        ;;
		*.exe)      cabextract "$1"   ;;
		*)          echo "'$1' cannot be extracted" ;;
	esac
else
	echo "'$1' is not a file"
fi

Offline

#2169 2013-08-02 13:15:24

karol
Archivist
Registered: 2009-05-06
Posts: 25,427

Re: Post your handy self made command line utilities

Offline

#2170 2013-08-02 15:00:48

HalosGhost
Member
From: Twin Cities, MN
Registered: 2012-06-22
Posts: 1,486
Website

Re: Post your handy self made command line utilities

You could probably just use `bsdtar` for most of those. bsdtar is awesome tongue

All the best,

-HG


"All errors are ᴘᴇʙᴋᴀᴄ errors—It's just a matter of narrowing down which keyboard and chair." -Trilby
\ldots

Offline

#2171 2013-08-06 12:31:58

defer
Member
From: Finland
Registered: 2013-06-25
Posts: 46
Website

Re: Post your handy self made command line utilities

C CLI Wlan Manager
ccwm allows you to easily connect to any wlan network.
There is no need for configuration.
Uses iproute2, iw, wpa_supplicant and dhcpcd.
Newest version with Makefile can be found from my github.

Features
- Scan access points with nice output
- Connect to ESS access point (Open/WEP/WPA)
- Join to IBSS network (Open/WEP)
- Show interface info

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

void usage(char *progname) {
	printf("Usage: %s [options]\n\n", progname);

	puts("-h              Show help\n");

	puts("-i [interface]  Set interface (default: wlan0)");
	puts("-e [ssid]       Set SSID");
	puts("-f [freq]       Set frequency");
	puts("-w [key]        Set WEP key");
	puts("-W [passphrase] Set WPA passphrase\n");

	puts("-s              Scan access points");
	puts("-S              Scan access points verbosely");
	puts("-I              Show interface info\n");

	puts("-c              Connect to ESS access point (default if ssid is set)");
	puts("-j              Join to IBSS network (default if freq is set)");
}

void ifup(char *ifname) {
	char *cmdline = malloc(36);
	size_t s1 = strlen(ifname);

	snprintf(cmdline, s1+16, "ip link set %s up", ifname);
	system(cmdline);
	free(cmdline);
}

void info(char *ifname) {
	char *cmdline = malloc(43);
	size_t s1 = strlen(ifname);

	snprintf(cmdline, s1+13, "iw dev %s info", ifname);
	system(cmdline);
	snprintf(cmdline, s1+13, "iw dev %s link", ifname);
	system(cmdline);
	snprintf(cmdline, s1+23, "iw dev %s get power_save", ifname);
	system(cmdline);
	free(cmdline);
}

void scan(char *ifname, int type) {
	char *cmdline = malloc(33);
	size_t s1 = strlen(ifname);
	FILE *fp;
	char line[100];
	char aparray[20][5][100];
	int apnumber = -1;
	int specnumber = 0;
	int i = 0;

	ifup(ifname);

	if (type == 0) {
		snprintf(cmdline, s1+13, "iw dev %s scan", ifname);
		fp = popen(cmdline, "r");
		if (fp == NULL)
			puts("error");

		while (fgets(line, 100, fp) != NULL) {
			if (line[0] == 'B' && line[1] == 'S' && line[2] == 'S' && line[3] == ' ') {
				apnumber++;
				specnumber = 0;
				i = 0;
			}
			else if (strstr(line, "\tSSID: ") || strstr(line, "\tfreq: ") ||
					strstr(line, "\tsignal: ") || strstr(line, "\tcapability: ")) {
				memmove (line, line+1, strlen (line));
				strcpy(aparray[apnumber][specnumber], line);
				specnumber++;
			}
			else if (i < 1) {
				strcpy(aparray[apnumber][4], "Encryption: Open\n");
				i++;
			}
			else if (strstr(line, "\tWPA:\t")) {
				strcpy(aparray[apnumber][specnumber], "Encryption: WPA\n");
			}
			else if (strstr(line, "\tWEP:\t")) {
				strcpy(aparray[apnumber][specnumber], "Encryption: WEP\n");
			}
		}

		pclose(fp);

		for (i = 0; i < apnumber+1; i++) {
			if (i > 0)
				puts("");
			printf("%s%s%s%s%s",
				aparray[i][3], aparray[i][1], aparray[i][0], aparray[i][2], aparray[i][4]);
		}
	}
	else {
		snprintf(cmdline, s1+13, "iw dev %s scan", ifname);
		system(cmdline);
	}

	free(cmdline);
}

void connect(char *ifname, char *essid, char *freq, char *key, int type) {
	char *cmdline = malloc(100);
	size_t s1 = strlen(ifname);
	size_t s2 = strlen(essid);
	size_t s3 = strlen(freq);
	size_t s4 = strlen(key);

	ifup(ifname);
	system("killall dhcpcd 2> /dev/null");

	if (type < 3)
		snprintf(cmdline, s1+25, "iw dev %s set type managed", ifname);
	else
		snprintf(cmdline, s1+22, "iw dev %s set type ibss", ifname);

	system(cmdline);

	if (type == 0) {
		snprintf(cmdline, s1+s2+20, "iw dev %s connect -w %s", ifname, essid);
		system(cmdline);
	}
	else if (type == 1) {
		snprintf(cmdline, s1+s2+s4+26, "iw dev %s connect -w %s key %s", ifname, essid, key);
		system(cmdline);
	}
	else if (type == 2) {
		snprintf(cmdline, s1+s2+s4+45, "wpa_supplicant -B -i %s -c <(wpa_passphrase %s %s)",
				ifname, essid, key);
		system(cmdline);
	}
	else if (type == 3) {
		snprintf(cmdline, s1+s2+s3+20, "iw dev %s ibss join %s %s", ifname, essid, freq);
		system(cmdline);
	}
	else if (type == 4) {
		snprintf(cmdline, s1+s2+s3+s4+25, "iw dev %s ibss join %s %s key %s",
				ifname, essid, freq, key);
		system(cmdline);
	}
	else if (type == 5) {
		printf("Join to WPA encrypted IBSS not yet supported!\n");
		free(cmdline);
		return;
	}
	else {
		free(cmdline);
		return;
	}

	snprintf(cmdline, s1+8, "dhcpcd %s", ifname);
	system(cmdline);

	free(cmdline);
}

int main(int argc, char *argv[]) {
	int opt;
	int task = 0;
	char *ifname = "wlan0";
	char *essid = "";
	char *freq = "";
	char *key = "";
	char *passphrase = "";
	uid_t uid=getuid();

	if (uid != 0) {
		puts("\nThis program needs to be run with root privileges!\n");
		/* return 1; */
	}

	if (argc <= 1) {
		usage(argv[0]);
		return 0;
	}

	while ((opt = getopt(argc, argv, "sSIcjhi:e:f:w:W:")) != -1) {
		switch (opt) {
			case 'i':
				ifname = optarg;
				break;
			case 'e':
				essid = optarg;
				break;
			case 'f':
				freq = optarg;
				break;
			case 'w':
				key = optarg;
				break;
			case 'W':
				passphrase = optarg;
				break;
			case 's':
				task = 1;
				break;
			case 'S':
				task = 2;
				break;
			case 'I':
				task = 3;
				break;
			case 'c':
				task = 4;
				break;
			case 'j':
				task = 5;
				break;
			case 'h':
			default:
				usage(argv[0]);
				return 0;
				break;
		}
	}

	if (task == 0 && essid[0] != '\0')
		task = 4;
	else if (task == 0 && freq[0] != '\0' )
		task = 5;

	if (task == 1) {
		scan(ifname, 0);
	}
	else if (task == 2) {
		scan(ifname, 1);
	}
	else if (task == 3) {
		info(ifname);
	}
	else if (task == 4) {
		if (essid[0] == '\0') {
			printf("You must specify ssid!\n");
		}
		else if (key[0] != '\0') {
			connect(ifname, essid, freq, key, 1);
		}
		else if (passphrase[0] != '\0') {
			connect(ifname, essid, freq, passphrase, 2);
		}
		else {
			connect(ifname, essid, freq, key, 0);
		}
	}
	else if (task == 5) {
		if (essid[0] == '\0' || freq[0] == '\0') {
			printf("You must specify ssid and freq!\n");
		}
		else if (key[0] != '\0') {
			connect(ifname, essid, freq, key, 4);
		}
		else if (passphrase[0] != '\0') {
			connect(ifname, essid, freq, key, 5);
		}
		else {
			connect(ifname, essid, freq, key, 3);
		}
	}

	return 0;
}

Last edited by defer (2013-08-07 10:43:37)

Offline

#2172 2013-08-07 05:18:44

Earnestly
Member
Registered: 2011-08-18
Posts: 804

Re: Post your handy self made command line utilities

Or wpa_cli?

Offline

#2173 2013-08-11 01:49:34

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,761
Website

Re: Post your handy self made command line utilities

#!/usr/bin/env bash
# set wallpaper using sxiv & update .xinitrc

file="${1##*.}"
extn=$(grep "$file" $HOME/.xinitrc)
line=$(sed -n '/background.[jpg|png|gif]/ {=}' $HOME/.xinitrc)
xres=$(awk '/dimensions/ {sub(/x/,","); print $2}' <(xdpyinfo))

imlibsetroot --store $XDG_CONFIG_HOME/background."$file" -s "$xres" "$1"

[[ -z "$extn" ]] && sed -i "${line}s:\(jpg\|png\|gif\):${file}:" $HOME/.xinitrc

Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#2174 2013-08-11 02:24:41

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,804
Website

Re: Post your handy self made command line utilities

Why adjust the filename in xinitrc every time?  Just have one "wallpaper" file that is a link the the current background of choice.  Xinitrc can just set the background to /path/to/wallpaper_link, and changes can made by linking wallpaper to the new file.

Last edited by Trilby (2013-08-11 02:25:33)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#2175 2013-08-11 02:31:23

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,761
Website

Re: Post your handy self made command line utilities

I'm probably missing something; but when called from imlibsetroot, the background is set as background.{jpg,png,etc}; and if I switch between formats, I either have to update .xinitrc or a symlink (which is your suggestion). Is there an advantage to the latter over the former?


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB