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

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!

Related

FTP using Unix shell script and then triggering a task

I have a requirement at hand in Unix where I need to build a shell script.
The requirement is below:
I need to SFTP a file (let's say CSV file) from my dev server to uat server.
After the SFTP is done to that server, as soon as the file comes there and the exit code of the previous SFTP is 0, I need to trigger a task (this task I can take care of).
I have the basic idea on SFTP but I am not aware of how to trigger the next task as soon as the file comes to the uat server.
Please need a pseudo code to start my exploration.
If you want to copy from somewhere to your local machine and run a command locally
If you have access to ssh then it can be done easily which I am doing it usually.
For example I have a backup file from one of my server. We can get a copy this way using scp
scp root#server:/home/weekly.sql.zip .
. means put the file with its name here on this directory I am in now
the problem with this command is that it has an interaction for getting password so to eliminate this we can install sshpass and use it this way:
sshpass -p'your-password' scp root#server:/home/weekly.sql.zip .
Since we are using bash and it take care of exiting code if you add and && operator then you can add a second command so to be triggered after first command has successfully done.
sshpass -p'your-password' scp root#server:/home/weekly.sql.zip . && unzip weekly.sql.zip
First task is copying a file and second is to unzip it.
installing sshpass:
sudo apt install -y sshpass
If you want to copy from your local machine to somewhere and run a command remotely
sshpass -p'your-password' scp test.txt root#address:/home/ && sshpass -p'your-password' ssh root#address cat /home/test.txt
Which does this:
copy file test.txt to the server
then read it by cat command

Using Plink, how to auto-input password into Ubuntu ssh?

I'm currently setting up a batch file to ssh from a Windows machine into a Ubuntu machine and issue a series of commands. I'm using plink, and I'm using the -m argument to pass a .txt file with a list of commands.
The batch file code that runs through cmd:
set PATH=c:\path\to\plink.exe
plink.exe -ssh -t user#ipaddress -pw <psw> -m c:\path\to\textFile\commands.txt
The commands.txt code:
sudo -s #access the root login
<root psw> #enter the password for the root login
command-1 #issue a command in linux as root
command-2 #issue a command in linux as root
command-3 #issue a command in linux as root
The issue I'm running into is that when I run this batch file, the output within command prompt still prompts the user to manually enter the password. Is there a means to input the password form the next line of the commands.txt file? Or does this process require something else?
As even your question says, the file commands.txt specified by -m switch should contain commands. A password is not a command.
Moreover, the commands in the file are executed one-by-one. The sudo (had it worked) would execute an interactive shell session and wait for a user input (commands). Only once the sudo exits, the following commands (command-1, etc) are executed.
Automating password input for sudo is generally a bad idea. If you need to run some commands that require root privileges, a better solution is to associate a dedicated private key with the commands in sudoers file. And then use sudo and the private key in Plink.
Anyway, to automate an input (of a password in this case) to a command, you need to use an input redirection. The same is true for commands to be executed within (not after) sudo.
Like:
(
echo passwod
echo command-1
echo command-2
) | plink.exe -ssh -t user#ipaddress -pw <psw> sudo -s
As now there's only one real top-level command - sudo, I'm specifying it directly on Plink command-line, instead of using -m switch. Functionally, it's identical.

Script to upload to sftp is not working

I have 2 Linux boxes and i am trying to upload files from one machine to other using sftp. I have put all the commands I use in the terminal to she'll script like below.
#!/bin/bash
cd /home/tests/sftptest
sftp user1#192.168.0.1
cd sftp/sftptest
put test.txt
bye
But this is not working and gives me error like the directory does not exist. Also, the terminal remain in >sftp, which means bye is not executed. How can I fix this?
I suggest to use a here-document:
#!/bin/bash
cd /home/tests/sftptest
sftp user1#192.168.0.1 <<< EOF
cd sftp/sftptest
put test.txt
EOF
When you run the sftp command, it connects and waits for you to type commands. It kind of starts its own "subshell".
The other commands in your script would execute only once the sftp command finishes. And they would obviously execute as local shell commands, so particularly the put will fail as a non existing command.
You have to provide the sftp commands directly to sftp command.
One way to do that, is using an input redirection. E.g. using the "here document" as the answer by #cyrus already shows:
sftp username#host <<< EOF
sftp_command_1
sftp_command_2
EOF
Other way is using an external sftp script:
sftp username#host -b sftp.txt
Where, the sftp.txt script contains:
sftp_command_1
sftp_command_2

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

Ssh to remote server through windows batch

Can anyone suggest me how I can use Windows Batch Program to ssh to remote server and then execute shell scripts over there?
Any simple sample example will be good for me to start on this.
Server Name:- ares-ingest.vip.host.com
Username:- uname
Password:- password
Any suggestions will be appreciated as I am new to Windows Batch Program
Update:-
I tried using plink to execute the shell script that I have on my local machine and I always get the error like below, Is there anything wrong I am doing?
C:\PLINK>plink uname#cli.host.com -m email.sh
Using keyboard-interactive authentication.
Password:
Using keyboard-interactive authentication.
Your Kerberos password will expire in 73 days.
sh: HIVE_OPTS= -hiveconf mapred.job.queue.name=hdmi-technology: is not an identifier
Below is the content in my shell script-
#!/bin/bash
export HIVE_OPTS="$HIVE_OPTS -hiveconf mapred.job.queue.name=hdmi-technology"
hive -S -e 'SELECT count(*) from testingtable2' > attachment.txt
try using plink to send your commands over.
Usage: plink [options] [user#]host [command]

Resources