Command doesn't work in script, but works in shell - linux

I'm writing a script for using SSH "profiles", ~/scripts/ssh-profiled.sh
PROFILE=`cat ~/script/ssh-profiles/$1`
echo [ssh $PROFILE]
ssh $PROFILE
~/scripts/ssh-profiles/tummi
-i ~/Dropbox/security/key-nopass/key-nopass.pvt bart#example.com
When I run the script, it fails:
bart#bart-laptop:~$ script/ssh-profiled.sh tummi
[ssh -i ~/Dropbox/security/key-nopass/key-nopass.pvt bart#example.com]
Warning: Identity file ~/Dropbox/security/key-nopass/key-nopass.pvt not accessible: No such file or directory.
bart#example.com's password:
But this works:
bart#bart-laptop:~$ ssh -i ~/Dropbox/security/key-nopass/key-nopass.pvt bart#example.com
Linux tummi 2.6.32-24-server #39-Ubuntu SMP Wed Jul 28 06:21:40 UTC 2010 x86_64 GNU/Linux
Ubuntu 10.04.1 LTS
Welcome to the Ubuntu Server!
Is there an error/gotcha in my script?

Change 1st line to
eval PROFILE=`cat ~/script/ssh-profiles/$1`
For explanation see here

The ~ in your file needs to be the full home directory path, it's not getting expanded.

My guess is the "~/" is not being interpreted as expected when passed in that way. Try using an explicit full path.

What are the permissions on the .pvt file? If only you have read access, and no one can execute it, then your script might not be able to even see the file. That could be why you are getting the "...not accessible: No such file or directory." message.

Related

Copying a remote file from Linux to a local server using scp

So, I'm attempting to simply transfer a unix file (demo) from a server to my local computer via ssh and scp. In Putty, I'm using the command as follows:
scp hasanjee#cs.utm.utoronto.ca:~/A1/demo \desktop
this works and even downloads:
demo 100% 4096 885.9KB/s 00:00
I've tried multiple directories:
scp hasanjee#cs.utm.utoronto.ca:~/A1/demo \desktop\demo
What am I missing here? Any ideas? I am using Windows 10
maybe try adding a full path...
scp -P 22 hasanjee#cs.utm.utoronto.ca:~/A1/demo/somefile C:\Users\{username}\Desktop\
-p: portnumber

"no such file or directory" after using SCP

I am using WSL Ubuntu-Bionic on my laptop and copied a file to a beaglebone (Debian-Stretch). Somehow it is shown by ls -l when I ssh onto the beagle, but if I try to run it, I get an error:
scp test debian#134.147.152.133:~/crosstest
ssh debian#134.147.152.133
ls -l
-rwxr-xr-x 1 debian debian 8936 Jan 8 09:50 crosstest
./crosstest
-bash: ./crosstest: No such file or directory
After searching around a bit, I found a solution:
As Romeo wrote, this had to have something todo with the file itself. I recompiled the file using arm-linux-gnueabihf-g++ instead of arm-linux-gnueabi-g++.
I found this after reading this SO post

connection closed in Linux server ssh

I am trying to connect to my Linux server.
After entering the password it is showing below output:
Last Logan: Mon Jun 24 12:22:48 2013 from xxx.xxx.xxx.xxx
/bin/bash: No such file or directory
Connection to xxx.xxx.x.xx closed.
How do I connect to the server?
you are trying to enter your server as a user privileges and you see /bin/bash file. When you chroot, you can reach /bin/bash directroy. And you can add your user in sudo.
Then you should see directory /home/username/bin/bash/
Edit :
When you chroot, the named directory becomes /. The correct shell path inside the chroot is then /bin/bash, not /home/username/bin/bash.
You will also need to make sure there's enough other stuff inside the chroot for the system to work. You can test this with sudo chroot /home/username /bin/bash and see what works and what doesn't
And also there is good information about chroot configuration
Your user is associated with an incorrect shell. The path to the associated shell "/bin/bash" doesn't exist on the system.
Correct your user's shell from the root or ask the administrator to do it.
Similar question: changing default shell in linux

Permission denied when running without bash

I have a script which calls some other bash, python, and ruby scripts. The script is similar to the following:
#!/bin/bash
set -x
./aux1.py
./aux2.sh
When I run the script from the command line, it runs but it gives me the following output:
./script.sh: line 5: ./aux1.py: Permission denied
The aux.py script has #!/usr/bin/env python at the top; when I call it from the command line with ./aux.py it is fine, however when called from this script in the exact same way I get this error. The strange part is that this only happens when I run the script with ./script.sh; when I run the script with bash script.sh the auxilliary scripts work perfectly.
I'm using Linux, why does this happen?
Update, 3 March 2014
There's nothing conflicting in the PATH, and all of the scripts are chmod +rx. uname -srvmo gives
Linux 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Dec 13 06:58:20 EST 2013 x86_64 GNU/Linux
and there's no more information available in uname -a. SELinux is enabled, but I'm not an administrator so I can't access that information.
Before launching the main script you must be sure that all the subscripts have got the right permissions. In particular you are interested in the permission for you (your user and owner of the files) to execute the file. This is a short tutorial on permissions.
http://blog.pluralsight.com/linux-file-permissions
http://www.linux.com/learn/tutorials/309527-understanding-linux-file-permissions
If this is the problem (and it look so), the simpler solution is to run the following commands
chmod u+x script.sh aux1.py aux2.sh
To check whether you have permission to execute simply run
ls -l
You should see something like
-rwxr--r-- 1 youruser youruser 26 Feb 28 21:44 script.sh
-rwxr--r-- 1 youruser youruser 32 Feb 28 21:44 aux1.py
where you should see a "x" in the 4th column corresponding to the file.
Maybe type sudo su before running your script so that you are the root user. When the script completes simply type exit to get out of root. Type exit again to close the shell.
:D

Executing db2 command through ssh

trying to ssh to another system then perform db2 commands however using 'su db2admin -c' does not seem to work, although it works for normal system commands ..
#!/bin/bash
sshpass -p 'passw0rd' ssh root#server.com "su db2admin -c 'db2text start'"
this is the output ..
rob#laptop:~/Desktop$ ./script.sh
bash: db2text: command not found
Any ideas?
The PATH is not getting updated to the normal root users PATH. Either specify the full path to db2text or add a dash (-) before the username to reload the environment variables
I'll hazard a guess and say that root doesn't have any of the db2 stuff in hi path.
And since you're using su db2admin rather than su - db2admin db2admin inherits
root's environment. Try with that extra - thrown in.
That all said: why on earth aren't you connecting w/ passwordless keys as db2admin?
Another solution that worked ..
#!/bin/bash
sshpass -p 'passw0rd' ssh root#server.com "su db2admin -c '~/sqllib/bin/db2text start'"
But problem is db2 path may change, better to use Eric's answer.

Resources