Tool / command to query debian repository (like repoquery for rpm repository) - linux

Im basically trying to query the latest version of a package given a repository url & package name.
So if http://in.archive.ubuntu.com/ubuntu/ is repository & gcc is package name then is there a command / tool which will help me find the latest version of it available?
i tried using apt-get & apt-cache
the problem is i do not want to add the repository url into /etc/apt/sources.list of the box since i dont have root access.
i tried running
apt-get update -o RootDir=<local-dir>
thinking i could then add the repository url into < local-dir >/etc/apt/sources.list & run
apt-cache show <pkg-name> -o RootDir=<local-dir>
but the apt-get update with a switched root-dir fails saying
chrooting into <local-dir>/
E: Sub-process returned an error code
Is this possible? or is there an alternative?

I don't know if there is an "official" way, but it's easy to write your own script that parses the package list:
#!/bin/bash
if [ -n "$1" ]
then
package="$1"
else
package="gcc"
fi
release=$(lsb_release -c|awk '{ print $2 }')
file=$(tempfile)
wget -o /dev/null -O $file http://in.archive.ubuntu.com/ubuntu/dists/$release/main/binary-amd64/Packages.gz
gunzip -qc $file | grep -A 10 "^Package: $package$" | awk '/Version/ { print $2 }'
rm $file
This works if you are running on Ubuntu and are looking for the packages matching your current version. If you're running on another distro, set
release="saucy"
for example.
Have fun!

Related

How to match two strings from separate lists - Posix Shell

