Except script to run a local shell script on remote server using root access

local script:

cat > first.sh
cd /tmp
echo $PWD
echo `whoami`
cd /tmp/123
tar -cvf 789.tar 456
sleep 10
[ctrl-d]

except script:

cat > first
#!/usr/bin/expect
set ip 10.5.15.20
set user "xyz123"
set password "123456"
set script first.sh

spawn sh -c "ssh $user@$ip bash < $script"

expect "Password:"
send "$password\r"

expect eof
[ctrl -d]

When I run the except script, I get the below error because the folder " /tmp/123 " has root permission

bash: line 4: cd: /tmp/123: Permission denied

How can I run first.sh scipt as root user?

It would be better, if you actually know the credentials of the root user, to connect and set up SSH keys to achieve this rather than trying to get round it with expect.

You have to decide on the target server if root is allowed to login, and this may be preventing you, and for a good reason too.

Clearly this is a dummy set up, so what are you actually trying to achieve? There are probably better ways rather than trying to crack in and shovel in direct commands. Think - If you can do it then so can someone else with different motives.

If you can describe what you need then we can probably help you find a much neater way to do this.

Kind regards,
Robin

This looks a lot like the exploit I saw when my server was compromised years ago - login, extract code in a temp dir, sleep.

If you are not actually trying to crack and exploit the destination server, use ssh shared keys as intended! It will be so much easier.