./script.sh: line 8: /etc/passwd: Permission denied - bash

I have this script which I can't execute:
#!/bin/bash
USERS="/etc/passwd"
for user in `$USERS | cut -f 1 -d ':'`
do
echo $user
done
This is the output of ls -l script.sh:
-rwxrwxrwx 1 user user 94 Jul 30 21:24 script.sh
What am I doing wrong? :|
I also tried running it as root and with sudo and nothing worked...it's annoying...

You're trying to execute /etc/passwd and send the output to cut. You want to redirect the contents of the file:
for user in `cut -f 1 -d : < $USERS`

Related

Process substitution not working with sudo

From a main bash script run as root, I want to execute a subprocess using sudo as unpriviledge user nobody; that subprocess should source a file, which content is provided by the main script.
I am trying to solve this using bash process substitution. But I cannot manage to get this to work.
Can someone tell me why the following script, ...
#! /bin/bash
sudo -u nobody \
bash -c 'source /dev/stdin || ls -l /dev/stdin /proc/self/fd/0 /proc/$$/fd/0; echo "A=$A"' \
< <(echo "A=$(ls /root/.profile)")
... when run as root, produces the following ouput ?
root#raspi:~# ./test3.sh
bash: line 1: /dev/stdin: Permission denied
lrwxrwxrwx 1 root root 15 Mar 20 20:55 /dev/stdin -> /proc/self/fd/0
lr-x------ 1 nobody nogroup 64 Aug 21 14:38 /proc/3243/fd/0 -> 'pipe:[79069]'
lr-x------ 1 nobody nogroup 64 Aug 21 14:38 /proc/self/fd/0 -> 'pipe:[79069]'
A=
I would expect reading from stdin to work because, as indicated by ls -l, read access to stdin is granted to nobody (which makes sense).
So why this does not work ? And is there any way to get this to work ?
Answers to this question did not help: as sample above shows, code in the <(...) bloc should access data that only root can.
To see why you have Permission denied, use ls -lL
sudo -u nobody \
bash -c 'source /dev/stdin || ls -lL /dev/stdin /proc/self/fd/0 /proc/$$/fd/0; echo "A=$A"' \
< <(echo "A=$(ls /root/.profile)")
To get around the error, use cat |
sudo -u nobody \
bash -c 'cat | { source /dev/stdin || ls -lL /dev/stdin /proc/self/fd/0 /proc/$$/fd/0; echo "A=$A"; }' \
< <(echo "A=$(ls /root/.profile)")

Check if file exists remotly in bash

