2

Hi i have created a batch file (run.bat) that after execution connects me to UNIX server with help of plink. But issue starts from this point i have to execute a script after connection to my server the script contains a command sudo -l. After the execution i get the error as mentioned in subject can anyone help me on this issue ??

Batch File-:

"C:\Program Files\PuTTY" plink -ssh -pw Tos@12Ts w44dvftyw@caa1607UX009.wvd.abcd.net /opt/sieb/w44dvftyw/run.sh

Script file(run.sh) -:

#!/bin/bash
sudo -l

It says

sudo: command not found

But when i run my script normally on UNIX server it runs with no issues. What am i missing here to make it work this way please help.

Tushar Sharma
  • 2,839
  • 1
  • 16
  • 38
  • You might want to mask usernames and passwords when posting on a public forum – mjsqu Jun 22 '17 at 14:56
  • 1
    @mjsqu this is not orignal password or userid – Tushar Sharma Jun 22 '17 at 14:57
  • Replacing your command with `"bash -x /opt/sieb/w44dvftyw/run.sh"` might be useful. If it literally just says that `sudo -l` is what's being run, and not something like `sudo -l$'\r'`, that tells us strongly that we need to adjust the PATH. – Charles Duffy Jun 22 '17 at 14:58
  • Check if the user w44dvftyw is added to sudoers (sudo) group. – Yore Jun 22 '17 at 14:59
  • (on which point: Also try sending as a remote command `declare -p PATH`, and compare that to the value you have with an interactive shell). – Charles Duffy Jun 22 '17 at 14:59
  • @Yore, that would be a different error message, no? Or are you supposing a system where `sudo` is only `g+x` and not `o+x`? – Charles Duffy Jun 22 '17 at 14:59
  • @CharlesDuffy let me try. – Tushar Sharma Jun 22 '17 at 15:00
  • I don't think so. When you can't see the command the message is like this. – Yore Jun 22 '17 at 15:00
  • @Yore, agreed, but on most systems `sudo` is world-executable, but *configured* not to perform escalations for users not in the pertinent group. That said, it is certainly *possible* to configure it as you're supposing. – Charles Duffy Jun 22 '17 at 15:01
  • @Yore i am able to sudo normally on server but not from batch file. – Tushar Sharma Jun 22 '17 at 15:01
  • @TusharSharma Usually these problems are caused by lack of permission in some way. – Yore Jun 22 '17 at 15:05
  • @Yore i have given full permissions to file chmod 777 to my file. – Tushar Sharma Jun 22 '17 at 15:07
  • @CharlesDuffy still facing same issue after running "bash -x /opt/sieb/w44dvftyw/run.sh" – Tushar Sharma Jun 22 '17 at 15:08
  • @TusharSharma, that's not intended to *solve* the issue, it's intended to give you debugging output. – Charles Duffy Jun 22 '17 at 15:09
  • BTW, `chmod 777` is a **really** bad idea, and shouldn't be done even for temporary debugging -- it gives anonymous users, including accounts like `nobody` used to run code that hasn't successfully authenticated yet, write access to content you're running as code. (Granted, those accounts should never get to filesystem access, but sandboxing as the `nobody` account is done precisely because control over them is sometimes questionable). – Charles Duffy Jun 22 '17 at 15:10
  • @charles Duffy This it what it gives + sudo -l /opt/sie/w44gasdfdf/a.sh: line 3: sudo: command not found – Tushar Sharma Jun 22 '17 at 15:11
  • @TusharSharma, thank you -- that's helpful (it rules out hidden characters in the remote file). And `declare -p PATH`, compared against the interactive version? – Charles Duffy Jun 22 '17 at 15:11
  • @CharlesDuffy did't get you where to add -p ? – Tushar Sharma Jun 22 '17 at 15:12
  • Run a new command, `declare -p PATH` (or `printf 'PATH=%q\n' "$PATH"`), both as an argument passed as a script to be run, and interactively direct on the remote system (in the scenario where `sudo` works). – Charles Duffy Jun 22 '17 at 15:13
  • @CharlesDuffy now i got this (bash -x /opt/sieb/w44dvftyw/run.sh + declare -p PATH declare -x PATH="/usr/bin:/bin" + sudo -l /opt/sieb/w44dvftyw/run.sh: line 4: sudo: command not found) – Tushar Sharma Jun 22 '17 at 15:18
  • okay, so the `PATH` includes only `/usr/bin` and `/bin`. Now, in an interactive shell, do you get the same thing? And more importantly, what does `type sudo` return (in an interactive shell where `sudo` works properly)? If it shows a location other than `/usr/bin` or `/bin`, well, there's your problem. – Charles Duffy Jun 22 '17 at 15:19
  • @TusharSharma Have a look at this https://stackoverflow.com/questions/21659637/how-to-fix-sudo-no-tty-present-and-no-askpass-program-specified-error see if that helps you. – Yore Jun 22 '17 at 15:21
  • @CharlesDuffy at my unix server where sudo works similar command prints this output- : declare -x PATH="/usr/bin:/bin:/usr/local/bin:/usr/sbin:/usr/local/sbin" – Tushar Sharma Jun 22 '17 at 15:22
  • "At my UNIX server" -- the *same* server or a *different* server? If it's a different server, the one with the failure may just not have `sudo` installed at all. – Charles Duffy Jun 22 '17 at 15:23
  • (and the output from `type sudo` is still important -- it'll tell us where on the system `sudo` is being found, out of all those possible locations in the PATH). – Charles Duffy Jun 22 '17 at 15:24
  • @CharlesDuffy different server . – Tushar Sharma Jun 22 '17 at 15:24
  • Then how do you know the one saying it can't find `sudo` even *has* `sudo` at all? I mean, that's the obvious answer... – Charles Duffy Jun 22 '17 at 15:24
  • @CharlesDuffy i am not getting you i created a bat script which connects me to unix box using plink and i can manually login to that server using putty as well.And when i run sudo -l there it ask for password . – Tushar Sharma Jun 22 '17 at 15:26
  • If you can manually log in, **then do that, and then run `declare -p PATH` and `type sudo` inside that interactive session**. – Charles Duffy Jun 22 '17 at 15:26
  • If you use a completely different machine to run tests about whether and where `sudo` is installed, as opposed to the machine you're using `plink` to connect to, then *of course* the results of those tests are invalid. – Charles Duffy Jun 22 '17 at 15:27
  • @CharlesDuffy i already provided you output for declare -p PATH other one gives this -: sudo is hashed (/usr/local/bin/sudo) – Tushar Sharma Jun 22 '17 at 15:28
  • There's your problem! It's in `/usr/local/bin`, but only `/bin` and `/usr/bin` are in your PATH. – Charles Duffy Jun 22 '17 at 15:28
  • Just put the line `PATH=/bin:/usr/bin:/usr/local/bin` at the front of your script. – Charles Duffy Jun 22 '17 at 15:28
  • @CharlesDuffy thanks sir let me try – Tushar Sharma Jun 22 '17 at 15:29
  • (correcting "at the front" -- it should still be *after* the shebang, but *before* the invocation of `sudo`). – Charles Duffy Jun 22 '17 at 15:30
  • @CharlesDuffy after running it says this now sudo: no tty present and no askpass program specified – Tushar Sharma Jun 22 '17 at 15:36
  • 2
    @TusharSharma Repeating my previous comment: https://stackoverflow.com/questions/21659637/how-to-fix-sudo-no-tty-present-and-no-askpass-program-specified-error – Yore Jun 22 '17 at 15:37

1 Answers1

5

Scripts such as ~/.profile or ~/.bash_profile responsible for setting up the current user's PATH are run only on login shells.

Running sh -c 'somescript' (as performed by ssh host 'somescript') is neither a login shell, nor an interactive shell; thus, it does not gain the benefit of such scripts.

This means that additions to the PATH (in your case, /usr/local/bin) may not be present with commands run in this way.

Among your options:

  • Pass the PATH you want as part of the command to remotely run. This might look like:

    plink -ssh user@host "PATH=/bin:/usr/bin:/usr/local/bin /opt/sieb/w44dvftyw/run.sh"
    
  • Embed a working value in the script you're running:

    #!/bin/bash
    PATH=/bin:/usr/bin:/usr/local/bin 
    # ...put the rest of your script here.
    
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441