Execute multiple commands sequentially with libssh2
Asked Answered
T

0

7

I'm looking for a way to execute multiple commands sequentially. What I do right now is creating a mew channel for each command and close It. If I use just one channel I get an error that a channel can not be reused. But I'm not sure If this is the right way because opening a channel for each command sounds costly.

What I'm looking to do is creating a ssh connection to an OpenWrt device which contains an executable called uci which can modify the configuration files on the device and use It like this:

uci set network.lan.ipaddr='192.168.1.2'
uci set network.lan.dns='192.168.1.1'

My code is similar to this:

let tcp = TcpStream::connect("127.0.0.1:22").unwrap();
let mut sess = Session::new().unwrap();
sess.set_tcp_stream(tcp);
sess.handshake().unwrap();
sess.userauth_agent("username").unwrap();

let mut channel = sess.channel_session().unwrap();
channel.exec("ls").unwrap();
channel.wait_close();
println!("#1 exit: {}", channel.exit_status().unwrap());

let mut channel = sess.channel_session().unwrap();
channel.exec("ls").unwrap();
channel.wait_close();
println!("#2 exit: {}", channel.exit_status().unwrap());

If I don't close the channel and execute 2 commands sequentially I get the error code -39(wrong usage).

Tautologism answered 9/11, 2019 at 0:9 Comment(3)
Welcome to StackOverflow! Can you share the code you've started with?Lamellar
I've added some code example which is similar to my code with ssh2.Tautologism
I suppose you could open a shell and interact with it via stdin and stdoutSteviestevy

© 2022 - 2024 — McMap. All rights reserved.