unable to execute multiple commands in a shell script - bash

I am new to unix and scripting, need your help for the below scenario.
These are the contents of my .sh file
#!/bin/bash
usrun xyz
whoami
When I am calling this bash file from putty its asking me for my xyz user's password some other information to properly log in the xyz user.
After successfully login, the $ sign in putty changes to #### xyz$, so I am guessing its opening a new session for the xyz user.
However, after that, the whoami command is not getting executed. Only after I type exit the whoami command is getting executed.
why is this happening? How to execute the whoami command after successful authentication of xyz user?
The #### represents the last four digits of my server to which I am currently connected to via putty.

The usrun command without any parameters blocks the execution of the bash script. Thus, until the command is not finished (when you type exit), the next command (whoami) is not executed.
If you want to login into the machine and execute a command you should try:
#!/bin/bash
usrun -u xyz whoami
The -u option allows you to specify the user and next you can provide the command to execute.
If you want to execute more than one command in the remote machine using Putty I suggest you to follow something similar to this post:
https://superuser.com/questions/1103873/how-to-type-commands-in-putty-by-creating-batch-file

Related

How to invoke an interactive bash shell as another user with initial commands?

I currently have a RemoteCommand, RemoteCommand sudo su - admin-user set up in my ssh config that allows me to connect to a system and immediately switch users to an admin user. This is because I must only make changes to the system as that admin-user, the admin-user does not have a password, and I am not allowed to add my public key to the system to connect without specifying a password. This works perfectly; I'm able to login as "login-user", and it immediately switches me to the admin-user.
However, I want to do the same, and create a temporary function that would allow me to run a common command in a more shorthand manner. This is because I am not allowed to change the admin-user's bashrc.
My thought in setting this up was to do the following in my ssh config:
HOST SYSTEM
Hostname 12.34.56.78
User login-user
RemoteCommand sudo -Hu admin-user /usr/bin/bash --init-file <(echo ". ~/bashrc; function testy() { ls ; }") -li
...
In this case, I'm just testing with a function that runs ls. Just using a named pipe to source the normal bashrc and add a func to the new shell via an init file/rc file.
This creates an interactive bash shell as the admin-user as expected, but upon trying to run the function testy in this shell, I get bash: testy: command not found. Doing the same without switching users in the same step works, but not if I add the flags to run the shell as admin-user. I can't figure out how to get this working. Any help using this approach or another is greatly appreciated!
Likely a named pipe sharing issue. You can use another wrapper shell:
HOST SYSTEM
Hostname 12.34.56.78
User login-user
RemoteCommand sudo -Hiu admin-user /usr/bin/bash -c 'exec /usr/bin/bash --init-file <(echo ". ~/bashrc; function testy() { ls ; }")'
...
. ~/.bashrc also likely can be unnecessary but that's besides the main point.

Run SSH on Jenkins from file in Git

I have a file on Git with the following shell script
File Name = Job.sh
echo "Warehouse script starting"
ssh -n username#server_name "mkdir -p ~/directory_name/folder_name/file_name"
Under Execute Shell in Jenkins I am running -
sh job.sh
The echo command gets printed in the console. But the job is not doing ssh into username#server_name and creating the directory. Appreciate any feedback.
Try the same command when connected directly to the Jenkins agent (using the account used by Jenkins on that agent)
The goal is to check if that user (on that server agent) does have the right pubic/private key pair in its $HOME/.ssh folder.
And double-check the public key is in servername:~username/.ssh/authorized_keys.
Make sure the private key is not encrypted, to avoid having to deal with ssh-agent and passphrase caching. At least for now, for testing your setup.
Note: the -n option (preventing to read from stdin) is usually for ssh commands executed in the background. You might not need it in your case.
Try also to add #!/bin/bash -x at the beginning of your script (assuming you do have a bash) in order to print all lines executed.

How to return to the script once sudo is done

I am on a server and running a script file that has following code.
ssh username#servername
sudo su - root
cd /abc/xyz
mkdir asdfg
I am able to ssh... but then the next command is not working.. the script is not sudo-ing. any idea?
Edit: Able to create a mech id and then do the things.. though still looking for the answer to above question :|
First of all your command will "stuck" on the first line because it will go into an interactive mode. The ssh command will require a password to be provided by a user (unless there is an sshkey being used) . And if the ssh is logged into the remote server then it will wait for user commands from standard input.
Secondly the lines following the ssh command will be executed only when the first process has exited. This is why your script is not "sudoing" - it's waiting for the ssh to end.
So if your point is to run a command on a remote server then put the command as a parameter into the same line as ssh connection. In your case:
ssh user#server sudo su - root
But this will not be of satisfaction for you. I suggest you create a script of what you want to execute on the remote server and then execute the script.
ssh user#server scriptName
The sudo thing here is very tricky because again your script might get stuck in the interactive mode waiting for a password to be inserted so I suggest you think again on the basis of the script.
mb47!
You want to run the script on the remote computer, correct?
On the remote machine, create a file containing the commands you would like to execute.
Then, on the other machine, run ssh user#machine /path/to/script/you/created/earlier
I hope this helps!
ALinuxLover

