App-Cheats

 view release on metacpan or  search on metacpan

cheats.txt  view on Meta::CPAN

#
# CheatSheet
#
#############################################################


#############################################################
## ADB - OnePlus
#############################################################

# Connect to phone and fix file permissions. (oneplus)
adb shell
su
chmod 777 /data/data

#############################################################
## Android
#############################################################

# Copy android files over the network (like myPhoneExplorer)
sudo apt install syncthing
syncthing

# Launch "My Files" on Android.
am start -n 'com.sec.android.app.myfiles/.external.ui.MainActivity'


#############################################################
## Apache
#############################################################

# Start apache server web pages
sudo apachectl start

# Check status of apache server web pages
apachectl status

# Show apache settings as in config files
httpd -S


#############################################################
## Apt Mark
#############################################################

# Prevent certain packages from ever automatically
# upgrading or being removed.
apt-mark hold apksigner


#############################################################
## ASM
#############################################################

# Compile, link, and run assembly program on windows
nasm -fwin64 hello.asm && gcc hello.obj -o hello.exe && hello.exe


#############################################################
## Batch - Conditions
#############################################################

# If statement in windows 10 DOS (windows 10, command prompt)
if exist "C:\Program Files\Git\usr\bin\perl2.exe" (echo FOUND) else (echo not found)