I have a bashscript which checks if a file exists on the remote server.
When i execute this bashscript on commandline it works fine and say to me the file exist (as it should). But when crontab is executing this bashscript it says that the file not exist (although it would exist
).
can anybody help me?
myscript.sh
#!/bin/bash
if $(sudo ssh -i <path/to/ssh/keys> <user>#<ip> "[[ -f /etc/ssl/file.txt ]]");then
echo "exist"
else
echo "not exist"
fi
crontab:
*/1 * * * * bash /home/user/myscript.sh | mail -s "betreff" user#email.com
stderr: (when i run the script on the commandline)
++ sudo ssh -i <path/to/ssh/keys> <user>#<ip> '[ -f /etc/ssl/file.txt ]'
+ echo exist
exist
stderr: (when i run the script in cron)
++ sudo ssh -i <path/to/ssh/key> <user>#<ip> '[ -f /etc/ssl/file.txt ]'
Warning: Identity file /root/.ssh/key/keyfile not accessible: No such file or directory.
Permission denied, please try again.
Permission denied, please try again.
root#<ip>: Permission denied (publickey,password).
Permission of ssh keyfile:
-rw------- 1 root root 3243 Sep 30 15:34 keyfile
-rw-r--r-- 1 root root 741 Sep 30 15:34 keyfile.pub
Thanks for helping :D

Looping through file to create other command files

I am trying to create a script that will automatically log me in to a specific remote device (let's call it a fw). The "command" is a bit elaborate, as we are logging in from a protected network server, and there are hundreds of these to login to.
I have created a file with two parameters (command and name) separated by "#", the first parameter is the "command" string with spaces (ie: "sudo --user ....") which I want to put (echo) into an executeable file called "name" (name of the device I want to login to).
My logic was originally:
for line in $(awk -F# '{print $1, $2}' list.txt), do touch $2; && echo "$1 > $2" && chmod +x $2; done
my end should create x number of files named "$name", each with only a one-line command of "$command" and be "executeable".
I have tried several things to make this work. I can iterate of the file with not much issue using for, while, and even [[ -n $name ]], but, this only provides me with one variable and doesn't split the line into the two I need, "$command" and "$name". Even $1 and $2 would be fine for my purposes...
While testing:
$ while IFS=# read -r line; do echo "$line"; done < list
sudo --user xxxxxxxxxxxxxx#yyyyyyyyy
sudo --user xxxxxxxxxxxxxx#yyyyyyyyy
sudo --user xxxxxxxxxxxxxx#yyyyyyyyy
even using IFS=# to split the $line - doesn't remove the "#" as expected.
for-looping:
$ for line in $(cat list); do echo $line; done
sudo --user xxxxxxxxxxxxxx
yyyyyyyyy
sudo --user xxxxxxxxxxxxxx
yyyyyyyyy
sudo --user xxxxxxxxxxxxxx
yyyyyyyyy
Trying to expand to:
bin$ for line in $(cat list); do awk -F# '{print $1, $2}' $line; done
awk: fatal: cannot open file ` xxxxxxxxxxxxxxxxx' for reading (No such file or directory)
awk: fatal: cannot open file `yyyyyyyyy
sudo --user xxxxxxxxxxxxxxxxx' for reading (No such file or directory)
awk: fatal: cannot open file `yyyyyyyyy
sudo --user xxxxxxxxxxxxxxxxx' for reading (No such file or directory)
I would like to parse (loop) through the file - separate the parms and create $name with $command inside and chmod +x $name to have an executeable that will log me in automatically to "$name" node.
I suggest inserting all your logic into the awk script.
script.awk
BEGIN {FS = "[\r#]"} # set field separator to # or <CR>
{ # for each input line
print $1 > $2; # write input 1st field to file named 2nd field
system("chmod a+x "$2); # set file named 2nd field, to be executable
}
running the script:
awk -f script.awk list.txt
input list.txt
sudo --user xxxxxxxxxxxxxx#yyyyyyyy1
sudo --user xxxxxxxxxxxxxx#yyyyyyyy2
sudo --user xxxxxxxxxxxxxx#yyyyyyyy3
output:
dudi#DM-840$ ls -l yy*
total 3
-rwxrwxrwx 1 dudi dudi 28 Jun 23 01:21 yyyyyyyy1*
-rwxrwxrwx 1 dudi dudi 28 Jun 23 01:21 yyyyyyyy2*
-rwxrwxrwx 1 dudi dudi 28 Jun 23 01:21 yyyyyyyy3*
update:
changed FS to include the <CR> char, otherwise appended to filenames (seen as ^M).

While_Run_For_loop_In_bash-Permission-denied

When I'm executing the bash script it said permission denied on a line. below the script and other details.
#!/bin/bash
find /var/opt/gitlab/backups/ -amin +60 |grep tar | cut -d '/' -f 6 >
/tmp/delete-files.txt
chmod +rw /var/opt/gitlab/backups/*.tar
chmod +rw /tmp/delete-files.txt
for i in `/tmp/delete-files.txt`
do
rm -rf /var/opt/gitlab/backups/$i
[root#git opt]# ./asaaa
./asaaa: line 10: /tmp/delete-files.txt: Permission denied'
[root#git opt]#
[root#git opt]# ll /tmp/delete-files.txt
-rw-r--r--. 1 root root 100 Mar 11 12:43 /tmp/delete-files.txt
[root#git opt]#
Help me to sort out this issue.
This line is incorrect:
for i in `/tmp/delete-files.txt`
Backticks mean command substitution. Your script will try to execute /tmp/delete-files.txt. This is not an executable file.
My guess is that what you wanted to do was:
for i in `cat /tmp/delete-files.txt`
Ie. execute cat command to print the contents of the /tmp/delete-files.txt and then loop through each of the printed lines in the for loop.

FTP not working UNIX

hi i have a script where i am performing sudo and going to particular directory,and within that directory editing files name as required. After getting required file name i want to FTP files on windows machine but script after reading FTP commands says-:
-bash: line 19: quote: command not found
-bash: line 20: quote: command not found
-bash: line 21: put: command not found
-bash: line 22: quit: command not found
My ftp is working if i run normally so it is some other problem.Script is below-:
#!/usr/bin/
path=/global/u70/glob
echo password | sudo -S -l
sudo /usr/bin/su - glob << 'EOF'
#ls -lrt
cd "$path"
pwd
for entry in $(ls -r)
do
if [ "$entry" = "ADM" ];then
cd "$entry"
FileName=$(ls -t | head -n1)
echo "$FileName"
FileNameIniKey=$(ls -t | head -n1 | cut -c 12-20)
echo "$FileNameIniKey"
echo "$xmlFileName" >> "$xmlFileNameIniKey.ini"
chmod 755 "$FileName"
chmod 755 "$FileNameIniKey.ini"
ftp -n hostname
quote USER ftp
quote PASS
put "$FileName"
quit
rm "$FileNameIniKey.ini"
fi
done
EOF
You can improve your questions and make them easier to answer and more useful for future readers by including a minimal, self-contained example. Here's an example:
#!/bin/bash
ftp -n mirrors.rit.edu
quote user anonymous
quote pass mypass
ls
When executed, you get a manual FTP session instead of a file listing:
$ ./myscript
Trying 2620:8d:8000:15:225:90ff:fefd:344c...
Connected to smoke.rc.rit.edu.
220 Welcome to mirrors.rit.edu.
ftp>
The problem is that you're assuming that a script is a series of strings that are automatically typed into a terminal. This is not true. It's a series of commands that are executed one after another.
Nothing happens with quote user anonymous until AFTER ftp has exited, and then it's run as a shell command instead of being written to the ftp command.
Instead, specify login credentials on the command line and then include commands in a here document:
ftp -n "ftp://anonymous:passwd#mirrors.rit.edu" << end
ls
end
This works as expected:
$ ./myscript
Trying 2620:8d:8000:15:225:90ff:fefd:344c...
Connected to smoke.rc.rit.edu.
220 Welcome to mirrors.rit.edu.
331 Please specify the password.
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
200 Switching to Binary mode.
229 Entering Extended Passive Mode (|||19986|).
150 Here comes the directory listing.
drwxrwxr-x 12 3002 1000 4096 Jul 11 20:00 CPAN
drwxrwsr-x 10 0 1001 4096 Jul 11 21:08 CRAN
drwxr-xr-x 18 1003 1000 4096 Jul 11 18:02 CTAN
drwxrwxr-x 5 89987 546 4096 Jul 10 10:00 FreeBSD
ftp -n "ftp://anonymous:passwd#mirrors.rit.edu" << end
Name or service not known

Resources