I want to write a simple script that will send email to a specific address as soon as a USB device is plugged into a system or unplugged. Would someone please provide me a code snippet for this? I want to run it on different flavors of Linux where Ruby will be installed already.
You could add a new udev rule as follows. Create a file /etc/udev/rules.d/99-my-custom-rule, whose contents looks as follows:
SUBSYSTEM=="usb", ACTION=="add", RUN+="usb_notify_admin add %b"
SUBSYSTEM=="usb", ACTION=="remove", RUN+="usb_notify_admin remove %b"
Then put a script usb_notify_admin somewhere in the PATH:
#!/bin/sh
echo $# | mail -s "USB Notify Script" admin#example.com
Details:
Writing udev rules: http://www.reactivated.net/writing_udev_rules.html
mail man page: http://linux.die.net/man/1/mail
Related
I have written a bash script which starts a tcpip port and connects my device to my laptop for wireless debugging. This is the script at /bin/device_added.sh:
#!/bin/bash
adb shell ip -f inet addr show 2> /tmp/scripts.log
ip=$(adb shell ip -f inet addr show | egrep -o '192.*/' | sed 's/.$//')
adb tcpip 5555
adb connect $ip:5555
echo "USB device added at $(date)" >>/tmp/scripts.log
After configuring permissions with chmod, this works flawlessly on its own. But I want this script to be triggered whenever I plug in usb. I followed this answer to try to make this work. I created a 80-test.rules file at /etc/udev/rules.d and added this:
SUBSYSTEM=="usb", ACTION=="add", ENV{DEVTYPE}=="usb_device", RUN+="/bin/device_added.sh"
and reloaded the rules file using: sudo udevadm control --reload
Whenever I plug in usb, the script gets run(the date gets logged in scripts.log) but my device doesn't get connected. What am I doing wrong? Why does the script work properly when I run it manually but not when it is triggered through udev?
Edit: On basis of #markp-fuso's and #Charles Duffy's comment, I tried logging the error to /tmp/scripts.log file. Turns out I am getting this error:
line 3: adb: command not found
Now the strange part is, I got this error earlier but I solved it by placing the shell command before the tcpip command(atleast that worked when I ran the script directly). How am I supposed to deal with this error now?
Update:
As #markp-fuso pointed out, the problem was that environment variables weren't accessible to that script. Hence I created a the adb's location as a variable in the script and then made that used that variable as throught. My script now:
#!/bin/bash
adb=/home/pranil/Android/Sdk/platform-tools/adb
$adb shell ip -f inet addr show 2> /tmp/scripts.log
ip=$($adb shell ip -f inet addr show | egrep -o '192.*/' | sed 's/.$//')
$adb tcpip 5555
$adb connect $ip:5555
echo "USB device added at $(date)" >>/tmp/scripts.log
This solved the error I was getting in logs but still the adb doesn't get connected at the required port. I have no idea where I am going wrong now. One more thing, after my script runs, the offline emulator is no longer shown as an output of abd devices command.
I ssh to a device that gets attached to a test bench with the following:ssh root#1.2.3.4
Because the actual device has been changed since the last time I connected to that IP I get:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! all as expected. The warning tells me that I can easily fix this with ssh-keygen -f "/home/myuser/.ssh/known_hosts" -R "1.2.3.4"
I do find it repeating the same thing over and over, there must be some way to improve this. I know this IP and it is internal to my company.
I started working on trying to use something like !!:s/find/replace but the spaces in the replace is making my life difficult.
What is the easiest way to automate this, maybe create an alias?
Thank you
Jack
I asked a senior dev at my company and he suggested that I just update my ~/.ssh.config file. I added:
Host 1.2.3.*
User root
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
No more having to copy and paste the line to update my known_host file
I'm trying to use the bash shell to get a wifi profile for a certain network, and from that, the password
What I'm looking for is basically a mac equivalent to netsh wlan show profile name="name" key=clear, which gets exactly what I want with windows
I have looked into the airport and networksetup commands on mac, but still have yet to find something that will either show me the network profiles or, even better, what's inside.
Does anyone have any ideas or at least something to point me in the right direction?
If it's okay to use a Node.js script for this, you might have a look at wifi-password-cli which does exactly what you want:
$ wifi-password --help
Usage
$ wifi-password [network-name]
Example
$ wifi-password
unicorns
$ wifi-password foo-network
foosecretpassword
(Sample taken from the tool's documenation)
To use it, simply run (supposed that Node.js has already been installed on your system before):
$ npm install -g wifi-password-cli
The simple way is:
security find-generic-password -ga "ROUTERNAME" | grep "password:"
Add the WIFI name you are connected in the place of ROUTERNAME
Simply run the command from terminal app
security find-generic-password -wa <WIFI_NAME>
Replace <WIFI_NAME> with your Wifi Name.
A prompt will ask you for username and password.
Is it possible to access the current user's name and email address from the command line in OS X? If so, how would it be done?
There's an open-source CLI app to interact with Contacts (previously known as Address Book).
Setup
# install CLT, hombrew
brew install contacts
Email of Current User
contacts -Hm -f '%e'
Name of Current User
contacts -Hm -f '%n'
id -F will display the full name of the current user. id is a BSD replacement for whoami, which will be more portable than using osascript.
You can get the user's full name using AppleScript. Invoke the osascript command from the command line:
osascript -e "long user name of (system info)"
There's also the system_profiler command, which does provides a bunch of information, including this line:
User Name: First Last (username)
But system_profiler is really overkill.
If you want to scrape the email accounts from the users' mail preferences (providing they use mail.app) you could do
defaults read com.apple.mail|grep -A 1 EmailAddresses|grep #
I have written a udev rule that call a script to send email when a usb drive is attached/removed. so far its working fine. but i want to know if there is any way i can include detail of usb drive in my email? how can i get it using udev? like usb size, label, mounted on, time. following is my udev rule and send email script:
SUBSYSTEM=="usb", ACTION=="add", RUN+="/u/usb_added add %b"
usb_added script is:
sendEmail -f root#IP -t abc#live.com -s smtp.live.com:587 -m "USB Attached" -o username=xyz#live.com
I found the way by using udevadm monitor --environment. This gives complete details when a USB device is attached or removed.