Skip to main content
gamedev.net
🔒 Locked

Need help with basic Linux Shell Scripting

Started by Khaos Dragon 9 replies 2.2k views
Original Post
Khaos Dragon
Khaos Dragon
I am trying to do the following to make my work life easier... I am new to linux, but this is the shell script I am trying to write: (note: I modified the addresses and password for this example) ssh root@111.11.11.11 -X #SSH Server will now prompt for password #However I am inside SSH so I simply can't echo it. echo password ssh serverName@222.222.222.2 -X -i identification_key_file If it is impossible to shell script while inside a program, is there a way I can execute shell commands from a c/c++ program?
Khaos Dragon
Khaos Dragon
Anyone has any idea? I thought there were a multitude of linux gurus here.
twxs
twxs
hello,
look at execl function family or system function

hydroo
hydroo
Quote:
Original post by Khaos Dragon
I am trying to do the following to make my work life easier...

I am new to linux, but this is the shell script I am trying to write:
(note: I modified the addresses and password for this example)

ssh root@111.11.11.11 -X
#SSH Server will now prompt for password
#However I am inside SSH so I simply can't echo it.
echo password
ssh serverName@222.222.222.2 -X -i identification_key_file

If it is impossible to shell script while inside a program, is there a way I can execute shell commands from a c/c++ program?


I am not really sure what your problem is, but maybe you want to exchange your ssh-keys, so you don't need to use your password every login?

save the publickey of your computer into the ~/.ssh/authorized_keys on your remotemachine. (this file may differ form system to system @see man ssh)

For executing stuff on the commandline use system(), or popen() (popen provides a pipe to the out or in of the called command, so you can for example read what "ls -l" says.
tstrimp
tstrimp
What are you trying to do? Log in via SSH using a script? Are RSA keys a viable option?

SSH login without password

It allows one computer with the right key to login to another without a password.

Edit: beaten, by a healthy margin too!
Perost
Perost
Quote:
Original post by Khaos Dragon
Anyone has any idea? I thought there were a multitude of linux gurus here.

They're probably hanging around in the "Everything Unix"-forum [smile]

Khaos Dragon
Khaos Dragon
I am actually tunneling from one ssh server to another so flagging a key as a password for the first machine won't fix the issue. I will look at the execl function family and system function however.
Khaos Dragon
Khaos Dragon
Is there a way to write to a console, like perhaps:

WriteToConsole( int PidOfConsole, char* text );

?
Bregma
Bregma
Quote:
Original post by Khaos Dragon
I am trying to do the following to make my work life easier...

I am new to linux, but this is the shell script I am trying to write:
(note: I modified the addresses and password for this example)

ssh root@111.11.11.11 -X
#SSH Server will now prompt for password
#However I am inside SSH so I simply can't echo it.
echo password
ssh serverName@222.222.222.2 -X -i identification_key_file

I am having difficulty understanding the problem. Let me guess what you're trying to do.

You're trying to write a shell script that will connect you to server 1 with an X tunnel, and from there connect you onwards to server 2 (again with an X tunnel). The first server prompts you for a password and you can't plop your own private key on the remote server as documented above, for some undisclosed reason.

Is your problem that you can't type in your password, or is your problem that you the second command does not get executed?

If your problem is the latter, try the following.
ssh -f -Y root@11.11.11.11 "ssh serverName@222.222.222.2 -X -i identification_key_file"

Of course, if you need to do more, you can always drop the -f switch and redirect stdin from a here document.

If the problem is that you can't have the shell script pass the password to ssh, that's because it a wide-open gaping security hole. If you want to use ssh without a password prompt, see above.

Oh, and yes it's possible to write a C program that will open a pseudotty for a forked ssh session, but that's not usually found in introductory courses on Unix systems programming. You'll need to man forkpty() and get to know execl() and select() and wait() pretty thoroughly.
Stephen M. Webb
Professional Free Software Developer
Ravuya
Ravuya
Moving to Everything Unix.
Sander
Sander
Quote:
Original post by Khaos Dragon
I am actually tunneling from one ssh server to another so flagging a key as a password for the first machine won't fix the issue.


Sure it will. Just put your computer's public key into ~/.ssh/authorized_keys on the first server and put the public key of the first server into ~/.ssh/authorized_keys on the second server.

Also, I think ssh can read from standard input. So if you have a file (somecommands.txt) that contains come commands, you can do:

$ ssh username@server < commands.txt

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.