My bash code is:
1 #!/bash/sh
2 set -e
3 USER="admin"
4 HOST="1.2.3.4"
5 PASSWORD="$(cat password.txt)"
6
7 sshpass -p"$PASSWORD" ssh -t $USER@$HOST "/usr/bin/sudo bash -c 'sudo ls 2>&1; echo \"testing\";sudo ls src 2>&1;exit;'" >> log.txt
8
9 echo "------------------------------Another Method ----------------------------------------"
10 sshpass -p"$PASSWORD" ssh -t $USER@$HOST << EOF
11 echo "....1...."
12 sudo ls 2>&1
13 echo "....2...."
14 ls src 2>&1
15 sudo echo "....3...."
16 exit
17 EOF
Line 7 can run multiple commands with sudo and print info to log.txt, Line 9-> Line 17 cannot. But I prefer to use Line9 to 17, because the commands I would like to run may contain nested control statements (such as if-else-fi). I have no idea how to do it in line 7. How to make line 9 to line 17 run multiple commands with sudo and print to local log.txt?
Part of screen output is:
....1....
sudo: no tty present and no askpass program specified
sudo: pam_authenticate: User not known to the underlying authentication module
....2....
tmp
....3....
Any help is welcome. Thanks.