run command as other user in unix shell script csh

Assume I am user A(not root user) and I want to run a c-shell script which will execute command in User B(password for B is known to me) and will use the result of that command here.
How can I run a command in User-B.
sudo -u username command1
Above command may prompt for password but I want this to be done in script only.
Thanks for all your suggestions.
you could use a ssh key to allow your user A to log in as user B using user A's private key (and with user A's public key in ~B/.ssh/authorised_keys)
then you simply execute the script as B with:
ssh B#localhost "/path/to/script and maybe some arguments here"
you have following options,
run your command with root so that su - username wont prompt for password
create passordless login for user like passwordless ssh,remove password for user etc. and then run your command
for getting output here to work,
write it in a file and access it in code
store it in a variable and access it in code

ssh to run shell script on remote machine and then copy the output to local machine

I am using plink to execute the shell script on the remote MachineB. And shell script is there on the MachineA(Windows Box).
C:\PLINK>plink uname#MachineB -m test.sh
Using keyboard-interactive authentication.
Password:
Using keyboard-interactive authentication.
Your Kerberos password will expire in 73 days.
And that shell script generates the output in a text file(aa.txt) on MachineB and that gets stored in /export/home/uname/aa.txt
So my question is- Is there any way that I can copy the aa.txt file from MachineB to MachineA as soon the script has completed all its task using the ssh. Or we need to put all these things in Windows Batch file?
So Problem Statement is like this-
Execute the shell script on MachineB from MachineA.
Then wait for the shell script to complete its task, in my case it will write the output to a text file.
And after the shell script has completed all its task means it finished writing everything to a txt file, then copy that txt file to MachineA from MachineB
Any suggestions will be appreciated on how I can achieve the above scenario?
Update:-
So Suppose if this is the content in test.sh shell script file and also after adding pscp at the end of script, then it should be like this?
#!/bin/bash
export HIVE_OPTS="$HIVE_OPTS -hiveconf mapred.job.queue.name=hdmi-technology"
hive -S -e 'SELECT count(*) from testingtable2' > aa.txt
pscp uname#MachineB:/export/home/uname/aa.txt c:\documents\foo.txt
So I am executing a hive query in the above script and whose output is getting stored in aa.txt file and as soon as the query is completed and output is stored in aa.txt file, it will go to fourth line of pscp which will transfer aa.txt file to my local windows machine inside documents folder. Am I right? This will be the whole process?
And if the above process is right as far as I understood, then I can simply go to windows cmd prompt, and do like below and it will do the exact same process. Right?
C:\PLINK>plink uname#MachineB -m test.sh
Using keyboard-interactive authentication.
Password:
Using keyboard-interactive authentication.
Your Kerberos password will expire in 73 days.
Updated Again:-
So I need to create a bat file, and suppose this is the below test.bat file, so content should be like this in that test.bat file-
plink uname#MachineB -m test.sh
pscp uname#MachineB:/export/home/uname/aa.txt c:\documents\foo.txt
You can use scp to download the file after execution. If you setup winsshd on your windows machine and append a copy command in the test.sh file:
scp /export/home/uname/aa.txt user#windowsmachine.com:/homedir
The file will be transferred after completion.
Similarly, you can use a windows scp client like pscp on your windows machine, you can pull the file from the linux machine:
pscp uname#MachineB:/export/home/uname/aa.txt c:\documents\foo.txt
So on windows machine, have a batch script:
plink uname#MachineB -m test.sh
pscp uname#MachineB:/export/home/uname/aa.txt c:\documents\foo.txt
Just to add on to what has already been said, I am running something very similar and use the following format (from a batch script):
plink -ssh user# -pw password -m yourScript.sh
pscp -sftp -pw password user#IP-Address:/path/to/remote/file C:\path\to\local\save\directory
That second command is on one line, but it might not render like that on here. Of course, replace IP-Address, user, and password with the appropriate values for your login :-)
Hope that helps!
EDIT - Sorry, just realized this EXACT answer was already given. Kudos!

Resources