0

I want to login to a Linux environment say ENV1 and run a command from ENV1, to copy a file from ENV1 to ENV2 automatically.

Everything is working fine when i am using below commands.

I am running "BATCH_FILE.bat" which reads command from "COMMANDS_TO_RUN_IN_LINUX.bat" file, to copy file from ENV1 to ENV2.

BATCH_FILE.bat file content as below:

"C:\putty.exe" -ssh myuser_ENV_ONE@ENV1_ipaddress -pw "mypassword_ENV_ONE" -m "C:\COMMANDS_TO_RUN_IN_LINUX.bat"

COMMANDS_TO_RUN_IN_LINUX.bat content as below:

sshpass -p "mypassword_ENV_TWO" scp /u01/file.txt myuser_ENV_TWO@ENV2_ipaddress:/u01/file.txt

Now comes the issue, I want user to pass both the environment ipaddress(ENV1 and ENV2) rather then hard coding and for that I modified the command as below:

Modified BATCH_FILE.bat file content as below:

"C:\putty.exe" -ssh myuser_ENV_ONE@%1 -pw "mypassword_ENV_ONE" -m "C:\COMMANDS_TO_RUN_IN_LINUX.bat"

COMMANDS_TO_RUN_IN_LINUX.bat content as below:

sshpass -p "mypassword_ENV_TWO" scp /u01/file.txt myuser_ENV_TWO@%2:/u01/file.txt

I'm passing ENV1 detail while running the BATCH_FILE.bat from command line and its working fine and is getting logged in ENV1.

But I have no idea now to pass the ENV2 as well which in turn will replace %2.

I tried using %1 instead just to check if the parameter %1 is getting passed from BATCH_FILE.bat to COMMANDS_TO_RUN_IN_LINUX.bat and in turn it will copy file from ENV1 back to ENV1.

But that also gave me an error "unknown host %1" .i.e. it wasn't passed as parameter.

I have gone through some posts but none of them has resolved my issue.

Rahul
  • 191
  • 1
  • 11
  • 1
    Also note that you should probably use Plink, (from PuTTY package), instead of PuTTY itself. See [Automating command/script execution using PuTTY](https://stackoverflow.com/q/39361444/850848). – Martin Prikryl Jul 30 '18 at 17:46
  • Thanks Martin, I have gone through the above link and i agree on how -m works but the workaround mentioned didn't work for me... – Rahul Jul 30 '18 at 18:37
  • I'll try with plink as well and will update – Rahul Jul 30 '18 at 18:39
  • Tried Plink and "C:\plink.exe" -ssh myuser@%1 -pw "mypass123" "/u01/./COPY_SCRIPT.sh %1" this is working i.e. i am able to transfer the file from same environment (used %1 in both places and passed 1 parameter) – Rahul Jul 30 '18 at 19:55
  • But when i am trying to pass 2 parameter as "C:\plink.exe" -ssh myuser@%1 -pw "mypass123" "/u01/./COPY_SCRIPT.sh %2" its not working – Rahul Jul 30 '18 at 19:55
  • and i am getting below error – Rahul Jul 30 '18 at 19:56
  • "C:\plink.exe" -ssh oracle@11.111.11.11 -pw "pass123" "/u01/./COPY_SCRIPT.sh 11.111.11.12" ssh: Could not resolve hostname : Name or service not known lost connection – Rahul Jul 30 '18 at 19:56
  • "C:\plink.exe" -ssh oracle@11.111.11.11 -pw "pass123" "/u01/./COPY_SCRIPT.sh 11.111.11.11" is working fine – Rahul Jul 30 '18 at 19:56
  • So it looks like that 11.111.11.11 is not able to identify 11.111.11.12 ?? – Rahul Jul 30 '18 at 19:57
  • But the strange thing is if i run COPY_SCRIPT.sh manually from putty, copying from 11.111.11.11 to 11.111.11.12 is working fine even with parameters – Rahul Jul 30 '18 at 19:58
  • OK, so now you have a different question. So either edit your current question accordingly. Or accept that this question is duplicate and post a new question for your new problem. – Martin Prikryl Jul 30 '18 at 20:52
  • I have accepted it as duplicate, I'll post new question..Would be great if you can help me in that new question. – Rahul Jul 31 '18 at 05:58

0 Answers0