Is there any way to connect to a remote container without open a foder? - vscode-remote

I am implementing a dev environment for Arduino an other MCUs. I have a container image with all the compilers and tool-chains required and I have an script to connect VSCode to it.
The connection magic is done by this:
CONTAINER_NAME="dev-environments-mcus"
hex=$(printf \{\"containerName\"\:\""$CONTAINER_NAME"\"\} | od -A n -t x1 | tr -d '[\n\t ]')
code --folder-uri vscode-remote://attached-container+${hex}/App_Home/mcu-projects
This works perfectly but the problem is that by doing this I am opening a specific folder in the container which is not ideal for a generic dev enviroment.
I would like to know if it is possible to replicate in cmdline the "Attach in new window" button behaviour, which open an "empty" window when you click on it.
Edit1: Replacing --folder-uri by --file-uri make my script work better but I would like to open no file or at least open the start page.
PS: Just in case you are curious this is the project github

Ok I think that I manage to solve it. I will share what I did just in case anyone find himself on the same situation.
I just had to use the option --file-uri rather than --folder-uri and append a slash / at then end of the command. Now no folder or empty file is open when VSCode starts.
This is how the script looks now:
CONTAINER_NAME="dev-environments-mcus"
hex=$(printf \{\"containerName\"\:\""$CONTAINER_NAME"\"\} | od -A n -t x1 | tr -d '[\n\t ]')
code --file-uri vscode-remote://attached-container+${hex}/

Related

Write a File from Jenkins Groovy Script-Console

I'm trying to find a way to write some content to a file using Jenkins Groovy Script-Console.
The use-case: Our CI manages some state-machine using a volume shared between all the nodes (which is in turn mapped to EFS). However - following the discovery of a bug in our CI groovy shared libs I found that some state files gone corrupt, and needed to write to them the corrected values, together with fixing the bug.
I could do that using ssh connection, however, as we're in process of abstracting out the workers we're trying to back off from that and manage ourselves only from the script-console and/or ci jobs.
I tried all these forms, all of which failed:
"echo 'the text' > /mnt/efs-ci-state/path/to/the-state-file.txt".execute().text
"""
cat <<<EOF > /mnt/efs-ci-state/path/to/the-state-file.txt
the text
EOF
""".execute().text
"bash -c 'echo the text > /mnt/efs-ci-state/path/to/the-state-file.txt'".execute().text
"echo 'the text' | tee /mnt/efs-ci-state/path/to/the-state-file.txt"
Can anybody show me the way to do that?
I'd also appreciate an explanation why the forms above won't work and/or a hint on how to execute commands that include piping and/or stdio directing from that script console.
Thanks :)
["bash", "echo the text > /mnt/efs-ci-state/path/to/the-state-file.txt"].execute().text
or use plain groovy:
new File('/mnt/efs-ci-state/path/to/the-state-file.txt').text = "echo the text"
why not working:
options 1, 2, 4 : echo and piping is a feature of shell/bash - it will not work without bash
option 3 you have c echo and c is not a valid command
use array to execute complex commands and to separate bash from main part
i suggest you to use this kind of code if you want to capture and validate stderr
["bash", 'echo my text > /222/12345.txt'].execute().with{proc->
def out=new StringBuilder(), err=new StringBuilder()
proc.waitForProcessOutput(out, err)
assert !err.toString().trim()
return out.toString()
}

How to view apps packageName with ADB commands? [duplicate]

I need to get the package name of an Android APK. I have tried to unzip the APK and read the contents of the AndroidManifest.xml file but it seems that it's not a text file.
How can I extract the APK's package name?
aapt dump badging <path-to-apk> | grep package:\ name
Install the apk on your Android device. Then
you can launch adb shell and execute pm list packages -f, which shows the package name for each installed apk.
This is taken from
Find package name for Android apps to use Intent to launch Market app from web.
Based on #hackbod answer ... but related to windows.
aapt command is located on Android\SDK\build-tools\version.
If you need more info about what is appt command (Android Asset Packaging Tool) read this https://stackoverflow.com/a/28234956/812915
The dump sub-command of aapt is used to display the values of individual elements or parts of a package:
aapt dump badging <path-to-apk>
If you want see only the line with package: name info, use findstr
aapt dump badging <path-to-apk> | findstr -n "package: name" | findstr "1:"
Hope it help other windows user!
If you are looking at google play and want to know its package name then you can look at url or address bar. You will get package name. Here com.landshark.yaum is the package name
https://play.google.com/store/apps/details?id=com.landshark.yaum&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS5sYW5kc2hhcmsueWF1bSJd
The following bash script will display the package name and the main activity name:
apk_package.sh
package=$(aapt dump badging "$*" | awk '/package/{gsub("name=|'"'"'",""); print $2}')
activity=$(aapt dump badging "$*" | awk '/activity/{gsub("name=|'"'"'",""); print $2}')
echo
echo " file : $1"
echo "package : $package"
echo "activity: $activity"
run it like so:
apk_package.sh /path/to/my.apk
If you open the AndroidManifest.xml using MS Notepad, search for phrase package and you'll find following:
package manifest $xxx.xxxxxxx.xxxxxxx |
where xxx.xxxxxxx.xxxxxxx is your package name, just written with a space after each character.
It's useful way when you don't have any specific tools installed.
Since its mentioned in Android documentation that AAPT has been deprecated, getting the package name using AAPT2 command in Linux is as follows:
./aapt2 dump packagename <path_to_apk>
Since I am using an older version of Gradle build, I had to download a newer version of AAPT2 as mentioned here :
Download AAPT2 from Google Maven
Using the build-tools in my sdk - 25.0.3, 26.0.1 and 27.0.3, executing the aapt2 command shows an error: Unable to open 'packagename': No such file or directory. That's why I went for the newer versions of AAPT2.
I used 3.3.0-5013011 for linux.
A Programmatic Answer
If you need to do this programmatically, it's a little more involved than just getting the answer into your brain. I have a script that I use to sign all of our apps, but each use a different key. Here are 2 ways to get just the Package Name as output so you can put it in a variable or do whatever you need with it.
Example output: com.example.appname (and nothing more)
Requirements
aapt - Android Asset Packaging Tool, part of the SDK Tools download
Solution 1
Using awk specify ' as the Field Separator, search for a line with package: name=, and print only the 2nd "field" in the line:
aapt dump badging /path/to/file.apk | awk -v FS="'" '/package: name=/{print $2}'
A weakness of this method is that it relies on aapt to output the package information fields in the same order:
package: name='com.example.appname' versionCode='3461' versionName='2.2.4' platformBuildVersionName='4.2.2-1425461'
We have no commitments from the developers to maintain this format.
Solution 2
Using awk specify " as the Field Separator, search for a line with package=, and print only the 2nd "field" in the line:
aapt list -a /path/to/file.apk | awk -v FS='"' '/package=/{print $2}'
A weakness of this method is that it relies on aapt to output package= only in the Android Manifest: section of the output. We have no commitments from the developers to maintain this format.
Solution 3
Expand the apk file with apktool d and read the AndroidManifest.xml.
This would be the best method, but the AndroidManifest.xml is a binary file and all the SO answers I see for converting it to text do not work. (Using apktool d instead of a simple unzip is supposed to do this for you, but it does not.) Please comment if you have an solution to this issue
A simple solution would be Open Android Studio -> Build -> Analyze Apk... browse and select the APK now you can find the package name and pretty much you can read.
You can use Analyze APK... from the Build menu in Android Studio, it will display the package name at the top of new window.
If you don't have the Android SDK installed, like in some test scenarios, you can get the package name using the following bash method:
getAppIdFromApk() {
local apk_path="$1"
# regular expression (required)
local re="^\"L.*/MainActivity;"
# sed substitute expression
local se="s:^\"L\(.*\)/MainActivity;:\1:p"
# tr expression
local te=' / .';
local app_id="$(unzip -p $apk_path classes.dex | strings | grep -Eo $re | sed -n -e $se | tr $te)"
echo "$app_id"
}
Tested on a mac. 'strings' and 'unzip' are standard on most linux's, so should work on linux too.
A very simple method is to use apkanalyzer.
apkanalyzer manifest application-id "${_path_to_apk}"
On Mac:
Way 1:
zgong$ /Users/zgong/Library/Android/sdk/build-tools/29.0.3/aapt dump badging ~/Downloads/NonSIMCC-151-app-release-signed.apk
package: name='com.A.B' versionCode='2020111801' versionName='1.0.40' compileSdkVersion='29' compileSdkVersionCodename='10'
sdkVersion:'24'
targetSdkVersion:'29'
......
Way 2:
/Users/zgong/Library/Android/sdk/build-tools/29.0.3/aapt2 dump packagename ~/Downloads/NonSIMCC-151-app-release-signed.apk
com.A.B
If you just want to know package name, run adb logcat, launch the activity you want , you will get a hint on the package name.
Another solution is to use aapt list and use sed to parse through that:
aapt list -a $PATH_TO_YOUR_APK | sed -n "/^Package Group[^s]/s/.*name=//p"
I think the best and simplest way to extract only the package name in Linux is
aapt dump badging <APK_path> | grep package | sed -r "s/package: name='([a-z0-9.]*)'.*/\1/"
Explanation:
AAPT extracts the APK information
Grep "package" to keep only the line about the package information
Make sed replace the whole line with the package name only using the following regex: package: name='([a-z0-9.]*)'.* and replacing with the first (and only) matching group.
There's a very simple way if you got your APK allready on your Smartphone. Just use one of these APPs:
Package Name Viewer Apps
To use this in batch scripting it's handy to have the script return just the package name (e.g. for uninstalling an app when you have the APK).
Here's the script I use:
# extract the android package id from a built apk file
# usage ./getPackageName.sh <path-to-apk>
line=`aapt dump badging "$1" | grep package:\ name`
# above returns:
# package: name='com.calvium.myapp' versionCode='1' versionName='1.0'
if [[ $line =~ name=\'(.+)\'\ versionCode ]]; then
echo ${BASH_REMATCH[1]}
else
echo "Failed to find package name"
exit 1
fi
available on gist
So you could write:
adb uninstall `./getPackageName.sh file.apk`
You can extract AndroidManifest.xml from the APK, remove all NULL bytes, skip everything until after the string 'manifest', and then you are at a length byte followed by the package name (and what comes after it). For the difficult task I use the great GEMA tool, so the command looks like this:
7z e -so MyApp.apk AndroidManifest.xml | gema '\x00=' | gema -match 'manifest<U1><U>=#substring{0;#char-int{$1};$2}'
Of course, you can use any other tool to do the filtering.
For Windows following worked for me:
:: // Initializing variables
SET adb="C:\Users\<User name>\AppData\Local\Android\Sdk\platform-tools\adb"
SET aapt="C:\Users\<User name>\AppData\Local\Android\Sdk\build-tools\22.0.0\aapt"
SET APKPath=C:\Users\<User name>\Desktop\APK\Instant_Instal\
CD %APKPath%
:: // Searching for apk file and storing it
FOR /F "delims=" %%f IN ('dir /S /B *.apk') DO SET "APKFullPath=%%f"
SET apk=%APKFullPath%
:: // Command adb install apk, run apk
%adb% install %apk%
:: // Fetching package name from apk
%aapt% dump badging %APKFullPath% | FIND "package: name=" > temp.txt
FOR /F "tokens=2 delims='" %%s IN (temp.txt) DO SET pkgName=%%s
del temp.txt
:: // Launching apk
%adb% shell monkey -p %pkgName% -c android.intent.category.LAUNCHER 1
pause
Note
Please edit the paths of adb, aapt, APKPath according to the paths of adb, aapt, and the apk location in your system.
Working:
Here I have added the apk in a folder on Desktop "\Desktop\APK\Instant_Instal\".
The command %adb% install %apk% installs the application if the device is connected.
This %aapt% dump badging %APKFullPath% | FIND "package: name=" > temp.txt fetches package name and a few other details like version etc. of the apk and stores in a temp.txt file in same location as that of the apk.
FOR /F "tokens=2 delims='" %%s IN (temp.txt) DO SET pkgName=%%sextracts the package name and assigns topkgName` variable
Finally %adb% shell monkey -p %pkgName% -c android.intent.category.LAUNCHER 1 launches the app.
In essence the above code installs the apk from given location in desktop "Desktop\APK\Instant_Instal\" to the device and launches the application.
You can get the package name programmatically by :
packageManager.getPackageArchiveInfo(apkFilePath, 0)?.packageName
you can instal Package_Name_Viewer.apk on your emulator and next you can see package name of all instaled app on your emulator.
I also tried the de-compilation thing, it works but recently I found the easiest way:
Download and install Appium from Appium website
Open Appium->Android setting, choose the target apk file. And then you get everything you want, the package info, activity info.
As I don't was able to find the package name in the .apk file with editor (like suggested above), I have checked the functions in the App "ES Datei Explorer" / "ES File Explorer" (free version) that I had installed already.
In this tool, the package name is showed properly.
As I think a good file explorer should not be missing on a phone, I suggest to use this tool (if you already have installed the apk on an mobile and have to know the package name).
If you want to read the package name of a typical APK file in your app, there's an easy way to analyze the PackageInfo:
fun getAPKPackageName(apkFile: File?): String? {
if (apkFile == null || !apkFile.isFile || !apkFile.exists()) return null
val apkFilePath = apkFile.absolutePath
if (apkFilePath.isNullOrEmpty()) return null
val packageManager = App.context.packageManager ?: return null
val packageInfo = packageManager.getPackageArchiveInfo(apkFilePath, 0) ?: return null
return packageInfo.packageName
}

How do I test a manual check in check_mk / Nagios

My organization is using Nagios with the check_mk plugin to monitor our nodes. My question is: is it possible run a manual check from the command line? It is important, process-wise, to be able to test a configuration change before deploying it.
For example, I've prepared a configuration change which uses the ps.perf check type to check the number of httpd processes on our web servers. The check looks like this:
checks = [
( ["web"], ALL_HOSTS, "ps.perf", "Number of httpd processes", ( "/usr/sbin/httpd", 1, 2, 80, 100 ) )
]
I would like to test this configuration change before committing and deploying it.
Is it possible to run this check via the command line, without first adding it to main.mk? I'm envisioning something like:
useful_program -H my.web.node -c ps.perf -A /usr/sbin/httpd,1,2,80,100
I don't see any way to do something like this in the check_mk documentation, but am hoping there is a way to achieve something like this.
Thanks!
that is easy to check.
Just make your config changes and then run:
cmk -nv HOSTNAME.
That (-n) will try run everything and return (-v) the output.
So can see the same results like later in the GUI.
List the check
$check_mk -L | grep ps.perf
if it listing ps.perf then run following command,
$check_mk --checks=ps.perf -I Hostname

On-the-fly compression of stdin failing?

From what was suggested here, I am trying to pipe the output from sqlcmd to 7zip so that I can save disk space when dumping a 200GB database. I have tried the following:
> sqlcmd -S <DBNAME> -Q "SELECT * FROM ..." | .\7za.exe a -si <FILENAME>
This does not seem to be working even when I leave the system for a whole day. However, the following works:
> sqlcmd -S <DBNAME> -Q "SELECT TOP 100 * FROM ..." | .\7za.exe a -si <FILENAME>
and even this one:
> sqlcmd -S <DBNAME> -Q "SELECT * FROM ..."
When I remove the pipe symbol, I can see the results and can even redirect it to a file within finishes in 7 hours.
I am not sure what is going on with piping large amount of output but what I could understand up until this point is that 7zip seems to be waiting to consume the whole input before it creates an archive file (because I don't really see a file being created to begin with) so I am not sure if it is actually performing on-the-fly compression. So I tried gzip and here's my experience:
> echo "Test" | .\gzip.exe > test.gz
> .\gzip.exe test.gz
gzip: test.gz: not in gzip format
I am not sure I am doing this the right way. Any suggestions?
Oh boy! It was PowerShell all along! I have no idea why this is happening at least with gzip. Gzip kept complaining that the input was not in gzip format. I switched over to the normal command prompt and everything started working.
I did observe this before. Looks like | and > have a slightly different functionality in PowerShell and Command prompt. Not sure what exactly it is but if someone knows about it, please add in here.

Change File Encoding to utf-8 via vim in a script

I just got knocked down after our server has been updated from Debian 4 to 5.
We switched to UTF-8 environment and now we have problems getting the text printed correctly on the browser, because all files are in non-utf8 encodings like iso-8859-1, ascii, etc.
I tried many different scripts.
The first one I tried is "iconv". That one doesn't work, it changes the content, but the file's encoding is still non-utf8.
Same problem with enca, encamv, convmv and some other tools I installed via apt-get.
Then I found a python code, which uses chardet Universal Detector module, to detect encoding of a file (which works fine), but using the unicode class or the codec class to save it as utf-8 doesn't work, without any errors.
The only way I found to get the file and its content converted to UTF-8, is vi.
These are the steps I do for one file:
vi filename.php
:set bomb
:set fileencoding=utf-8
:wq
That's it. That one works perfect. But how can I get this running via a script?
I would like to write a script (Linux shell) which traverses a directory taking all php files, then converting them using vi with the commands above.
As I need to start the vi app, I do not know how to do something like this:
"vi --run-command=':set bomb, :set fileencoding=utf-8' filename.php"
Hope someone can help me.
This is the simplest way I know of to do this easily from the command line:
vim +"argdo se bomb | se fileencoding=utf-8 | w" $(find . -type f -name *.php)
Or better yet if the number of files is expected to be pretty large:
find . -type f -name *.php | xargs vim +"argdo se bomb | se fileencoding=utf-8 | w"
You could put your commands in a file, let's call it script.vim:
set bomb
set fileencoding=utf-8
wq
Then you invoke Vim with the -S (source) option to execute the script on the file you wish to fix. To do this on a bunch of files you could do
find . -type f -name "*.php" -exec vim -S script.vim {} \;
You could also put the Vim commands on the command line using the + option, but I think it may be more readable like this.
Note: I have not tested this.
You may actually want set nobomb (BOM = byte order mark), especially in the [not windows] world.
e.g., I had a script that didn't work as there was a byte order mark at the start. It isn't usually displayed in editors (even with set list in vi), or on the console, so its difficult to spot.
The file looked like this
#!/usr/bin/perl
...
But trying to run it, I get
./filename
./filename: line 1: #!/usr/bin/perl: No such file or directory
Not displayed, but at the start of the file, is the 3 byte BOM. So, as far as linux is concerned, the file doesn't start with #!
The solution is
vi filename
:set nobomb
:set fileencoding=utf-8
:wq
This removes the BOM at the start of the file, making it correct utf8.
NB Windows uses the BOM to identify a text file as being utf8, rather than ANSI. Linux (and the official spec) doesn't.
The accepted answer will keep the last file open in Vim. This problem can be easily resolved using the -c option of Vim,
vim +"argdo set bomb | set fileencoding=utf-8 | w" -c ":q" file1.txt file2.txt
If you need only process one file, the following will also work,
vim -c ':set bomb' -c ':set fileencoding=utf-8' -c ':wq' file1.txt

Resources