I have a POSIX shell script that tries to find manual install paths for tomcat on an ubuntu (20.04) system. I run dpkg-query to make a list of installs that were installed using the package manager. Then I iterate over a list of common install paths. If any of the installs found using dpkg-query match, I need to exclude those from my discovery. But, I can't seem to figure out an efficient way to do this.
Code:
#/bin/sh
APP="Apache Tomcat"
# Common install paths to iterate over
set -- "/opt/" "/usr/share/" "/var/" "/var/lib/" "/usr/" "/usr/local/"
# Create exclusion list for managed installs from apt and yum
EXCLUSION_LIST=""
if [ -x "$(command -v dpkg-query)" ]; then
dpkg_check=$(dpkg-query -l | grep -Eo 'tomcat[0-9]?' | sort -u)
EXCLUSION_LIST=$dpkg_check
echo $EXCLUSION_LIST
# Check for manual installs
for _i in "$#"; do
find_tomcat=$(ls $_i | grep -E '^tomcat[0-9]{0,2}$')
excluded_list=$(echo $EXCLUSION_LIST)
for match in $excluded_list; do
if [ $find_tomcat == $match ]; then
echo "$match is excluded"
else
echo $_i$find_tomcat
done
done
else
echo "DPKG not found."
fi
Desired Output:
Install path of manual version not installed by the package manager.
E.g., /usr/share/tomcat
Observations:
The first line needs to start literally with the two characters #! to be a valid shebang
You are needlessly copying the same strings to multiple variables, in one place with a useless echo
Don't use upper case for your private variables; see Correct Bash and shell script variable capitalization
When to wrap quotes around a shell variable?
Don't parse ls output. Instead, I propose looping over wildcard matches in this case.
Your current logic seems to be basically saying, "if tomcat8 was installed by dpkg, don't print any tomcat8 paths." I'm guessing you actually want to avoid printing paths which are installed by a dpkg-installed tomcat* package, and print others.
Your dpkg-query -l command is slightly inexact, in that it will find not just package names, but also package descriptions which contain the search string. You can use dpkg-query -W instead, to only print package names and versions.
dpkg -L prints the actual paths installed by a set of packages.
Untested, but hopefully at least a step in the right direction.
#!/bin/sh
# Unused variable
# APP="Apache Tomcat"
if [ -x "$(command -v dpkg-query)" ]; then
exclusion_list=$(dpkg-query -W | sed -n 's/^\(tomcat[0-9]*\)\t[^\t].*/\1/p' | sort -u)
echo "$exclusion_list"
# Check for manual installs
for _i in "/opt" "/usr/share" "/var" "/var/lib" "/usr" "/usr/local"; do
for found in "$_i"/tomcat*; do
case $found in
*/tomcat | */tomcat[0-9] | */tomcat[0-9][0-9] ) ;;
*) continue;;
esac
found_dir=${found%/*}
if dpkg -L $exclusion_list | fgrep -qx "$found_dir"
then
echo "$match is excluded"
else
echo "$found"
fi
done
done
else
echo "DPKG not found."
fi

a weird ssh file appearing from nowhere with name pbcopyfy in .ssh directory

Well not a question as such, but more of a brainstorming thing or better say an issue. Well today I came to my server and realised that i can't git pull due to the keys being denied which was okay because for sure there were no keys in .ssh directory. All deleted and a new file (pbcopyfy there). Not sure what this means though. The content of the file are shown below:
#!/bin/sh
# Copyright (C) 2009-2017 Three Nine Consulting
# Always good practice to update packages. However ask user if they would like to do so
# For explanation on how this works and why check out https://garywoodfine.com/use-pbcopy-on-ubuntu/
read -p "Do you want to update your package repositories before proceeding ? " -n 1 -r
echo #adding new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
sudo apt update
sudo apt upgrade -y
sudo apt autoremove -y
fi
# Check to see if Xclip is installed if not install it
if [ $(dpkg-query -W -f='${Status}' xclip 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
echo 'xclip not installed .... installing now!'
sudo apt install xclip -y;
fi
# Add the aliases to the .bashrc
echo 'updating bash profile'
echo "#pbcopy & pbpaste aliases" >> ~/.bashrc
echo "alias pbcopy='xclip -selection clipboard'" >> ~/.bashrc
echo "alias pbpaste='xclip -selection clipboard -o'" >> ~/.bashrc
source ~/.bashrc
echo 'Complete! happy PbCopy'
Is this something I should be worried about or should I just get new keys and go up and running with new keys?

Non-root users Installing packages from a rpm file

I'm a non-root user running centos Linux.....the problem is i have the desired rpm file i don't how to install them into my home directory being a non-root user.Any help would be appreciated.thanks in advance
This nice little script ( unPack_rpm.sh ) will unpack rpm packages to a folder with the package name ...
#!/bin/bash
# Purpose: Unpack RPM archives (author: #unSpawn)
# Args: /path/to/archive
# Deps: Bash, GNU utils, RPM
rpmDetails() { for Q in changelog provides requires scripts triggers triggerscripts; do
rpm -q -p --${Q} "${f}" 2>&1 | grep -v NOKEY > "${Q}.log"; done; }
rpmUnpack() { f=$(readlink -f "${f}"); file "${f}"|grep -q "RPM.v" && \
{ d=$(basename "${f}" .rpm); d="./${d:=ERROR_$$}"; mkdir -p "${d}" && \
{ cd "${d}" && rpm2cpio "${f}" | cpio -idmv && rpmDetails "${f}"; }; }; }
for f in $#; do rpmUnpack "${f}"; done
exit 0
I think this is somewhat a duplicate question, the same as the one here. In this thread, it is suggested to extract the rpm (using conversion to cpio) and then use the source files locally.
A link to an in-depth explenation for this is also supplied, you can see it here.

How to create an offline repository for debian non-free?

I am using debian squeeze and want to create an offline repository or a cd/dvd for the debian non-free branch. I looked around the internet and all i found out is that there are neither iso images nor there are jidgo files for creating such image so I had the idea to fetch the packages from one of the debian package servers using:
wget -r --no-parent -nH -A*all.deb,*any.deb,*i386.deb \
ftp://debian.oregonstate.edu/debian/pool/non-free/
I know that that I must use file: in my */etc/apt/sources.list* to indicate local repositories but how do I actually create one so that apt or aptitude understands this?
(Answered in a questioned edit. Converted to a community wiki answer. See What is the appropriate action when the answer to a question is added to the question itself? )
The OP wrote:
Update: With a few ugly tricks I was able to extract the needed data from pool and the dist folder.
I used the unzipped Package.gz to do this:
grep '^Package\:.*' Packages|awk '{print $2}' >> Names.lst
grep '^Version\:.*' Packages|awk '{print $2}' >> Versions.lst
grep '^Architecture\:.*' Packages|awk '{print $2}' >> Arch.lst
With vim I find and remove the ':' in the file Versions.lst and generate a shorter Content.lst more easy to parse with bash tools:
paste Names.lst Versions.lst Arch.lst >> Content.lst
Now I do this:
cat content.lst | while read line; \
do echo "$(echo $line|awk '{print $1}')\
_$(echo $line|awk '{print $2}')_$(echo $line|awk '{print $3}')";\
done >> content.lst.tmp && mv content.lst.tmp content.lst
which generates me the file names in the debian directory I need. When finishing with my downloads using wget I find and rsync the needed file names. mv does not work here because I needed the structure as it is referring to in Packages.gz:
cat content.lst |while read line; \
do find debian/ -type f -name ${line}.deb -exec \
rsync -rtpog -cisR {} debian2/ \; ;done
rm -r debian && mv debian2 debian
To receive the complete dists tree structure I used wget again:
wget -c -r --no-parent -nH -A*.bz2,*.gz,Release \
ftp://debian.oregonstate.edu/debian/dists/squeeze/non-free/binary-i386/
I think the only thing I have to do now is to create the Contents.gz file.
The Contents.gz file can easily be created using the apt-ftparchive program:
apt-ftparchive contents > Contents-i386 && gzip -f Contents-i386

CentOS directory structure as tree?

Is there an equivalent to tree on CentOS?
If tree is not installed on your Centos system (I typically recommend server setups to use minimal install disk anyhow) you should type the following at your command line:
# yum install tree -y
If this doesn't install it's because you don't have the proper repository. I would use the Dag Wieers repository:
http://dag.wieers.com/rpm/FAQ.php#B
After that you can do your install:
# yum install tree -y
Now you're ready to roll. Always read the man page: http://linux.die.net/man/1/tree
So quite simply the following will return a tree:
# tree
Alternatively you can output this to a text file. There's a ton of options too.. Again, read your man page if you're looking for something other than default output.
# tree > recursive_directory_list.txt
(^^ in a text file for later review ^^)
You can make your own primitive "tree" ( for fun :) )
#!/bin/bash
# only if you have bash 4 in your CentOS system
shopt -s globstar
for file in **/*
do
slash=${file//[^\/]}
case "${#slash}" in
0) echo "|-- ${file}";;
1) echo "| |-- ${file}";;
2) echo "| | |-- ${file}";;
esac
done
As you can see here. tree is not installed by default in CentOs, so you'll need to look for an RPM and install it manually
Since tree is not installed by default in CentOS ...
[user#CentOS test]$ tree
-bash: tree: command not found
[user#CentOS test]$
You can also use the following ls command to produce almost similar output with tree
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
Example:
[user#CentOS test]$ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
.
|-directory1
|-directory2
|-directory3
[user#CentOS directory]$
You have tree in the base repo.
Show it (yum list package-name):
# yum list tree
Available Packages
tree.i386 1.5.0-4 base
Install it:
yum install tree
(verified on CentOS 5 and 6)
I need to work on a remote computer that won't allow me to yum install. So I modified bash-o-logist's answer to get a more flexible one.
It takes an (optional) argument that is the maximum level of subdirectories you want to show. Add it to your $PATH, and enjoy a tree command that doesn't need installation.
I am not an expert in shell (I had to Google a ton of times just for this very short script). So if I did anything wrong, please let me know. Thank you so much!
#!/bin/bash
# only if you have bash 4 in your CentOS system
shopt -s globstar # enable double star
max_level=${1:-10}
for file in **
do
# Get just the folder or filename
IFS='/'
read -ra ADDR <<< "$file"
last_field=${ADDR[-1]}
IFS=' '
# Get the number of slashes
slash=${file//[^\/]}
# print folder or file with correct number of leadings
if [ ${#slash} -lt $max_level ]
then
spaces=" "
leading=""
if [ "${#slash}" -gt 0 ]
then
leading=`eval $(echo printf '"|${spaces}%0.s"' {1..${#slash}})`
fi
echo "${leading}|-- $last_field"
fi
done

Resources