# Batch check if file/folder exists (windows,dos)
set folder=D:\my\ncfile\dev\assets
echo %folder%
IF EXIST %folder% (
	echo Yes;
	echo 123;
) ELSE (
	echo No;
	echo 456;

cheats.txt  view on Meta::CPAN

sudo apt dist-upgrade.


#############################################################
## Linux Commands - apt-key
#############################################################

# Add the key of a package site to allow installing from other sources
wget -q http://opensource.wandisco.com/wandisco-debian.gpg -O- | sudo apt-key add -


#############################################################
## Linux Commands - apt-list
#############################################################

# List trusted source lists for packages
sudo apt-key list


#############################################################
## Linux Commands - arp
#############################################################

# View all IP addresses (Debug,pi)
# Scan network
arp -a
netstat -a | findstr 172.17

# Remove arp -a cache (pi,debug)
netsh interface ip delete arpcache


#############################################################
## Linux Commands - awk
#############################################################

# Get the first line, second field (read file)
sui -u | grep "abc" | awk 'NR==1 {print $2}'


#############################################################
## Linux Commands - basename
#############################################################

# Get the basename or last file/directory of a string
basename <string>


#############################################################
## Linux Commands - cat
#############################################################

# Add line numbers to a file
cat -n <file>

# Skip the first line on the file
cat file | tail -n +2


#############################################################
## Linux Commands - chmod
#############################################################

# Set suid for a file
sudo chmod +s file

# Change permission of only the group
chmod g+w file


#############################################################
## Linux Commands - chown
#############################################################

# Fix ownership of user directory
cd home_dir/user_dir
sudo chown -Rh ${PWD##*/} `ls -A`


#############################################################
## Linux Commands - cmatrix
#############################################################

# See the matrix
cmatrix


#############################################################
## Linux Commands - cp
#############################################################

# Update all files that are less than 70 days old according to a file
# (Modified cp command)
cp -s --no-preserve=ownership  $FILE `find $DIR/*/$FILE -ctime -70`

# Create a hardlinked folder.
cp -al A B


#############################################################
## Linux Commands - crontab
#############################################################

# Add new crontab
crontab -e

# Start running cron tab
crontab <cron_file>

# View all crontabs on a bench
ls /var/spool/cron/crontabs

# Schedulers
crontab
cron
at

# Crontab job in interactive mode
* * * * * DISPLAY=localhost:11.0 xterm -e 'read -p "aaa - 3"'

# crontab job sent to any particular IP address
* * * * * DISPLAY=1.1.1.1:0 xterm -e 'read -p "aaa - 3"'

# Run a task according to a step amount (say every 5 minutes, crontab)
# min hr dom mon dow   command
  /5  *  *   *   *     my_command

# Run crontab at 3am and 3pm (task)

cheats.txt  view on Meta::CPAN



#############################################################
## Linux Commands - eval
#############################################################

# Variable name is stored in another variable
n1=abc
abc=blue
abc2=green
eval var=\$$n1
echo $var
n1=abc2
eval var=\$$n1
echo $var

# Expand $HOME variable (dollar,bash)
file='$HOME/my_path'
"`eval echo $file`"

# Expand uicfg $HOME variables
cat $HOME/uicfg | while read line; do echo `eval echo $line`; done


#############################################################
## Linux Commands - expect
#############################################################

# Run a command where user input is required (script,auto,password)
expect -c 'spawn ssh SOME_USER@irkdes "date"; expect "password:"; send "asdf1234\r"; interact'

# Expect quirks (scipt,auto,password)
# This will NOT work
echo "SOME_USER" | while read n; do expect ... ; done
# This DOES work
for n in "SOME_USER"; do expect ... ; done

# Suppress output (scipt,auto,password)
# Some output
expect -c 'spawn -noecho ... '
# All output
expect -c 'log_user 0; spawn ... '     # Restore with "log_user 1"


#############################################################
## Linux Commands - find
#############################################################

# Find every file where a name is used (starting from pwd location)
find . * | perl -ne 'print unless /^\./' | perl -lne 'print if -f' | xargs perl -lne 'print $ARGV if /start.txt/'

# Find all files but do not look in certain directories
find $source -not \( -path '*.svn' -prune -or -path '*.git' -prune \) > $all_files

# Find specific files but return directories
find . -name "*.h" -printf '%h\n'
find . -name "*.h" -exec dirname {} ';'
find . -name "*.h" -exec dirname {} \;

# Change permissions of all files / folders.
find . -type f -exec chmod 664 {} ';'
find . -type d -exec chmod 775 {} ';'

# Using a variable to define the find string.
n="-name *.pm";   find . $n     # Works.
n="-name '*.pm'"; find . $n     # Looks correct, but does NOT work!

# Find all git directories from current location
find . -name .git -type d | xargs -I{}  readlink -f {} | perl -ple 's#^/\w+##; s#/\.git$##'

# Find all files owned by a certain user
sudo find . -user xbe4092
sudo find . -maxdepth 3 -user xbe4092

# Find all files which are more than 100 days old (since they were accessed)
find $DIR -name "*.txt*" -atime +100
ll `find $DIR -name "*.txt*" -atime +100`

# Find all files which are more than 100 days old and deletes them
sudo find $DIR -name "*.txt*" -atime +100 -delete

# Search for multiple files patterns
find $DIR -name "*.glo*" -o -name "*name*"

# Find all files in current location
# but do not go into certain folders (omit, exclude)
find . -not \( -path '*.svn' -prune \)

# Find and perform an action (1,semicolon)
find $DIR -exec echo Arg: {} ';'

# Find and perform an action (2,plus)
find $DIR -exec echo Arg: {} '+'

# Find all empty folders (directory)
find . -type d -empty

# Delete all files using "find"
find dir/ -exec rm -f{} +								# real    0m7.810s	0m26.089s	0m28.655s
find dir/ -delete										# real    0m27.124s	0m40.121s
find dir -type f -print0 | xargs -0 -P13 -n100000 rm	# real	  38m5.886s

# Find all the hard links to a file (path)
find . -samefile otrs/.vimrc

# Find and delete files smaller than 100M.
find MY_PATH -name '*.mp3' -size -100M -delete


#############################################################
## Linux Commands - free
#############################################################

# Check swap memory usage (Linux Commands - free)
free -h
top
htop


#############################################################
## Linux Commands - fuser
#############################################################

# Find which process has a file open
fuser - identify processes using files or sockets


#############################################################
## Linux Commands - getconf
#############################################################

# Show/See/Displays all configuration variables
getconf -a


#############################################################
## Linux Commands - grep
#############################################################

# Find what is setting the sticky bit to /tmp folder upon machine reboot
grep -r chmod /etc/* 2>/dev/null | NOT "chmod (?:0?(?:-[f]\s+)?\d{3}|[aguo]*[+-][rwxs]+)\b" '^Binary' | add_color chmod
grep -r '/tmp' /etc/* 2>/dev/null | NOT '^Binary' | add_color tmp

# Return matching file name only
grep --color "trace config off"  * -l

# Return nonmatching file name only
grep --color "trace config off"  * -L

# Find what is common in both files (difference)
fgrep -Ff a b
grep -f a b
perl -e '@s{`cat a`}=(); exists $s{$_} and print for `cat b`'

# Find what is in second but not in first file (difference)
fgrep -v -Ff a b
grep -vf a b
perl -e '@s{`cat a`}=(); exists $s{$_} or print for `cat b`'

# Using grep for a OR like selection
cat file | grep -e this -e that -e those

# Change grep --color
export GREP_COLOR="32"              # green
echo "AabcC" | grep --color abc
export GREP_COLOR="32;4"            # green, underlined
echo "AabcC" | grep --color abc


#############################################################
## Linux Commands - groupadd
#############################################################

# Create new user group
groupadd


#############################################################
## Linux Commands - gzip
#############################################################

# Check file size of a gzip ubin file
# Shows compressed uncompressed  ratio uncompressed_name
gzip -l my.ubin2.gz


#############################################################
## Linux Commands - hdparm
#############################################################

# Disable write_cacheh on Ubuntu/Linux.
vi /etc/hdparm.conf
# Uncomment "write_cache"

# Check write_cacheh on Ubuntu/Linux.
sudo hdparm -W -i /dev/sda


#############################################################
## Linux Commands - history
#############################################################

cheats.txt  view on Meta::CPAN

# Check processor family (DES,UI)
grep "cpu family" /proc/cpuinfo
# Cpu family of "6" means use "Pentium-Pro"

# Change boot loader to use certain cpu's (DES,UI,Mark,shielding)
cd /boot/grub
sudo vi grub.cfg
/\Vlinux /boot/vmlinuz-3.16.51+20180205+1524
/\Vlinux /boot/vmlinuz-3.16.51+20180205+1524
# Add this to
isolcpus=0,1,2,3,4,5,6,7,8,9
cat /proc/cmdline

# Check timer frequency (DES,UI,CONFIG_HZ_1000)
cat /usr/src/linux.config | OR CONFIG_COMPAT_BRK CONFIG_HZ_PERIODIC CONFIG_X86_GENERIC CONFIG_SCHED_SMT CONFIG_HZ_1000

# find biggest binary number on the machine
perl -lE '@a=split //, sprintf "%b",  ~~-1; print @a'
perl -lE '@a=split //, sprintf "%b",  ~~-1; print scalar @a'

# Size of an integer on this system
perl -le 'print length pack "i"'


#############################################################
## Linux File Properties - General
#############################################################

# View more info about a file (Bash)
# Number of links, inode
stat $FILE

# Check inode of a file.
ls -i FILE

# Find all hardlinks to a file.
find . -samefile FILE
find . -inum INODE

# Print all the symbolic links in a directory and show where they link to (for Melvin)
ls | perl -lne '$r=readlink; print "$_ -> $r" if -l'

# Check if a file is opened (Added: 2017-11-20 02:48:53 PM)
lsof | grep my_file

# Check if vi has open (Added: 2017-11-20 02:49:11 PM)
lsof -c vi

# Check/Detect a symbolic link
[ -L "link_file" ] && echo "A"


#############################################################
## Linux File Properties - Sticky Bit
#############################################################

# Show if sticky bit is set on temp directory
[ -k /tmp ] && echo t

# Set sticky bit
chmod +t /tmp

# Remove sticky bit
chmod -t /tmp


#############################################################
## Linux Firewall
#############################################################

# Add port to firewall
firewall-cmd --add-port=8081/tcp --permanent
firewall-cmd --reload

# Problem:
#     Created a docker container and published it to a certain port.
#     Port returns nothing in a web browser
# Solution:
#     The machine has a firewall.
#     I had to do as root:
firewall-cmd -add-port=8089/tcp --permanent

# View firewall rules.
iptables -L


#############################################################
## Linux Logging
#############################################################

# View the error log for puppet
cat /var/log/syslog


#############################################################
## Linux Mounting
#############################################################

# Check paths to file servers
mount

# Fix Stale NFS on a file server. home4 down. cannot access
sudo umount -lf /net/home4
				# should automount in a few secs
/etc/init.d/autofs restart	# Otherwise, restart manually
/etc/init.d/nfs-common restart	# Do both

# Change path to file server.
sudo vi /etc/auto.pw 		# edit OLD to NEW
sudo service autofs reload	# reload auto mounting (only on higher benches)
sudo /etc/init.d/autofs restart
sudo /etc/init.d/autofs reload

# Get copy of latest auto loader server file
sudo \cp /home3/SOME_USER/auto.pw /etc/auto.pw
sudo /etc/init.d/autofs reload

# Setup puppet to change path to file server
sudo vi /etc/puppet/modules/autofs/files/auto.pw
sudo /etc/init.d/autofs reload


#############################################################
## Linux Software
#############################################################

# Which OS version are we using (DES,Ubuntu 22.04)
lsb_release -a

# Find out if using 32 or 64 bit linux system
uname -m     # 32 is i686 or i386, 64 is x86_64

# Find out the release name (UI,bench,DES)
lsb_release -a

# Show which debian version name machine is (such as wheezy) (Added: 2017-10-05 11:26:48 AM)
lsb_release -cs

# Get name of our OS:
cat /etc/os-release | \grep '^ID='
ID="centos"


#############################################################
# Linux Startup
#############################################################

# Prevent a program from starting on Ubuntu during boot.
# Click on “Startup Application” and launch Startup Program Preference.


#############################################################
## Linux Swap Memory
#############################################################

# Create swap memory file
#
mkdir /media/fasthdd
dd if=/dev/zero of=/media/fasthdd/swapfile.img bs=512 count=1M
#
# Turn that swap file into a filesystem !?
mkswap /media/fasthdd/swapfile.img
chmod 600 /media/fasthdd/swapfile.img
#
# Add this line to /etc/fstab to be able to use the swap at startup
/media/fasthdd/swapfile.img swap swap sw 0 0
#
# Activate the swap file
swapon /media/fasthdd/swapfile.img
#
# Deactivate the swap file
swapoff /media/fasthdd/swapfile.img

# View swap memory
cat /proc/swaps
top


#############################################################
## Ansible
#############################################################

# Install ansible prerequisites (on lnxbr42)
sudo apt-get install vagrant virtualbox

# Install ansible
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 123456
sudo apt-get update
sudo apt-get install ansible


#############################################################
## Bash - General
#############################################################

# See max expansion of *
getconf ARG_MAX		# 2097152

# Run a bash command without using a file
bash -c 'echo abc'

# Link without having to go the that folder location
bash -c 'cd; cd sb_stats; rm -f latest; ln -s my latest'
#
latest_file=nytprof/1/my.out
latest_link=nytprof/latest
ln -r -s "$latest_file" "$latest_link"

# Brace expansion in bash.
# (will create multiple files).
touch file-{1..10}.txt


#############################################################
## Bash - Arrays
#############################################################

# Making an array in bash ($* $@)
a=(one two "three four")

# Subtle differences in using an array in bash ($* $@)
print_args ${a[*]}
[one]

cheats.txt  view on Meta::CPAN

## Perl Modules - Module::Refresh
#############################################################

# Perl Modules - Module::Refresh
# My.pm:
#
#!/usr/bin/env perl
package My;
use strict;
use warnings;
use parent qw( Exporter );
our @EXPORT = qw( Run );
sub Run { print "111\n" }
1;
#
perl -I. -MModule::Refresh -E 'use My; Run(); say qq(before: $INC{"My.pm"}); Module::Refresh->refresh_module("My.pm"); say qq(after: $INC{"My.pm"}); Run()'
111
before: My.pm
after: My.pm
Undefined subroutine called at -e line 1.

# Cannot undef, delete, and require a subroutine.
# My.pm:
package My;
sub Run { print "111\n" }
1;
#
perl -e '
    require My;
    My->Run();
    undef &My::Run;
    delete $My::{Run};
    require My; Run();
'
111
Undefined subroutine called at -e line 1.


#############################################################
## Perl Modules - Module::Starter
#############################################################

# Create a new distribution in perl abd run it.
x2hs -X Example      # Pure Perl
h2xs -A -n Example   # XS
perl Makefile.PL
make
perl -Mblib -MExample2 -E 'Example2::print_hello()'

# Using -Mblib is similar to using:
perl -Iblib/lib -Iblib/arch -MExample2 -e 'Example2b::print_hello'

# Create a new distribution in perl.
module-starter --module=My::Test --distro=my-test --author="AUTHOR" --email="EMAIL" --mb --verbose

# Additional folder preparation (module-starter)
mv App-Pod/* .
rmdir App-Pod
mv ignore.txt .gitignore
echo "*.swp" >> .gitignore
chmod +x Build.PL
# Remove MYMETA.* and META.*
#
# Prepend to Build.PL
"#!/bin/env perl
"

# Additional folder preparation (module-starter)
# Add to Build.PL
    meta_merge     => {
        resources => {
            bugtracker => 'https://github.com/poti1/data-trace/issues',
            repository => 'https://github.com/poti1/data-trace',
        },
    },

# Additional folder preparation (module-starter)
# Create: .github/workflows/ci.yml
---
name: build and test
on:
  push:
    branches:
      - "*"
  pull_request:
    branches:
      - "*"
  workflow_dispatch:
jobs:
  build-job:
    name: Build distribution
    runs-on: ubuntu-20.04
    container:
      image: perldocker/perl-tester:5.38
    steps:
      - uses: actions/checkout@v4
      - name: Run Tests
        env:
          AUTHOR_TESTING: 1
          AUTOMATED_TESTING: 1
          EXTENDED_TESTING: 1
          RELEASE_TESTING: 1
        run: auto-build-and-test-dist
      - uses: actions/upload-artifact@v4
        with:
          name: build_dir
          path: build_dir
        if: ${{ github.actor != 'nektos/act' }}
  coverage-job:
    needs: build-job
    runs-on: ubuntu-20.04
    container:
      image: perldocker/perl-tester:5.38
    steps:
      - uses: actions/checkout@v4 # codecov wants to be inside a Git repository
      - uses: actions/download-artifact@v4
        with:
          name: build_dir
          path: .
      - name: Install deps and test
        run: cpan-install-dist-deps && test-dist

cheats.txt  view on Meta::CPAN

+ time.sleep(2)
+
+ elem = driver.find_element_by_name('q')
+ elem.send_keys('ChromeDriver')
+ elem.submit()
+ time.sleep(2)
+
+ driver.quit()

# Perl script to run a simple selenium test
# Requires having the selenium to be already running:
java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar /usr/local/lib/selenium/current.jar
#
+ #!/usr/bin/perl
+
+ use v5.10;
+ use Selenium::Remote::Driver;
+ use Selenium::Remote::WDKeys;;
+
+ my $driver = Selenium::Remote::Driver->new(
+    browser_name => 'chrome',   # Default is firefox
+ );
+
+ $driver->get('http://www.google.com');
+ $driver->pause(2000);
+
+ my $elem = $driver->find_element_by_name('q');
+ $elem->send_keys('ChromeDriver');
+ $elem->send_keys( KEYS->{enter} );
+ $driver->pause(2000);
+
+ $driver->quit;

# Check if selenium service is running
systemctl status selenium.service

# Install selenium-server-standalone
# https://tecadmin.net/setup-selenium-chromedriver-on-ubuntu/
#
# Step 1 – Prerequisites:
sudo apt install -y unzip xvfb libxi6 libgconf-2-4
sudo apt install default-jdk
#
# Step 2 – Install Google Chrome:
sudo curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add
sudo bash -c "echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' >> /etc/apt/sources.list.d/google-chrome.list"
sudo apt update
sudo apt install google-chrome-stable
#
# Step 3 – Installing ChromeDriver:
google-chrome --version
#
# Download same version:
# https://chromedriver.chromium.org/downloads
cd ~/Downloads
unzip chromedriver*.zip
#
# Move it:
sudo mv chromedriver /usr/bin/chromedriver
sudo chown root:root /usr/bin/chromedriver
sudo chmod +x /usr/bin/chromedriver
#
# Step 4 – Download Required Jar Files:


# Create selenium service
#
sudo cp ~/my/git/srto/selenium/setup/_etc_systemd_system_selenium.service /etc/systemd/system/selenium.service
sudo vi /etc/systemd/system/selenium.service
+ [Unit]
+ Description=Selenium Server
+
+ [Service]
+ EnvironmentFile=-/etc/default/selenium
+ User=<USER>
+ Group=<USER>
+ Environment="PERL5OPT=-d:NYTProf" "NYTPROF='trace=0:start=no:addpid=1:slowops=0'"
+ Environment=DISPLAY=:1
+ ExecStart=/usr/bin/java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar /usr/local/lib/selenium/current.jar $SELENIUM_OPTS
+ SuccessExitStatus=143
+
+ [Install]
+ WantedBy=graphical.target

# Selenium service environment file
#
sudo cp ~/my/git/srto/selenium/setup/_etc_default_selenium /etc/default/selenium
sudo vi /etc/default/selenium
+ SELENIUM_OPTS="-role standalone -debug"

# Enable selenium service (runs on login)
sudo systemctl enable selenium.service
sudo systemctl start selenium.service

# Run selenium test using curl (for debug)
curl -X POST http://localhost:4444/wd/hub/session -d '{ "desiredCapabilities": { "browserName": "chrome" } }'
curl -X POST http://localhost:4444/wd/hub/session -d '{ "desiredCapabilities": { "browserName": "firefox" } }'

# Type the Enter/Return key in Selenium.
perl -C -E 'say "\N{U+E007}"'   
perl -C -E 'say "\x{E007}"'     

# Make sure to use the apt firefox and not snap
# when seeing: Firefox profile not missing or not accessible.
sudo snap remove firefox
sudo add-apt-repository ppa:mozillateam/ppa
 echo '
Package: *
Pin: release o=LP-PPA-mozillateam
Pin-Priority: 1001
' | sudo tee /etc/apt/preferences.d/mozilla-firefox
echo 'Unattended-Upgrade::Allowed-Origins:: "LP-PPA-mozillateam:${distro_codename}";' | sudo tee /etc/apt/apt.conf.d/51unattended-upgrades-firefox


#############################################################
## SQLite3 Database
#############################################################

# Install sqlite on Unix (after in zipping the amalgamation file. make sure these 3 are present:
# shell.c, sqlite3.c, sqlite3.h). rename to a.out to sqlite3
# (database, sqlite3)



( run in 0.804 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )