Is there any Android shell command to know the connected device name - shell

When i connect my phone to my system and when i enter "adb devices" it shows
"List of devices attached
09478d300f4133f6 device"
Is there a shell command to extract only the name of device? I just want "09478d300f4133f6".
As i am not a programmer i am not expecting any code. Great if the solution can be provided with any shell commands.

You will need to use Linux or Mac OS to run this:
adb devices | grep -w "device" | awk '{print $1;}'

Related

Bash script doesn't function as intented on usb connection

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.

Alternative to hostname that does not depend on network connection

I develop code designed to run with file systems on multiple computers. The data directory is synced across computers, but the particular location of the data directory on each computer is unique. To address this, I setup a paths file where the beginning of the path depends on which computer the code is running on. I determine which computer the code is running on using the hostname function.
This has started causing problems running code on my local machine, as the output depends on the particular network I connect to, which can be pretty random if I'm using wifi at a coffee shop.
Is there an alternative to hostname that will return something that identifies I am using my local machine and is not affected by my particular network connection?
There are a number of options for this. You could get the Mac's serial number or hardware UUID (see this question):
SerialNumber=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}')
HardwareUUID=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformUUID/{print $(NF-1)}')
There's also the hardware address of the first network interface:
en0MAC=$(ifconfig en0 | awk '$1=="ether" {print $2}')
If you prefer to use the computer name as it's set in the Sharing pane of System Preferences (or the mDNS compatible version of it):
ComputerName=$(scutil --get ComputerName)
LocalHostName=$(scutil --get LocalHostName)
Warning: the computer name may contain spaces and other weird characters, so be extra-sure to double-quote any references to that variable to avoid parsing problems.

How to remove only some network services on Mac?

I noticed that on my Network I have thousands of useless services and I want to remove them, but I want to remove only these starting with "SAMSUNG".
When I run:
% networksetup -listallnetworkservices
I get:
SAMSUNG Modem 1
...
SAMSUNG Modem 1392
Wi-Fi
LGE Android Phone 2
iPhone
Bluetooth PAN
Thunderbolt Bridge
As you can see I have more than on thousand of services starting with "SAMSUNG" and I want to remove only them.
When I trey to remove one of them:
% networksetup -removenetworkservice "SAMSUNG Modem 4"
I just can't because of this error:
You cannot remove SAMSUNG Modem 4 because there aren't any other network services on SAMSUNG Modem.
** Error: The parameters were not valid.
How can I remove this?
I had this problem recently and the solution is very simple
this file contains all network adapters /Library/Preferences/SystemConfiguration/preferences.plist
this command list all samsung modems: networksetup -listallnetworkservices | grep "SAMSUNG Modem"
this command remove one samsung modem (where X is a modem number, ex: "SAMSUNG Modem 155"): networksetup -deletepppoeservice "SAMSUNG Modem X"
To remove all Samsung Modems do it on terminal:
for service in $(/usr/sbin/networksetup -listallnetworkservices | grep "SAMSUNG Modem" ); do
/usr/sbin/networksetup -deletepppoeservice "${service}"
done
Solved by executing the following bash script:
/usr/sbin/networksetup -listallnetworkservices | grep "SAMSUNG Modem" | while read -r line
do
/usr/sbin/networksetup -deletepppoeservice "${line}"
printf 'Deleted %s\n' "${line}"
done
Decided not to touch /Library/Preferences/SystemConfiguration/preferences.plist, not sure if the connection should not be deleted elsewhere.
wish there was a more easy way...but nothing...
i stumbled upon your same issue...
Anyway...you can solve this issue cleaning this file
/Library/Preferences/SystemConfiguration/preferences.plist
And by cleaning i mean: "remove the networkservice you don't need"
UPDATE
I just emptied that file (ofc i did a backup before), opened network in setting and was totally empty (ofc). so i just added a new Position and named "Automatic" and now all works like a charm without the thousand of network bogus services
Have a good day
Antonio

Mac OS terminal command How to differentiate wlan from ethernet address?

I'm making a mac os app. I'm trying to get the ethernet and wlan addresses. I know these are en0 and en1 addresses but depending on devices, en0 can be the ethernet or the wlan one. Is there a way to know which one it is?
So far I'm using this which gets me both addresses but don't differentiate them:
let task=Process.init()
task.launchPath="/sbin/ifconfig"
task.arguments=["en0"] //or en1
let pipe=Pipe()
task.standardOutput=pipe
task.launch()
let data=pipe.fileHandleForReading.readDataToEndOfFile()
guard let stringResult=String(data: data, encoding: String.Encoding.utf8) as NSString? else{wlanFailed();return}
print("en0:", stringResult)
EDIT
So now I'm trying to run this command networksetup -listnetworkserviceorder which works from my terminal.
But I don't know how to make it work from my Mac app. For example, with this:
let task=Process.init()
task.launchPath="/sbin/networksetup"
task.arguments=["-listnetworkserviceorder"]
I get:
launch path not accessible
These are some commands that help to match the interface with the hardware name:
networksetup -listallhardwareports
networksetup -listnetworkserviceorder
system_profiler SPNetworkDataType
scutil <<< "list" | grep -i airport
Thanks #artem-dorodovskiy and #nbari for giving me the command line.
I found how to write it thanks to this SOF answer.
let task=Process.init()
task.launchPath="/usr/bin/env"
task.arguments=["networksetup", "-listnetworkserviceorder"]

Getting wifi passwords with a mac

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.

Resources