I have an AWS EC2 instance and I can ssh into it fine, but I want to use ssh -T so that my keys are shared for github access and so on, but ssh -T hangs after login.
Has anyone experienced this or any ideas about how to fix?
One thing I have done is stick verbose mode in .bashrc and it seems to get stuck after this output
+ export -f module
+ export -f switchml
+ '[' 5 -ge 3 ']'
+ [[ hxB =~ i ]]
+ [[ ! :/usr/local/cuda/bin:/opt/amazon/openmpi/bin/:/opt/amazon/efa/bin/:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin: =~ :/usr/bin: ]]
+ '[' '!' -n x ']'
++ manpath
+ [[ ! :/usr/local/man:/usr/local/share/man:/usr/share/man:: =~ :/usr/share/man: ]]
+ unset _mlcode _mlret
+ '[' -n '' ']'
one thing I notice is that with -T the $PS1 appears to be different and so the first line of the bashrc file sources an additional file:
[ -z "$PS1" ] && source /etc/profile.d/dlami.sh
but I'm not really sure how that's all related ...
okay, so this was all a confusion about ssh flags - should have been ssh -a
Related
I have written the following Bash script. Its role is to check its own name, and in case of nonexistent extension , to amend ".sh" with sed. Still I have error "missing target file..."
#!/bin/bash
FILE_NAME="$0"
EXTENSION=".sh"
FILE_NAME_MOD="$FILE_NAME$EXTENSION"
if [[ "$0" != "FILE_NAME_MOD" ]]; then
echo mv -v "$FILENAME" "$FILENAME$EXTENSION"
cp "$0" | sed 's/\([^.sh]\)$/\1.sh/g' $0
fi
#!/bin/bash
file="$0"
extension=".sh"
if [ $(echo -n $file | tail -c 3) != $extension ]; then
mv -v "$file" "$file$extension"
fi
Important stuff:
-n flag suppress the new line at the end, so we can test for 3 chars instead of 4
When in doubt, always use set -x to debug your scripts.
Try this Shellcheck-clean code:
#! /bin/bash -p
file=${BASH_SOURCE[0]}
extension=.sh
[[ $file == *"$extension" ]] || mv -i -- "$file" "$file$extension"
See choosing between $0 and BASH_SOURCE for details of why ${BASH_SOURCE[0]} is better than $0.
See Correct Bash and shell script variable capitalization for details of why file is better than FILE and extension is better than EXTENSION. (In short, ALL_UPPERCASE names are dangerous because there is a danger that they will clash with names that are already used for something else.)
The -i option to mv means that you will be prompted to continue if the new filename is already in use.
See Should I save my scripts with the .sh extension? before adding .sh extensions to your shell programs.
Just for fun, here is a way to do it just with GNU sed:
#!/usr/bin/env bash
sed --silent '
# match FILENAME only if it does not end with ".sh"
/\.sh$/! {
# change "FILENAME" to "mv -v FILENAME FILENAME.sh"
s/.*/mv -v & &.sh/
# execute the command
e
}
' <<<"$0"
You can also make the above script output useful messages:
#!/usr/bin/env bash
sed --silent '
/\.sh$/! {
s/.*/mv -v & &.sh/
e
# exit with code 0 immediately after the change has been made
q0
}
# otherwise exit with code 1
q1
' <<<"$0" && echo 'done' || echo 'no changes were made'
I'm trying to do a bulk reverse DNS the IP's which I got from CloudFront Logs with help of below shell script, which I need to differentiate whether it's suspicious or genuine Googlebot IP. But I'm not getting the output. Not sure where I am going wrong. Is there any other option to do bulk reverse DNS?
#!/bin/sh
file="googlebots"
while read -r line
do
hostName=`host $line | cut -d" " -f 5`
domainName=`echo $hostName | cut -d"." -f2,3`
#echo $domainName
#echo "$hostName"
hostIp=`host $hostName | cut -d" " -f 4`
#echo "$hostIp"
if [ $line == $hostIp ] && [ $domainName == "googlebot.com" ]
then
echo "Googlebot: $hostIp -> $hostName"
fi
done < "$file"
I'm trying to develop a bash setup script that includes mounting and migrating a boot drive. I've got most of it working, but would like to populate my /boot/cmdline.txt and fstab files with drive UUID and PARTUUID numbers.
I basically set a variable with the output of blkid:
disk=$(blkid)
echo "${disk}"
RESULT:
/dev/mmcblk0p1: LABEL_FATBOOT="boot" LABEL="boot" UUID="69D5-9B27" TYPE="vfat" PARTUUID="d9b3f436-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="24eaa08b-10f2-49e0-8283-359f7eb1a0b6" TYPE="ext4" PARTUUID="d9b3f436-02"
/dev/sda1: LABEL="usbfs" UUID="493b6467-7b7b-4291-a86d-dea5e842780b" TYPE="ext4" PARTUUID="83122dbb-cacf-4612-9be2-4301a03e8093"
/dev/mmcblk0: PTUUID="d9b3f436" PTTYPE="dos"
My goal is to set one variable to capture the /dev/sda1 value for UUID and the other for the same drives PARTUUID. My basic premise is to do something like this (based on being able to do this in python:
#sudo code#
Disk=diskInfo
While line in Disk; do
If Line contains /dev/sda1
Then
Do some Regex to set vUUID = "493b6467-7b7b-4291-a86d-dea5e842780b"
Do some Regex to set vPARTUUID = "83122dbb-cacf-4612-9be2-4301a03e8093"
I think I want something like this - - but can't get it to work:
disk=$(blkid)
while read line; do
if [[ $line == '/dev/sda1'* ]]; then
if [[ $line =~ UUID=(["'])(?:(?=(\\?))\2.)*?\1 ]]; then #captures too much
vUUID=${BASH_REMATCH[1]}
fi
if [[ $line =~ PARTUUID=(["'])(?:(?=(\\?))\2.)*?\1 ]]; then #captures too much
vPARTUUID=${BASH_REMATCH[1]}
fi
fi
done <<< "$disk"
You don't need a loop here.
$ IFS=\" read -r _ vUUID _ vPARTUUID _ < <(blkid /dev/sda1 -s UUID -s PARTUUID)
$
$ echo $vUUID
9099-AD46
$
$ echo $vPARTUUID
90afc43c-5b4d-4721-b82a-000e585fef62
If there is no such disk read will silently fail with a non-zero exit status; so you can use it as a condition in an if-else expression.
You are almost there.
Would you please try:
pat='^/dev/sda1.* UUID="([^"]+)".* PARTUUID="([^"]+)"'
while IFS= read -r line; do
if [[ $line =~ $pat ]]; then
vUUID="${BASH_REMATCH[1]}"
vPARTUUID="${BASH_REMATCH[2]}"
fi
done < <(blkid)
result:
echo "vUUID=$vUUID"
vUUID=493b6467-7b7b-4291-a86d-dea5e842780b
echo "vPARTUUOID=$vPARTUUID"
vPARTUUOID=83122dbb-cacf-4612-9be2-4301a03e8093
Hope this helps.
I have a script that reads a set of numeric file permissions as argument it works like this:
$ bash script 755
This will give 755 permissions to some file on the disk. I want to know how to check if the number passed as parameter contains a valid set of permissions. Any ideas?
thanks
You can use this regex:
[0-7]{3}
https://regex101.com/r/bQ8jB6/1
if you prefer:
^[0-7]{3}$
https://regex101.com/r/bQ8jB6/2
chmod returns an error code and visible error text to the shell if you pass chmod invalid permissions, e.g.,
> chmod 788 some/file
chmod: Invalid file mode: 788
so
chmod $1 some/file || <handle the error>
in your script will either chmod successfully, or else handle the error however you like.
Or, more verbosely:
chmod $1 some/file
if [ $? != 0 ]; then
<handle the error>
fi
If, for some unexplained reason you, prefer a pure regex answer before attempting to chmod, a modified version of #siete.sh's will work, e.g.,
[[ "$1" =~ ^[0-7]{3}$ ]] && chmod $1 some/file
or, since 1 to 5 digits are all valid arguments to chmod:
[[ "$1" =~ ^[0-7]{1,5}$ ]] && chmod $1 some/file
I having a program that successfully uploads all of the files that I need. I have new files everyday that I need to upload. After I have uploaded the files I no longer need them and thus am not looking to sync them.
I am curious if there is a way to check if given a path and file name if that exists within S3 using the s3cmd.
You can use the ls command in s3cmd to know if a file is present or not in S3.
Bash code
path=$1
count=`s3cmd ls $path | wc -l`
if [[ $count -gt 0 ]]; then
echo "exist"
else
echo "do not exist"
fi
Usage: ./s3_exist.sh s3://foo/bar.txt
Edit:
As cocoatomo pointed out in comments, s3cmd ls $path lists all file that begins with $path. A safer approach would be to use s3cmd info $path and check the exit code.
New Bash code
path=$1
s3cmd info $path >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
echo "exist"
else
echo "do not exist"
fi
Assuming that bar.txt and bar.txt.bak exist in a bucket s3://foo, "s3cmd ls s3://foo/bar.txt" shows a following output.
$ s3cmd ls s3://foo/bar.txt
2013-11-11 11:11 5 s3://foo/bar.txt
2013-11-11 11:11 5 s3://foo/bar.txt.bak
Since we should remove 2nd line from the command result, we use "awk" command to filter unnecessary lines.
$ filename=s3://foo/bar.txt
$ s3cmd ls ${filename} | awk "\$4 == \"${filename}\" { print \$4 }"
2013-11-11 11:11 5 s3://foo/bar.txt
Finally, we build up all commands.
filename=s3://foo/bar.txt
count=$(s3cmd ls ${filename} | awk "\$4 == \"${filename}\" { print \$4 }" | wc -l)
if [ $count -eq 0 ]; then
echo "file does not exist"
else
echo "file exists"
fi
In the newer version of AWS CLI, you can use the following code to detect the existence of a file or directory
count=$(aws s3 ls $path | wc -l)
if [ $count -gt 0 ]
then
(>&2 echo "$path already exists!")
return
fi
We can use s3cmd ls , Take one flag flag_exists true if file is there and false if file is not there.
FLAG_EXISTS=false
for j in $(s3cmd ls s3://abc//abc.txt); do
if [[ "$j" == "s3://abc//abc.txt" ]]; then
FLAG_EXISTS=true
break
fi
done
if [ "$FLAG_EXISTS" = false ]; then
echo 'file not exists'
else
echo 'file exists'
fi
Explanation - Since ls can return many values like if u search for s3cmd ls abc.txt , then it can return values like abc.txt abcd.txt and so on , so looping and checking using if condition if file exists.