SSH commands work in CMD but not all in batch - ssh

I hope you can help me.
This is the line in CMD:
putty -ssh -P 22 root#192.168.10.100 -i "C:\Users\RNA\MyKey.ppk" -m "C:\Users\RNA\plink_cmds.txt"
All the commands in my batch file work fine except for 'ls'. Why is that?
ls command will throw a sh: ls: cannot execute - No such file or directory.
This is my script:
hostname
cd /tmp
echo '** '
pwd
echo '**'
ls
read

Related

SSHPASS Not Passing Password to SUDO Command

I have the below command and I'm unable to get it to run. It's saying that no password is being supplied for the SUDO command, any idea or help greatly appreciated, I've read every post I can find but to no avail.
sshpass -p $PASSWD ssh client_user#192.169.0.178 'cd /tmp/;echo $PASSWD | sudo -S mkdir ./test/;'
Also tried the below with no luck:
sshpass -p $PASSWD ssh client_user#192.169.0.178 <<EOF
cd /tmp/;
echo $PASSWD | sudo -S mkdir ./test/;
EOF
Error:
sudo: no password was provided

ssh on remote server with nohup - not able to create files

I'm calling a shell script on a remote server with ssh ssh user#host '/dir/myscript.sh'and myscript.sh then runs another script with nohup, but the 2nd script isn't writing to nohup / to any other file with echo "" > test.txt.
myscript.sh
#!/bin/bash
shopt -s expand_aliases
source /home/user/.bash_profile
nohup /dir/myscript-real.sh > my.out 2> my.err < /dev/null &
myscript-real.sh
#!/bin/bash
shopt -s expand_aliases
source /home/user/.bash_profile
echo -e "test"
sleep 15
echo "2nd test"
echo "test" > test.out
exit
When ran via SSH - no file is being created (my.out, my.err or test.out), but when running myscript.sh directly - those 3 files are being created.
Any ideas where is the problem?
Thanks.

mkdir -p over SSH bash

I have a small test script as follows;
TESTDIR="$HOSTNAME"
ssh user#server.com "\$TESTDIR"
mkdir -p ~/$TESTDIR/test
exit
the output with bash -x is;
+ TESTDIR=ndx
+ ssh user#server.com '$TESTDIR'
+ mkdir -p /home/user/ndx/test
+ exit
Yet on the remote server, no directory exists?
The last argument of ssh is command you want to execute on the remote host:
TESTDIR="$HOSTNAME"
ssh user#server.com "mkdir -p ~/$TESTDIR/test"
If you have a pem file to ssh as authentication use the following
ssh -i your-key.pem user#ip_addr "mkdir -p /your_dir_name/test"

How to enable sshpass output to console

Using scp and interactively entering the password the file copy progress is sent to the console but there is no console output when using sshpass in a script to scp files.
$ sshpass -p [password] scp [file] root#[ip]:/[dir]
It seems sshpass is suppressing or hiding the console output of scp. Is there a way to enable the sshpass scp output to console?
After
sudo apt-get install expect
the file send-files.exp works as desired:
#!/usr/bin/expect -f
spawn scp -r $FILES $DEST
match_max 100000
expect "*?assword:*"
send -- "12345\r"
expect eof
Not exactly what was desired, but better than silence:
SSHPASS="12345" sshpass -e scp -v -r $FILES $DEST 2>&1 | grep -v debug1
Note that -e is considered a bit safer than -p.
Output:
Executing: program /usr/bin/ssh host servername, user username, command scp -v -t /src/path/dst_file.txt
OpenSSH_6.6.1, OpenSSL 1.0.1i-fips 6 Aug 2014
Authenticated to servername ([10.11.12.13]:22).
Sending file modes: C0600 590493 src_file.txt
Sink: C0600 590493 src_file.txt
Transferred: sent 594696, received 2600 bytes, in 0.1 seconds
Bytes per second: sent 8920671.8, received 39001.0
In this way:
output=$(sshpass -p $PASSWD scp -v $filename root#192.168.8.1:/root 2>&1)
echo "Output = $output"
you redirect the console output in variable output.
Or, if you only want to see the console output of scp command, you should add only -v command in your ssh pass cmd:
sshpass -p $PASSWD scp -v $filename root#192.168.8.1:/root

Psexec , cmd and batch file

I have a batch file named a.bat on a winserver2008 Desktop.
That batch file only write the SessionID (from environment variable) to a local eventlog.
I want to execute it remotely using cmd (otherwise the SessionName doesn't appear).
so I have tried
c:\PsTools\psexec.exe \\<Server> -u test2 -p <Password> -i 2 cmd "c:\Users\test-2\Desktop\a"
or
c:\PsTools\psexec.exe \\<server> -u test2 -p <Password> -i 2 "cmd \"c:\Users\test-2\Desktop\a\"";exit
all of these just open a terminal on the remote machine but don't execute the batch.
Any ides?
Best Regards,
Use a /c on the command line after cmd.
So, your first line would look like:
c:\PsTools\psexec.exe \\<Server> -u test2 -p <Password> -i 2 cmd /c "c:\Users\test-2\Desktop\a"
psexec \\<server> -s cmd.exe & whatever.bat
If you're trying to run a batch remotely then when cmd is open on the remote through your terminal connection, you have two options:
xcopy \\your_computer\filepath c:\wherever something.bat
run \\computername\c$\wherever_it_is_located